DownloadManager.java revision 64c48b8842ec9f62a48fdd74b669728c5f2408a4
1/*
2 * Copyright (C) 2010 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
17package android.app;
18
19import android.content.ContentResolver;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.content.Context;
23import android.database.Cursor;
24import android.database.CursorWrapper;
25import android.net.ConnectivityManager;
26import android.net.Uri;
27import android.os.Environment;
28import android.os.ParcelFileDescriptor;
29import android.provider.BaseColumns;
30import android.provider.Downloads;
31import android.util.Pair;
32
33import java.io.File;
34import java.io.FileNotFoundException;
35import java.util.ArrayList;
36import java.util.Arrays;
37import java.util.HashSet;
38import java.util.List;
39import java.util.Set;
40
41/**
42 * The download manager is a system service that handles long-running HTTP downloads. Clients may
43 * request that a URI be downloaded to a particular destination file. The download manager will
44 * conduct the download in the background, taking care of HTTP interactions and retrying downloads
45 * after failures or across connectivity changes and system reboots.
46 *
47 * Instances of this class should be obtained through
48 * {@link android.content.Context#getSystemService(String)} by passing
49 * {@link android.content.Context#DOWNLOAD_SERVICE}.
50 *
51 * Apps that request downloads through this API should register a broadcast receiver for
52 * {@link #ACTION_NOTIFICATION_CLICKED} to appropriately handle when the user clicks on a running
53 * download in a notification or from the downloads UI.
54 */
55public class DownloadManager {
56    /**
57     * An identifier for a particular download, unique across the system.  Clients use this ID to
58     * make subsequent calls related to the download.
59     */
60    public final static String COLUMN_ID = BaseColumns._ID;
61
62    /**
63     * The client-supplied title for this download.  This will be displayed in system notifications.
64     * Defaults to the empty string.
65     */
66    public final static String COLUMN_TITLE = "title";
67
68    /**
69     * The client-supplied description of this download.  This will be displayed in system
70     * notifications.  Defaults to the empty string.
71     */
72    public final static String COLUMN_DESCRIPTION = "description";
73
74    /**
75     * URI to be downloaded.
76     */
77    public final static String COLUMN_URI = "uri";
78
79    /**
80     * Internet Media Type of the downloaded file.  If no value is provided upon creation, this will
81     * initially be null and will be filled in based on the server's response once the download has
82     * started.
83     *
84     * @see <a href="http://www.ietf.org/rfc/rfc1590.txt">RFC 1590, defining Media Types</a>
85     */
86    public final static String COLUMN_MEDIA_TYPE = "media_type";
87
88    /**
89     * Total size of the download in bytes.  This will initially be -1 and will be filled in once
90     * the download starts.
91     */
92    public final static String COLUMN_TOTAL_SIZE_BYTES = "total_size";
93
94    /**
95     * Uri where downloaded file will be stored.  If a destination is supplied by client, that URI
96     * will be used here.  Otherwise, the value will initially be null and will be filled in with a
97     * generated URI once the download has started.
98     */
99    public final static String COLUMN_LOCAL_URI = "local_uri";
100
101    /**
102     * Current status of the download, as one of the STATUS_* constants.
103     */
104    public final static String COLUMN_STATUS = "status";
105
106    /**
107     * Provides more detail on the status of the download.  Its meaning depends on the value of
108     * {@link #COLUMN_STATUS}.
109     *
110     * When {@link #COLUMN_STATUS} is {@link #STATUS_FAILED}, this indicates the type of error that
111     * occurred.  If an HTTP error occurred, this will hold the HTTP status code as defined in RFC
112     * 2616.  Otherwise, it will hold one of the ERROR_* constants.
113     *
114     * When {@link #COLUMN_STATUS} is {@link #STATUS_PAUSED}, this indicates why the download is
115     * paused.  It will hold one of the PAUSED_* constants.
116     *
117     * If {@link #COLUMN_STATUS} is neither {@link #STATUS_FAILED} nor {@link #STATUS_PAUSED}, this
118     * column's value is undefined.
119     *
120     * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1">RFC 2616
121     * status codes</a>
122     */
123    public final static String COLUMN_REASON = "reason";
124
125    /**
126     * Number of bytes download so far.
127     */
128    public final static String COLUMN_BYTES_DOWNLOADED_SO_FAR = "bytes_so_far";
129
130    /**
131     * Timestamp when the download was last modified, in {@link System#currentTimeMillis
132     * System.currentTimeMillis()} (wall clock time in UTC).
133     */
134    public final static String COLUMN_LAST_MODIFIED_TIMESTAMP = "last_modified_timestamp";
135
136
137    /**
138     * Value of {@link #COLUMN_STATUS} when the download is waiting to start.
139     */
140    public final static int STATUS_PENDING = 1 << 0;
141
142    /**
143     * Value of {@link #COLUMN_STATUS} when the download is currently running.
144     */
145    public final static int STATUS_RUNNING = 1 << 1;
146
147    /**
148     * Value of {@link #COLUMN_STATUS} when the download is waiting to retry or resume.
149     */
150    public final static int STATUS_PAUSED = 1 << 2;
151
152    /**
153     * Value of {@link #COLUMN_STATUS} when the download has successfully completed.
154     */
155    public final static int STATUS_SUCCESSFUL = 1 << 3;
156
157    /**
158     * Value of {@link #COLUMN_STATUS} when the download has failed (and will not be retried).
159     */
160    public final static int STATUS_FAILED = 1 << 4;
161
162
163    /**
164     * Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit
165     * under any other error code.
166     */
167    public final static int ERROR_UNKNOWN = 1000;
168
169    /**
170     * Value of {@link #COLUMN_REASON} when a storage issue arises which doesn't fit under any
171     * other error code. Use the more specific {@link #ERROR_INSUFFICIENT_SPACE} and
172     * {@link #ERROR_DEVICE_NOT_FOUND} when appropriate.
173     */
174    public final static int ERROR_FILE_ERROR = 1001;
175
176    /**
177     * Value of {@link #COLUMN_REASON} when an HTTP code was received that download manager
178     * can't handle.
179     */
180    public final static int ERROR_UNHANDLED_HTTP_CODE = 1002;
181
182    /**
183     * Value of {@link #COLUMN_REASON} when an error receiving or processing data occurred at
184     * the HTTP level.
185     */
186    public final static int ERROR_HTTP_DATA_ERROR = 1004;
187
188    /**
189     * Value of {@link #COLUMN_REASON} when there were too many redirects.
190     */
191    public final static int ERROR_TOO_MANY_REDIRECTS = 1005;
192
193    /**
194     * Value of {@link #COLUMN_REASON} when there was insufficient storage space. Typically,
195     * this is because the SD card is full.
196     */
197    public final static int ERROR_INSUFFICIENT_SPACE = 1006;
198
199    /**
200     * Value of {@link #COLUMN_REASON} when no external storage device was found. Typically,
201     * this is because the SD card is not mounted.
202     */
203    public final static int ERROR_DEVICE_NOT_FOUND = 1007;
204
205    /**
206     * Value of {@link #COLUMN_REASON} when some possibly transient error occurred but we can't
207     * resume the download.
208     */
209    public final static int ERROR_CANNOT_RESUME = 1008;
210
211    /**
212     * Value of {@link #COLUMN_REASON} when the requested destination file already exists (the
213     * download manager will not overwrite an existing file).
214     */
215    public final static int ERROR_FILE_ALREADY_EXISTS = 1009;
216
217    /**
218     * Value of {@link #COLUMN_REASON} when the download is paused because some network error
219     * occurred and the download manager is waiting before retrying the request.
220     */
221    public final static int PAUSED_WAITING_TO_RETRY = 1;
222
223    /**
224     * Value of {@link #COLUMN_REASON} when the download is waiting for network connectivity to
225     * proceed.
226     */
227    public final static int PAUSED_WAITING_FOR_NETWORK = 2;
228
229    /**
230     * Value of {@link #COLUMN_REASON} when the download exceeds a size limit for downloads over
231     * the mobile network and the download manager is waiting for a Wi-Fi connection to proceed.
232     */
233    public final static int PAUSED_QUEUED_FOR_WIFI = 3;
234
235    /**
236     * Value of {@link #COLUMN_REASON} when the download is paused for some other reason.
237     */
238    public final static int PAUSED_UNKNOWN = 4;
239
240    /**
241     * Broadcast intent action sent by the download manager when a download completes.
242     */
243    public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE";
244
245    /**
246     * Broadcast intent action sent by the download manager when the user clicks on a running
247     * download, either from a system notification or from the downloads UI.
248     */
249    public final static String ACTION_NOTIFICATION_CLICKED =
250            "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
251
252    /**
253     * Intent action to launch an activity to display all downloads.
254     */
255    public final static String ACTION_VIEW_DOWNLOADS = "android.intent.action.VIEW_DOWNLOADS";
256
257    /**
258     * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a
259     * long) of the download that just completed.
260     */
261    public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
262
263    // this array must contain all public columns
264    private static final String[] COLUMNS = new String[] {
265        COLUMN_ID,
266        COLUMN_TITLE,
267        COLUMN_DESCRIPTION,
268        COLUMN_URI,
269        COLUMN_MEDIA_TYPE,
270        COLUMN_TOTAL_SIZE_BYTES,
271        COLUMN_LOCAL_URI,
272        COLUMN_STATUS,
273        COLUMN_REASON,
274        COLUMN_BYTES_DOWNLOADED_SO_FAR,
275        COLUMN_LAST_MODIFIED_TIMESTAMP
276    };
277
278    // columns to request from DownloadProvider
279    private static final String[] UNDERLYING_COLUMNS = new String[] {
280        Downloads.Impl._ID,
281        Downloads.COLUMN_TITLE,
282        Downloads.COLUMN_DESCRIPTION,
283        Downloads.COLUMN_URI,
284        Downloads.COLUMN_MIME_TYPE,
285        Downloads.COLUMN_TOTAL_BYTES,
286        Downloads.COLUMN_STATUS,
287        Downloads.COLUMN_CURRENT_BYTES,
288        Downloads.COLUMN_LAST_MODIFICATION,
289        Downloads.COLUMN_DESTINATION,
290        Downloads.Impl.COLUMN_FILE_NAME_HINT,
291        Downloads.Impl._DATA,
292    };
293
294    private static final Set<String> LONG_COLUMNS = new HashSet<String>(
295            Arrays.asList(COLUMN_ID, COLUMN_TOTAL_SIZE_BYTES, COLUMN_STATUS, COLUMN_REASON,
296                          COLUMN_BYTES_DOWNLOADED_SO_FAR, COLUMN_LAST_MODIFIED_TIMESTAMP));
297
298    /**
299     * This class contains all the information necessary to request a new download. The URI is the
300     * only required parameter.
301     *
302     * Note that the default download destination is a shared volume where the system might delete
303     * your file if it needs to reclaim space for system use. If this is a problem, use a location
304     * on external storage (see {@link #setDestinationUri(Uri)}.
305     */
306    public static class Request {
307        /**
308         * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
309         * {@link ConnectivityManager#TYPE_MOBILE}.
310         */
311        public static final int NETWORK_MOBILE = 1 << 0;
312
313        /**
314         * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
315         * {@link ConnectivityManager#TYPE_WIFI}.
316         */
317        public static final int NETWORK_WIFI = 1 << 1;
318
319        private Uri mUri;
320        private Uri mDestinationUri;
321        private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>();
322        private CharSequence mTitle;
323        private CharSequence mDescription;
324        private boolean mShowNotification = true;
325        private String mMimeType;
326        private boolean mRoamingAllowed = true;
327        private int mAllowedNetworkTypes = ~0; // default to all network types allowed
328        private boolean mIsVisibleInDownloadsUi = true;
329
330        /**
331         * @param uri the HTTP URI to download.
332         */
333        public Request(Uri uri) {
334            if (uri == null) {
335                throw new NullPointerException();
336            }
337            String scheme = uri.getScheme();
338            if (scheme == null || !scheme.equals("http")) {
339                throw new IllegalArgumentException("Can only download HTTP URIs: " + uri);
340            }
341            mUri = uri;
342        }
343
344        /**
345         * Set the local destination for the downloaded file. Must be a file URI to a path on
346         * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE
347         * permission.
348         *
349         * By default, downloads are saved to a generated filename in the shared download cache and
350         * may be deleted by the system at any time to reclaim space.
351         *
352         * @return this object
353         */
354        public Request setDestinationUri(Uri uri) {
355            mDestinationUri = uri;
356            return this;
357        }
358
359        /**
360         * Set the local destination for the downloaded file to a path within the application's
361         * external files directory (as returned by {@link Context#getExternalFilesDir(String)}.
362         *
363         * @param context the {@link Context} to use in determining the external files directory
364         * @param dirType the directory type to pass to {@link Context#getExternalFilesDir(String)}
365         * @param subPath the path within the external directory, including the destination filename
366         * @return this object
367         */
368        public Request setDestinationInExternalFilesDir(Context context, String dirType,
369                String subPath) {
370            setDestinationFromBase(context.getExternalFilesDir(dirType), subPath);
371            return this;
372        }
373
374        /**
375         * Set the local destination for the downloaded file to a path within the public external
376         * storage directory (as returned by
377         * {@link Environment#getExternalStoragePublicDirectory(String)}.
378         *
379         * @param dirType the directory type to pass to
380         *        {@link Environment#getExternalStoragePublicDirectory(String)}
381         * @param subPath the path within the external directory, including the destination filename
382         * @return this object
383         */
384        public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
385            setDestinationFromBase(Environment.getExternalStoragePublicDirectory(dirType), subPath);
386            return this;
387        }
388
389        private void setDestinationFromBase(File base, String subPath) {
390            if (subPath == null) {
391                throw new NullPointerException("subPath cannot be null");
392            }
393            mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath);
394        }
395
396        /**
397         * Add an HTTP header to be included with the download request.  The header will be added to
398         * the end of the list.
399         * @param header HTTP header name
400         * @param value header value
401         * @return this object
402         * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">HTTP/1.1
403         *      Message Headers</a>
404         */
405        public Request addRequestHeader(String header, String value) {
406            if (header == null) {
407                throw new NullPointerException("header cannot be null");
408            }
409            if (header.contains(":")) {
410                throw new IllegalArgumentException("header may not contain ':'");
411            }
412            if (value == null) {
413                value = "";
414            }
415            mRequestHeaders.add(Pair.create(header, value));
416            return this;
417        }
418
419        /**
420         * Set the title of this download, to be displayed in notifications (if enabled).  If no
421         * title is given, a default one will be assigned based on the download filename, once the
422         * download starts.
423         * @return this object
424         */
425        public Request setTitle(CharSequence title) {
426            mTitle = title;
427            return this;
428        }
429
430        /**
431         * Set a description of this download, to be displayed in notifications (if enabled)
432         * @return this object
433         */
434        public Request setDescription(CharSequence description) {
435            mDescription = description;
436            return this;
437        }
438
439        /**
440         * Set the MIME content type of this download.  This will override the content type declared
441         * in the server's response.
442         * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1
443         *      Media Types</a>
444         * @return this object
445         */
446        public Request setMimeType(String mimeType) {
447            mMimeType = mimeType;
448            return this;
449        }
450
451        /**
452         * Control whether a system notification is posted by the download manager while this
453         * download is running. If enabled, the download manager posts notifications about downloads
454         * through the system {@link android.app.NotificationManager}. By default, a notification is
455         * shown.
456         *
457         * If set to false, this requires the permission
458         * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
459         *
460         * @param show whether the download manager should show a notification for this download.
461         * @return this object
462         */
463        public Request setShowRunningNotification(boolean show) {
464            mShowNotification = show;
465            return this;
466        }
467
468        /**
469         * Restrict the types of networks over which this download may proceed.  By default, all
470         * network types are allowed.
471         * @param flags any combination of the NETWORK_* bit flags.
472         * @return this object
473         */
474        public Request setAllowedNetworkTypes(int flags) {
475            mAllowedNetworkTypes = flags;
476            return this;
477        }
478
479        /**
480         * Set whether this download may proceed over a roaming connection.  By default, roaming is
481         * allowed.
482         * @param allowed whether to allow a roaming connection to be used
483         * @return this object
484         */
485        public Request setAllowedOverRoaming(boolean allowed) {
486            mRoamingAllowed = allowed;
487            return this;
488        }
489
490        /**
491         * Set whether this download should be displayed in the system's Downloads UI. True by
492         * default.
493         * @param isVisible whether to display this download in the Downloads UI
494         * @return this object
495         */
496        public Request setVisibleInDownloadsUi(boolean isVisible) {
497            mIsVisibleInDownloadsUi = isVisible;
498            return this;
499        }
500
501        /**
502         * @return ContentValues to be passed to DownloadProvider.insert()
503         */
504        ContentValues toContentValues(String packageName) {
505            ContentValues values = new ContentValues();
506            assert mUri != null;
507            values.put(Downloads.COLUMN_URI, mUri.toString());
508            values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true);
509            values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE, packageName);
510
511            if (mDestinationUri != null) {
512                values.put(Downloads.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI);
513                values.put(Downloads.COLUMN_FILE_NAME_HINT, mDestinationUri.toString());
514            } else {
515                values.put(Downloads.COLUMN_DESTINATION,
516                           Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE);
517            }
518
519            if (!mRequestHeaders.isEmpty()) {
520                encodeHttpHeaders(values);
521            }
522
523            putIfNonNull(values, Downloads.COLUMN_TITLE, mTitle);
524            putIfNonNull(values, Downloads.COLUMN_DESCRIPTION, mDescription);
525            putIfNonNull(values, Downloads.COLUMN_MIME_TYPE, mMimeType);
526
527            values.put(Downloads.COLUMN_VISIBILITY,
528                    mShowNotification ? Downloads.VISIBILITY_VISIBLE
529                            : Downloads.VISIBILITY_HIDDEN);
530
531            values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes);
532            values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed);
533            values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi);
534
535            return values;
536        }
537
538        private void encodeHttpHeaders(ContentValues values) {
539            int index = 0;
540            for (Pair<String, String> header : mRequestHeaders) {
541                String headerString = header.first + ": " + header.second;
542                values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString);
543                index++;
544            }
545        }
546
547        private void putIfNonNull(ContentValues contentValues, String key, Object value) {
548            if (value != null) {
549                contentValues.put(key, value.toString());
550            }
551        }
552    }
553
554    /**
555     * This class may be used to filter download manager queries.
556     */
557    public static class Query {
558        /**
559         * Constant for use with {@link #orderBy}
560         * @hide
561         */
562        public static final int ORDER_ASCENDING = 1;
563
564        /**
565         * Constant for use with {@link #orderBy}
566         * @hide
567         */
568        public static final int ORDER_DESCENDING = 2;
569
570        private long[] mIds = null;
571        private Integer mStatusFlags = null;
572        private String mOrderByColumn = Downloads.COLUMN_LAST_MODIFICATION;
573        private int mOrderDirection = ORDER_DESCENDING;
574        private boolean mOnlyIncludeVisibleInDownloadsUi = false;
575
576        /**
577         * Include only the downloads with the given IDs.
578         * @return this object
579         */
580        public Query setFilterById(long... ids) {
581            mIds = ids;
582            return this;
583        }
584
585        /**
586         * Include only downloads with status matching any the given status flags.
587         * @param flags any combination of the STATUS_* bit flags
588         * @return this object
589         */
590        public Query setFilterByStatus(int flags) {
591            mStatusFlags = flags;
592            return this;
593        }
594
595        /**
596         * Controls whether this query includes downloads not visible in the system's Downloads UI.
597         * @param value if true, this query will only include downloads that should be displayed in
598         *            the system's Downloads UI; if false (the default), this query will include
599         *            both visible and invisible downloads.
600         * @return this object
601         * @hide
602         */
603        public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) {
604            mOnlyIncludeVisibleInDownloadsUi = value;
605            return this;
606        }
607
608        /**
609         * Change the sort order of the returned Cursor.
610         *
611         * @param column one of the COLUMN_* constants; currently, only
612         *         {@link #COLUMN_LAST_MODIFIED_TIMESTAMP} and {@link #COLUMN_TOTAL_SIZE_BYTES} are
613         *         supported.
614         * @param direction either {@link #ORDER_ASCENDING} or {@link #ORDER_DESCENDING}
615         * @return this object
616         * @hide
617         */
618        public Query orderBy(String column, int direction) {
619            if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) {
620                throw new IllegalArgumentException("Invalid direction: " + direction);
621            }
622
623            if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) {
624                mOrderByColumn = Downloads.COLUMN_LAST_MODIFICATION;
625            } else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
626                mOrderByColumn = Downloads.COLUMN_TOTAL_BYTES;
627            } else {
628                throw new IllegalArgumentException("Cannot order by " + column);
629            }
630            mOrderDirection = direction;
631            return this;
632        }
633
634        /**
635         * Run this query using the given ContentResolver.
636         * @param projection the projection to pass to ContentResolver.query()
637         * @return the Cursor returned by ContentResolver.query()
638         */
639        Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) {
640            Uri uri = baseUri;
641            List<String> selectionParts = new ArrayList<String>();
642            String[] selectionArgs = null;
643
644            if (mIds != null) {
645                selectionParts.add(getWhereClauseForIds(mIds));
646                selectionArgs = getWhereArgsForIds(mIds);
647            }
648
649            if (mStatusFlags != null) {
650                List<String> parts = new ArrayList<String>();
651                if ((mStatusFlags & STATUS_PENDING) != 0) {
652                    parts.add(statusClause("=", Downloads.STATUS_PENDING));
653                }
654                if ((mStatusFlags & STATUS_RUNNING) != 0) {
655                    parts.add(statusClause("=", Downloads.STATUS_RUNNING));
656                }
657                if ((mStatusFlags & STATUS_PAUSED) != 0) {
658                    parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP));
659                    parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY));
660                    parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK));
661                    parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI));
662                }
663                if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) {
664                    parts.add(statusClause("=", Downloads.STATUS_SUCCESS));
665                }
666                if ((mStatusFlags & STATUS_FAILED) != 0) {
667                    parts.add("(" + statusClause(">=", 400)
668                              + " AND " + statusClause("<", 600) + ")");
669                }
670                selectionParts.add(joinStrings(" OR ", parts));
671            }
672
673            if (mOnlyIncludeVisibleInDownloadsUi) {
674                selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'");
675            }
676
677            String selection = joinStrings(" AND ", selectionParts);
678            String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC");
679            String orderBy = mOrderByColumn + " " + orderDirection;
680
681            return resolver.query(uri, projection, selection, selectionArgs, orderBy);
682        }
683
684        private String joinStrings(String joiner, Iterable<String> parts) {
685            StringBuilder builder = new StringBuilder();
686            boolean first = true;
687            for (String part : parts) {
688                if (!first) {
689                    builder.append(joiner);
690                }
691                builder.append(part);
692                first = false;
693            }
694            return builder.toString();
695        }
696
697        private String statusClause(String operator, int value) {
698            return Downloads.COLUMN_STATUS + operator + "'" + value + "'";
699        }
700    }
701
702    private ContentResolver mResolver;
703    private String mPackageName;
704    private Uri mBaseUri = Downloads.Impl.CONTENT_URI;
705
706    /**
707     * @hide
708     */
709    public DownloadManager(ContentResolver resolver, String packageName) {
710        mResolver = resolver;
711        mPackageName = packageName;
712    }
713
714    /**
715     * Makes this object access the download provider through /all_downloads URIs rather than
716     * /my_downloads URIs, for clients that have permission to do so.
717     * @hide
718     */
719    public void setAccessAllDownloads(boolean accessAllDownloads) {
720        if (accessAllDownloads) {
721            mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI;
722        } else {
723            mBaseUri = Downloads.Impl.CONTENT_URI;
724        }
725    }
726
727    /**
728     * Enqueue a new download.  The download will start automatically once the download manager is
729     * ready to execute it and connectivity is available.
730     *
731     * @param request the parameters specifying this download
732     * @return an ID for the download, unique across the system.  This ID is used to make future
733     * calls related to this download.
734     */
735    public long enqueue(Request request) {
736        ContentValues values = request.toContentValues(mPackageName);
737        Uri downloadUri = mResolver.insert(Downloads.CONTENT_URI, values);
738        long id = Long.parseLong(downloadUri.getLastPathSegment());
739        return id;
740    }
741
742    /**
743     * Cancel downloads and remove them from the download manager.  Each download will be stopped if
744     * it was running, and it will no longer be accessible through the download manager.  If a file
745     * was already downloaded to external storage, it will not be deleted.
746     *
747     * @param ids the IDs of the downloads to remove
748     * @return the number of downloads actually removed
749     */
750    public int remove(long... ids) {
751        StringBuilder whereClause = new StringBuilder();
752        String[] whereArgs = new String[ids.length];
753
754        whereClause.append(Downloads.Impl._ID + " IN (");
755        for (int i = 0; i < ids.length; i++) {
756            if (i > 0) {
757                whereClause.append(",");
758            }
759            whereClause.append("?");
760            whereArgs[i] = Long.toString(ids[i]);
761        }
762        whereClause.append(")");
763
764        return mResolver.delete(mBaseUri, whereClause.toString(), whereArgs);
765    }
766
767    /**
768     * Query the download manager about downloads that have been requested.
769     * @param query parameters specifying filters for this query
770     * @return a Cursor over the result set of downloads, with columns consisting of all the
771     * COLUMN_* constants.
772     */
773    public Cursor query(Query query) {
774        Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri);
775        if (underlyingCursor == null) {
776            return null;
777        }
778        return new CursorTranslator(underlyingCursor, mBaseUri);
779    }
780
781    /**
782     * Open a downloaded file for reading.  The download must have completed.
783     * @param id the ID of the download
784     * @return a read-only {@link ParcelFileDescriptor}
785     * @throws FileNotFoundException if the destination file does not already exist
786     */
787    public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException {
788        return mResolver.openFileDescriptor(getDownloadUri(id), "r");
789    }
790
791    /**
792     * Restart the given downloads, which must have already completed (successfully or not).  This
793     * method will only work when called from within the download manager's process.
794     * @param ids the IDs of the downloads
795     * @hide
796     */
797    public void restartDownload(long... ids) {
798        Cursor cursor = query(new Query().setFilterById(ids));
799        try {
800            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
801                int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS));
802                if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) {
803                    throw new IllegalArgumentException("Cannot restart incomplete download: "
804                            + cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
805                }
806            }
807        } finally {
808            cursor.close();
809        }
810
811        ContentValues values = new ContentValues();
812        values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0);
813        values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1);
814        values.putNull(Downloads.Impl._DATA);
815        values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
816        mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids));
817    }
818
819    /**
820     * Get the DownloadProvider URI for the download with the given ID.
821     */
822    Uri getDownloadUri(long id) {
823        return ContentUris.withAppendedId(mBaseUri, id);
824    }
825
826    /**
827     * Get a parameterized SQL WHERE clause to select a bunch of IDs.
828     */
829    static String getWhereClauseForIds(long[] ids) {
830        StringBuilder whereClause = new StringBuilder();
831        whereClause.append(Downloads.Impl._ID + " IN (");
832        for (int i = 0; i < ids.length; i++) {
833            if (i > 0) {
834                whereClause.append(",");
835            }
836            whereClause.append("?");
837        }
838        whereClause.append(")");
839        return whereClause.toString();
840    }
841
842    /**
843     * Get the selection args for a clause returned by {@link #getWhereClauseForIds(long[])}.
844     */
845    static String[] getWhereArgsForIds(long[] ids) {
846        String[] whereArgs = new String[ids.length];
847        for (int i = 0; i < ids.length; i++) {
848            whereArgs[i] = Long.toString(ids[i]);
849        }
850        return whereArgs;
851    }
852
853    /**
854     * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and
855     * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants.
856     * Some columns correspond directly to underlying values while others are computed from
857     * underlying data.
858     */
859    private static class CursorTranslator extends CursorWrapper {
860        private Uri mBaseUri;
861
862        public CursorTranslator(Cursor cursor, Uri baseUri) {
863            super(cursor);
864            mBaseUri = baseUri;
865        }
866
867        @Override
868        public int getColumnIndex(String columnName) {
869            return Arrays.asList(COLUMNS).indexOf(columnName);
870        }
871
872        @Override
873        public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {
874            int index = getColumnIndex(columnName);
875            if (index == -1) {
876                throw new IllegalArgumentException("No such column: " + columnName);
877            }
878            return index;
879        }
880
881        @Override
882        public String getColumnName(int columnIndex) {
883            int numColumns = COLUMNS.length;
884            if (columnIndex < 0 || columnIndex >= numColumns) {
885                throw new IllegalArgumentException("Invalid column index " + columnIndex + ", "
886                                                   + numColumns + " columns exist");
887            }
888            return COLUMNS[columnIndex];
889        }
890
891        @Override
892        public String[] getColumnNames() {
893            String[] returnColumns = new String[COLUMNS.length];
894            System.arraycopy(COLUMNS, 0, returnColumns, 0, COLUMNS.length);
895            return returnColumns;
896        }
897
898        @Override
899        public int getColumnCount() {
900            return COLUMNS.length;
901        }
902
903        @Override
904        public byte[] getBlob(int columnIndex) {
905            throw new UnsupportedOperationException();
906        }
907
908        @Override
909        public double getDouble(int columnIndex) {
910            return getLong(columnIndex);
911        }
912
913        private boolean isLongColumn(String column) {
914            return LONG_COLUMNS.contains(column);
915        }
916
917        @Override
918        public float getFloat(int columnIndex) {
919            return (float) getDouble(columnIndex);
920        }
921
922        @Override
923        public int getInt(int columnIndex) {
924            return (int) getLong(columnIndex);
925        }
926
927        @Override
928        public long getLong(int columnIndex) {
929            return translateLong(getColumnName(columnIndex));
930        }
931
932        @Override
933        public short getShort(int columnIndex) {
934            return (short) getLong(columnIndex);
935        }
936
937        @Override
938        public String getString(int columnIndex) {
939            return translateString(getColumnName(columnIndex));
940        }
941
942        private String translateString(String column) {
943            if (isLongColumn(column)) {
944                return Long.toString(translateLong(column));
945            }
946            if (column.equals(COLUMN_TITLE)) {
947                return getUnderlyingString(Downloads.COLUMN_TITLE);
948            }
949            if (column.equals(COLUMN_DESCRIPTION)) {
950                return getUnderlyingString(Downloads.COLUMN_DESCRIPTION);
951            }
952            if (column.equals(COLUMN_URI)) {
953                return getUnderlyingString(Downloads.COLUMN_URI);
954            }
955            if (column.equals(COLUMN_MEDIA_TYPE)) {
956                return getUnderlyingString(Downloads.COLUMN_MIME_TYPE);
957            }
958
959            assert column.equals(COLUMN_LOCAL_URI);
960            return getLocalUri();
961        }
962
963        private String getLocalUri() {
964            long destinationType = getUnderlyingLong(Downloads.Impl.COLUMN_DESTINATION);
965            if (destinationType == Downloads.Impl.DESTINATION_FILE_URI) {
966                // return client-provided file URI for external download
967                return getUnderlyingString(Downloads.Impl.COLUMN_FILE_NAME_HINT);
968            }
969
970            if (destinationType == Downloads.Impl.DESTINATION_EXTERNAL) {
971                // return stored destination for legacy external download
972                String localPath = getUnderlyingString(Downloads.Impl._DATA);
973                if (localPath == null) {
974                    return null;
975                }
976                return Uri.fromFile(new File(localPath)).toString();
977            }
978
979            // return content URI for cache download
980            long downloadId = getUnderlyingLong(Downloads.Impl._ID);
981            return ContentUris.withAppendedId(mBaseUri, downloadId).toString();
982        }
983
984        private long translateLong(String column) {
985            if (!isLongColumn(column)) {
986                // mimic behavior of underlying cursor -- most likely, throw NumberFormatException
987                return Long.valueOf(translateString(column));
988            }
989
990            if (column.equals(COLUMN_ID)) {
991                return getUnderlyingLong(Downloads.Impl._ID);
992            }
993            if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
994                return getUnderlyingLong(Downloads.COLUMN_TOTAL_BYTES);
995            }
996            if (column.equals(COLUMN_STATUS)) {
997                return translateStatus((int) getUnderlyingLong(Downloads.COLUMN_STATUS));
998            }
999            if (column.equals(COLUMN_REASON)) {
1000                return getReason((int) getUnderlyingLong(Downloads.COLUMN_STATUS));
1001            }
1002            if (column.equals(COLUMN_BYTES_DOWNLOADED_SO_FAR)) {
1003                return getUnderlyingLong(Downloads.COLUMN_CURRENT_BYTES);
1004            }
1005            assert column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP);
1006            return getUnderlyingLong(Downloads.COLUMN_LAST_MODIFICATION);
1007        }
1008
1009        private long getReason(int status) {
1010            switch (translateStatus(status)) {
1011                case STATUS_FAILED:
1012                    return getErrorCode(status);
1013
1014                case STATUS_PAUSED:
1015                    return getPausedReason(status);
1016
1017                default:
1018                    return 0; // arbitrary value when status is not an error
1019            }
1020        }
1021
1022        private long getPausedReason(int status) {
1023            switch (status) {
1024                case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1025                    return PAUSED_WAITING_TO_RETRY;
1026
1027                case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1028                    return PAUSED_WAITING_FOR_NETWORK;
1029
1030                case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
1031                    return PAUSED_QUEUED_FOR_WIFI;
1032
1033                default:
1034                    return PAUSED_UNKNOWN;
1035            }
1036        }
1037
1038        private long getErrorCode(int status) {
1039            if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS)
1040                    || (500 <= status && status < 600)) {
1041                // HTTP status code
1042                return status;
1043            }
1044
1045            switch (status) {
1046                case Downloads.STATUS_FILE_ERROR:
1047                    return ERROR_FILE_ERROR;
1048
1049                case Downloads.STATUS_UNHANDLED_HTTP_CODE:
1050                case Downloads.STATUS_UNHANDLED_REDIRECT:
1051                    return ERROR_UNHANDLED_HTTP_CODE;
1052
1053                case Downloads.STATUS_HTTP_DATA_ERROR:
1054                    return ERROR_HTTP_DATA_ERROR;
1055
1056                case Downloads.STATUS_TOO_MANY_REDIRECTS:
1057                    return ERROR_TOO_MANY_REDIRECTS;
1058
1059                case Downloads.STATUS_INSUFFICIENT_SPACE_ERROR:
1060                    return ERROR_INSUFFICIENT_SPACE;
1061
1062                case Downloads.STATUS_DEVICE_NOT_FOUND_ERROR:
1063                    return ERROR_DEVICE_NOT_FOUND;
1064
1065                case Downloads.Impl.STATUS_CANNOT_RESUME:
1066                    return ERROR_CANNOT_RESUME;
1067
1068                case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR:
1069                    return ERROR_FILE_ALREADY_EXISTS;
1070
1071                default:
1072                    return ERROR_UNKNOWN;
1073            }
1074        }
1075
1076        private long getUnderlyingLong(String column) {
1077            return super.getLong(super.getColumnIndex(column));
1078        }
1079
1080        private String getUnderlyingString(String column) {
1081            return super.getString(super.getColumnIndex(column));
1082        }
1083
1084        private int translateStatus(int status) {
1085            switch (status) {
1086                case Downloads.STATUS_PENDING:
1087                    return STATUS_PENDING;
1088
1089                case Downloads.STATUS_RUNNING:
1090                    return STATUS_RUNNING;
1091
1092                case Downloads.Impl.STATUS_PAUSED_BY_APP:
1093                case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1094                case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1095                case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
1096                    return STATUS_PAUSED;
1097
1098                case Downloads.STATUS_SUCCESS:
1099                    return STATUS_SUCCESSFUL;
1100
1101                default:
1102                    assert Downloads.isStatusError(status);
1103                    return STATUS_FAILED;
1104            }
1105        }
1106    }
1107}
1108