blob: a25aa6f44f1054d6e4f19d1979062c4cee09523f [file] [log] [blame]
Steve Howarda2709362010-07-02 17:12:48 -07001/*
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
Steve Howard31fd85f2010-09-27 16:32:39 -070017package android.app;
Steve Howarda2709362010-07-02 17:12:48 -070018
19import android.content.ContentResolver;
Steve Howardeca77fc2010-09-12 18:49:08 -070020import android.content.ContentUris;
Steve Howarda2709362010-07-02 17:12:48 -070021import android.content.ContentValues;
Steve Howard4f564cd2010-09-22 15:57:25 -070022import android.content.Context;
Steve Howarda2709362010-07-02 17:12:48 -070023import android.database.Cursor;
24import android.database.CursorWrapper;
Steve Howard31fd85f2010-09-27 16:32:39 -070025import android.net.ConnectivityManager;
26import android.net.Uri;
Steve Howard4f564cd2010-09-22 15:57:25 -070027import android.os.Environment;
Steve Howarda2709362010-07-02 17:12:48 -070028import android.os.ParcelFileDescriptor;
Steve Howardf054e192010-09-01 18:26:26 -070029import android.provider.BaseColumns;
Steve Howarda2709362010-07-02 17:12:48 -070030import android.provider.Downloads;
Steve Howard4f564cd2010-09-22 15:57:25 -070031import android.util.Pair;
Steve Howarda2709362010-07-02 17:12:48 -070032
Steve Howard4f564cd2010-09-22 15:57:25 -070033import java.io.File;
Steve Howarda2709362010-07-02 17:12:48 -070034import java.io.FileNotFoundException;
35import java.util.ArrayList;
36import java.util.Arrays;
Steve Howarda2709362010-07-02 17:12:48 -070037import java.util.HashSet;
38import java.util.List;
Steve Howarda2709362010-07-02 17:12:48 -070039import 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}.
Steve Howard610c4352010-09-30 18:30:04 -070050 *
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.
Steve Howarda2709362010-07-02 17:12:48 -070054 */
55public class DownloadManager {
Vasu Norie7be6bd2010-10-10 14:58:08 -070056 private static final String TAG = "DownloadManager";
57
Steve Howarda2709362010-07-02 17:12:48 -070058 /**
59 * An identifier for a particular download, unique across the system. Clients use this ID to
60 * make subsequent calls related to the download.
61 */
Steve Howardf054e192010-09-01 18:26:26 -070062 public final static String COLUMN_ID = BaseColumns._ID;
Steve Howarda2709362010-07-02 17:12:48 -070063
64 /**
Steve Howard8651bd52010-08-03 12:35:32 -070065 * The client-supplied title for this download. This will be displayed in system notifications.
66 * Defaults to the empty string.
Steve Howarda2709362010-07-02 17:12:48 -070067 */
68 public final static String COLUMN_TITLE = "title";
69
70 /**
71 * The client-supplied description of this download. This will be displayed in system
Steve Howard8651bd52010-08-03 12:35:32 -070072 * notifications. Defaults to the empty string.
Steve Howarda2709362010-07-02 17:12:48 -070073 */
74 public final static String COLUMN_DESCRIPTION = "description";
75
76 /**
77 * URI to be downloaded.
78 */
79 public final static String COLUMN_URI = "uri";
80
81 /**
Steve Howard8651bd52010-08-03 12:35:32 -070082 * Internet Media Type of the downloaded file. If no value is provided upon creation, this will
83 * initially be null and will be filled in based on the server's response once the download has
84 * started.
Steve Howarda2709362010-07-02 17:12:48 -070085 *
86 * @see <a href="http://www.ietf.org/rfc/rfc1590.txt">RFC 1590, defining Media Types</a>
87 */
88 public final static String COLUMN_MEDIA_TYPE = "media_type";
89
90 /**
Steve Howard8651bd52010-08-03 12:35:32 -070091 * Total size of the download in bytes. This will initially be -1 and will be filled in once
92 * the download starts.
Steve Howarda2709362010-07-02 17:12:48 -070093 */
94 public final static String COLUMN_TOTAL_SIZE_BYTES = "total_size";
95
96 /**
97 * Uri where downloaded file will be stored. If a destination is supplied by client, that URI
Steve Howard8651bd52010-08-03 12:35:32 -070098 * will be used here. Otherwise, the value will initially be null and will be filled in with a
99 * generated URI once the download has started.
Steve Howarda2709362010-07-02 17:12:48 -0700100 */
101 public final static String COLUMN_LOCAL_URI = "local_uri";
102
103 /**
104 * Current status of the download, as one of the STATUS_* constants.
105 */
106 public final static String COLUMN_STATUS = "status";
107
108 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700109 * Provides more detail on the status of the download. Its meaning depends on the value of
110 * {@link #COLUMN_STATUS}.
Steve Howarda2709362010-07-02 17:12:48 -0700111 *
Steve Howard3e8c1d32010-09-29 17:03:32 -0700112 * When {@link #COLUMN_STATUS} is {@link #STATUS_FAILED}, this indicates the type of error that
113 * occurred. If an HTTP error occurred, this will hold the HTTP status code as defined in RFC
114 * 2616. Otherwise, it will hold one of the ERROR_* constants.
115 *
116 * When {@link #COLUMN_STATUS} is {@link #STATUS_PAUSED}, this indicates why the download is
117 * paused. It will hold one of the PAUSED_* constants.
118 *
119 * If {@link #COLUMN_STATUS} is neither {@link #STATUS_FAILED} nor {@link #STATUS_PAUSED}, this
120 * column's value is undefined.
Steve Howarda2709362010-07-02 17:12:48 -0700121 *
122 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1">RFC 2616
123 * status codes</a>
124 */
Steve Howard3e8c1d32010-09-29 17:03:32 -0700125 public final static String COLUMN_REASON = "reason";
Steve Howarda2709362010-07-02 17:12:48 -0700126
127 /**
128 * Number of bytes download so far.
129 */
130 public final static String COLUMN_BYTES_DOWNLOADED_SO_FAR = "bytes_so_far";
131
132 /**
Steve Howardadcb6972010-07-12 17:09:25 -0700133 * Timestamp when the download was last modified, in {@link System#currentTimeMillis
Steve Howarda2709362010-07-02 17:12:48 -0700134 * System.currentTimeMillis()} (wall clock time in UTC).
135 */
Steve Howardadcb6972010-07-12 17:09:25 -0700136 public final static String COLUMN_LAST_MODIFIED_TIMESTAMP = "last_modified_timestamp";
Steve Howarda2709362010-07-02 17:12:48 -0700137
Vasu Nori216fa222010-10-12 23:08:13 -0700138 /**
139 * The URI to the corresponding entry in MediaProvider for this downloaded entry. It is
140 * used to delete the entries from MediaProvider database when it is deleted from the
141 * downloaded list.
142 */
143 public static final String COLUMN_MEDIAPROVIDER_URI = "mediaprovider_uri";
Steve Howarda2709362010-07-02 17:12:48 -0700144
145 /**
146 * Value of {@link #COLUMN_STATUS} when the download is waiting to start.
147 */
148 public final static int STATUS_PENDING = 1 << 0;
149
150 /**
151 * Value of {@link #COLUMN_STATUS} when the download is currently running.
152 */
153 public final static int STATUS_RUNNING = 1 << 1;
154
155 /**
156 * Value of {@link #COLUMN_STATUS} when the download is waiting to retry or resume.
157 */
158 public final static int STATUS_PAUSED = 1 << 2;
159
160 /**
161 * Value of {@link #COLUMN_STATUS} when the download has successfully completed.
162 */
163 public final static int STATUS_SUCCESSFUL = 1 << 3;
164
165 /**
166 * Value of {@link #COLUMN_STATUS} when the download has failed (and will not be retried).
167 */
168 public final static int STATUS_FAILED = 1 << 4;
169
170
171 /**
172 * Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit
173 * under any other error code.
174 */
175 public final static int ERROR_UNKNOWN = 1000;
176
177 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700178 * Value of {@link #COLUMN_REASON} when a storage issue arises which doesn't fit under any
Steve Howarda2709362010-07-02 17:12:48 -0700179 * other error code. Use the more specific {@link #ERROR_INSUFFICIENT_SPACE} and
180 * {@link #ERROR_DEVICE_NOT_FOUND} when appropriate.
181 */
182 public final static int ERROR_FILE_ERROR = 1001;
183
184 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700185 * Value of {@link #COLUMN_REASON} when an HTTP code was received that download manager
Steve Howarda2709362010-07-02 17:12:48 -0700186 * can't handle.
187 */
188 public final static int ERROR_UNHANDLED_HTTP_CODE = 1002;
189
190 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700191 * Value of {@link #COLUMN_REASON} when an error receiving or processing data occurred at
Steve Howarda2709362010-07-02 17:12:48 -0700192 * the HTTP level.
193 */
194 public final static int ERROR_HTTP_DATA_ERROR = 1004;
195
196 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700197 * Value of {@link #COLUMN_REASON} when there were too many redirects.
Steve Howarda2709362010-07-02 17:12:48 -0700198 */
199 public final static int ERROR_TOO_MANY_REDIRECTS = 1005;
200
201 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700202 * Value of {@link #COLUMN_REASON} when there was insufficient storage space. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700203 * this is because the SD card is full.
204 */
205 public final static int ERROR_INSUFFICIENT_SPACE = 1006;
206
207 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700208 * Value of {@link #COLUMN_REASON} when no external storage device was found. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700209 * this is because the SD card is not mounted.
210 */
211 public final static int ERROR_DEVICE_NOT_FOUND = 1007;
212
Steve Howardb8e07a52010-07-21 14:53:21 -0700213 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700214 * Value of {@link #COLUMN_REASON} when some possibly transient error occurred but we can't
Steve Howard33bbd122010-08-02 17:51:29 -0700215 * resume the download.
216 */
217 public final static int ERROR_CANNOT_RESUME = 1008;
218
219 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700220 * Value of {@link #COLUMN_REASON} when the requested destination file already exists (the
Steve Howarda9e87c92010-09-16 12:02:03 -0700221 * download manager will not overwrite an existing file).
222 */
223 public final static int ERROR_FILE_ALREADY_EXISTS = 1009;
224
225 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700226 * Value of {@link #COLUMN_REASON} when the download is paused because some network error
227 * occurred and the download manager is waiting before retrying the request.
228 */
229 public final static int PAUSED_WAITING_TO_RETRY = 1;
230
231 /**
232 * Value of {@link #COLUMN_REASON} when the download is waiting for network connectivity to
233 * proceed.
234 */
235 public final static int PAUSED_WAITING_FOR_NETWORK = 2;
236
237 /**
238 * Value of {@link #COLUMN_REASON} when the download exceeds a size limit for downloads over
239 * the mobile network and the download manager is waiting for a Wi-Fi connection to proceed.
240 */
241 public final static int PAUSED_QUEUED_FOR_WIFI = 3;
242
243 /**
244 * Value of {@link #COLUMN_REASON} when the download is paused for some other reason.
245 */
246 public final static int PAUSED_UNKNOWN = 4;
247
248 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700249 * Broadcast intent action sent by the download manager when a download completes.
250 */
251 public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE";
252
253 /**
Steve Howard610c4352010-09-30 18:30:04 -0700254 * Broadcast intent action sent by the download manager when the user clicks on a running
255 * download, either from a system notification or from the downloads UI.
Steve Howardb8e07a52010-07-21 14:53:21 -0700256 */
257 public final static String ACTION_NOTIFICATION_CLICKED =
258 "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
259
260 /**
Steve Howarde78fc182010-09-24 14:59:36 -0700261 * Intent action to launch an activity to display all downloads.
262 */
263 public final static String ACTION_VIEW_DOWNLOADS = "android.intent.action.VIEW_DOWNLOADS";
264
265 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700266 * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a
267 * long) of the download that just completed.
268 */
269 public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
Steve Howarda2709362010-07-02 17:12:48 -0700270
271 // this array must contain all public columns
272 private static final String[] COLUMNS = new String[] {
273 COLUMN_ID,
Vasu Nori216fa222010-10-12 23:08:13 -0700274 COLUMN_MEDIAPROVIDER_URI,
Steve Howarda2709362010-07-02 17:12:48 -0700275 COLUMN_TITLE,
276 COLUMN_DESCRIPTION,
277 COLUMN_URI,
278 COLUMN_MEDIA_TYPE,
279 COLUMN_TOTAL_SIZE_BYTES,
280 COLUMN_LOCAL_URI,
281 COLUMN_STATUS,
Steve Howard3e8c1d32010-09-29 17:03:32 -0700282 COLUMN_REASON,
Steve Howarda2709362010-07-02 17:12:48 -0700283 COLUMN_BYTES_DOWNLOADED_SO_FAR,
Steve Howardadcb6972010-07-12 17:09:25 -0700284 COLUMN_LAST_MODIFIED_TIMESTAMP
Steve Howarda2709362010-07-02 17:12:48 -0700285 };
286
287 // columns to request from DownloadProvider
288 private static final String[] UNDERLYING_COLUMNS = new String[] {
289 Downloads.Impl._ID,
Vasu Nori216fa222010-10-12 23:08:13 -0700290 Downloads.Impl.COLUMN_MEDIAPROVIDER_URI,
Steve Howarda2709362010-07-02 17:12:48 -0700291 Downloads.COLUMN_TITLE,
292 Downloads.COLUMN_DESCRIPTION,
293 Downloads.COLUMN_URI,
294 Downloads.COLUMN_MIME_TYPE,
295 Downloads.COLUMN_TOTAL_BYTES,
Steve Howarda2709362010-07-02 17:12:48 -0700296 Downloads.COLUMN_STATUS,
Steve Howardadcb6972010-07-12 17:09:25 -0700297 Downloads.COLUMN_CURRENT_BYTES,
298 Downloads.COLUMN_LAST_MODIFICATION,
Steve Howarda9e87c92010-09-16 12:02:03 -0700299 Downloads.COLUMN_DESTINATION,
300 Downloads.Impl.COLUMN_FILE_NAME_HINT,
Steve Howardbb0d23b2010-09-22 18:56:29 -0700301 Downloads.Impl._DATA,
Steve Howarda2709362010-07-02 17:12:48 -0700302 };
303
304 private static final Set<String> LONG_COLUMNS = new HashSet<String>(
Steve Howard3e8c1d32010-09-29 17:03:32 -0700305 Arrays.asList(COLUMN_ID, COLUMN_TOTAL_SIZE_BYTES, COLUMN_STATUS, COLUMN_REASON,
Steve Howardadcb6972010-07-12 17:09:25 -0700306 COLUMN_BYTES_DOWNLOADED_SO_FAR, COLUMN_LAST_MODIFIED_TIMESTAMP));
Steve Howarda2709362010-07-02 17:12:48 -0700307
308 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700309 * This class contains all the information necessary to request a new download. The URI is the
Steve Howarda2709362010-07-02 17:12:48 -0700310 * only required parameter.
Steve Howard4f564cd2010-09-22 15:57:25 -0700311 *
312 * Note that the default download destination is a shared volume where the system might delete
313 * your file if it needs to reclaim space for system use. If this is a problem, use a location
314 * on external storage (see {@link #setDestinationUri(Uri)}.
Steve Howarda2709362010-07-02 17:12:48 -0700315 */
316 public static class Request {
317 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700318 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
319 * {@link ConnectivityManager#TYPE_MOBILE}.
320 */
321 public static final int NETWORK_MOBILE = 1 << 0;
Steve Howarda2709362010-07-02 17:12:48 -0700322
Steve Howardb8e07a52010-07-21 14:53:21 -0700323 /**
324 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
325 * {@link ConnectivityManager#TYPE_WIFI}.
326 */
327 public static final int NETWORK_WIFI = 1 << 1;
328
Steve Howardb8e07a52010-07-21 14:53:21 -0700329 private Uri mUri;
330 private Uri mDestinationUri;
Steve Howard4f564cd2010-09-22 15:57:25 -0700331 private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>();
332 private CharSequence mTitle;
333 private CharSequence mDescription;
Steve Howard8e15afe2010-07-28 17:12:40 -0700334 private boolean mShowNotification = true;
Steve Howard4f564cd2010-09-22 15:57:25 -0700335 private String mMimeType;
Steve Howardb8e07a52010-07-21 14:53:21 -0700336 private boolean mRoamingAllowed = true;
337 private int mAllowedNetworkTypes = ~0; // default to all network types allowed
Steve Howard90fb15a2010-09-09 16:13:41 -0700338 private boolean mIsVisibleInDownloadsUi = true;
Steve Howarda2709362010-07-02 17:12:48 -0700339
340 /**
341 * @param uri the HTTP URI to download.
342 */
343 public Request(Uri uri) {
344 if (uri == null) {
345 throw new NullPointerException();
346 }
347 String scheme = uri.getScheme();
348 if (scheme == null || !scheme.equals("http")) {
349 throw new IllegalArgumentException("Can only download HTTP URIs: " + uri);
350 }
351 mUri = uri;
352 }
353
354 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700355 * Set the local destination for the downloaded file. Must be a file URI to a path on
Steve Howarda2709362010-07-02 17:12:48 -0700356 * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE
357 * permission.
358 *
Steve Howard4f564cd2010-09-22 15:57:25 -0700359 * By default, downloads are saved to a generated filename in the shared download cache and
360 * may be deleted by the system at any time to reclaim space.
Steve Howarda2709362010-07-02 17:12:48 -0700361 *
362 * @return this object
363 */
364 public Request setDestinationUri(Uri uri) {
365 mDestinationUri = uri;
366 return this;
367 }
368
369 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700370 * Set the local destination for the downloaded file to a path within the application's
371 * external files directory (as returned by {@link Context#getExternalFilesDir(String)}.
372 *
373 * @param context the {@link Context} to use in determining the external files directory
374 * @param dirType the directory type to pass to {@link Context#getExternalFilesDir(String)}
375 * @param subPath the path within the external directory, including the destination filename
376 * @return this object
377 */
378 public Request setDestinationInExternalFilesDir(Context context, String dirType,
379 String subPath) {
380 setDestinationFromBase(context.getExternalFilesDir(dirType), subPath);
381 return this;
382 }
383
384 /**
385 * Set the local destination for the downloaded file to a path within the public external
386 * storage directory (as returned by
387 * {@link Environment#getExternalStoragePublicDirectory(String)}.
388 *
389 * @param dirType the directory type to pass to
390 * {@link Environment#getExternalStoragePublicDirectory(String)}
391 * @param subPath the path within the external directory, including the destination filename
392 * @return this object
393 */
394 public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
395 setDestinationFromBase(Environment.getExternalStoragePublicDirectory(dirType), subPath);
396 return this;
397 }
398
399 private void setDestinationFromBase(File base, String subPath) {
400 if (subPath == null) {
401 throw new NullPointerException("subPath cannot be null");
402 }
403 mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath);
404 }
405
406 /**
407 * Add an HTTP header to be included with the download request. The header will be added to
408 * the end of the list.
Steve Howarda2709362010-07-02 17:12:48 -0700409 * @param header HTTP header name
410 * @param value header value
411 * @return this object
Steve Howard4f564cd2010-09-22 15:57:25 -0700412 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">HTTP/1.1
413 * Message Headers</a>
Steve Howarda2709362010-07-02 17:12:48 -0700414 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700415 public Request addRequestHeader(String header, String value) {
416 if (header == null) {
417 throw new NullPointerException("header cannot be null");
418 }
419 if (header.contains(":")) {
420 throw new IllegalArgumentException("header may not contain ':'");
421 }
422 if (value == null) {
423 value = "";
424 }
425 mRequestHeaders.add(Pair.create(header, value));
Steve Howarda2709362010-07-02 17:12:48 -0700426 return this;
427 }
428
429 /**
Steve Howard610c4352010-09-30 18:30:04 -0700430 * Set the title of this download, to be displayed in notifications (if enabled). If no
431 * title is given, a default one will be assigned based on the download filename, once the
432 * download starts.
Steve Howarda2709362010-07-02 17:12:48 -0700433 * @return this object
434 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700435 public Request setTitle(CharSequence title) {
Steve Howarda2709362010-07-02 17:12:48 -0700436 mTitle = title;
437 return this;
438 }
439
440 /**
441 * Set a description of this download, to be displayed in notifications (if enabled)
442 * @return this object
443 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700444 public Request setDescription(CharSequence description) {
Steve Howarda2709362010-07-02 17:12:48 -0700445 mDescription = description;
446 return this;
447 }
448
449 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700450 * Set the MIME content type of this download. This will override the content type declared
Steve Howarda2709362010-07-02 17:12:48 -0700451 * in the server's response.
Steve Howard4f564cd2010-09-22 15:57:25 -0700452 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1
453 * Media Types</a>
Steve Howarda2709362010-07-02 17:12:48 -0700454 * @return this object
455 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700456 public Request setMimeType(String mimeType) {
457 mMimeType = mimeType;
Steve Howarda2709362010-07-02 17:12:48 -0700458 return this;
459 }
460
461 /**
Steve Howard8e15afe2010-07-28 17:12:40 -0700462 * Control whether a system notification is posted by the download manager while this
463 * download is running. If enabled, the download manager posts notifications about downloads
464 * through the system {@link android.app.NotificationManager}. By default, a notification is
465 * shown.
Steve Howarda2709362010-07-02 17:12:48 -0700466 *
Steve Howard8e15afe2010-07-28 17:12:40 -0700467 * If set to false, this requires the permission
468 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
469 *
470 * @param show whether the download manager should show a notification for this download.
Steve Howarda2709362010-07-02 17:12:48 -0700471 * @return this object
472 */
Steve Howard8e15afe2010-07-28 17:12:40 -0700473 public Request setShowRunningNotification(boolean show) {
474 mShowNotification = show;
Steve Howarda2709362010-07-02 17:12:48 -0700475 return this;
476 }
477
Steve Howardb8e07a52010-07-21 14:53:21 -0700478 /**
479 * Restrict the types of networks over which this download may proceed. By default, all
480 * network types are allowed.
481 * @param flags any combination of the NETWORK_* bit flags.
482 * @return this object
483 */
Steve Howarda2709362010-07-02 17:12:48 -0700484 public Request setAllowedNetworkTypes(int flags) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700485 mAllowedNetworkTypes = flags;
486 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700487 }
488
Steve Howardb8e07a52010-07-21 14:53:21 -0700489 /**
490 * Set whether this download may proceed over a roaming connection. By default, roaming is
491 * allowed.
492 * @param allowed whether to allow a roaming connection to be used
493 * @return this object
494 */
Steve Howarda2709362010-07-02 17:12:48 -0700495 public Request setAllowedOverRoaming(boolean allowed) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700496 mRoamingAllowed = allowed;
497 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700498 }
499
500 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700501 * Set whether this download should be displayed in the system's Downloads UI. True by
502 * default.
503 * @param isVisible whether to display this download in the Downloads UI
504 * @return this object
505 */
506 public Request setVisibleInDownloadsUi(boolean isVisible) {
507 mIsVisibleInDownloadsUi = isVisible;
508 return this;
509 }
510
511 /**
Steve Howarda2709362010-07-02 17:12:48 -0700512 * @return ContentValues to be passed to DownloadProvider.insert()
513 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700514 ContentValues toContentValues(String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700515 ContentValues values = new ContentValues();
516 assert mUri != null;
517 values.put(Downloads.COLUMN_URI, mUri.toString());
Steve Howardb8e07a52010-07-21 14:53:21 -0700518 values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true);
519 values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE, packageName);
Steve Howarda2709362010-07-02 17:12:48 -0700520
521 if (mDestinationUri != null) {
Steve Howardadcb6972010-07-12 17:09:25 -0700522 values.put(Downloads.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI);
523 values.put(Downloads.COLUMN_FILE_NAME_HINT, mDestinationUri.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700524 } else {
525 values.put(Downloads.COLUMN_DESTINATION,
526 Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE);
527 }
528
529 if (!mRequestHeaders.isEmpty()) {
Steve Howardea9147d2010-07-13 19:02:45 -0700530 encodeHttpHeaders(values);
Steve Howarda2709362010-07-02 17:12:48 -0700531 }
532
533 putIfNonNull(values, Downloads.COLUMN_TITLE, mTitle);
534 putIfNonNull(values, Downloads.COLUMN_DESCRIPTION, mDescription);
Steve Howard4f564cd2010-09-22 15:57:25 -0700535 putIfNonNull(values, Downloads.COLUMN_MIME_TYPE, mMimeType);
Steve Howarda2709362010-07-02 17:12:48 -0700536
Steve Howard8e15afe2010-07-28 17:12:40 -0700537 values.put(Downloads.COLUMN_VISIBILITY,
538 mShowNotification ? Downloads.VISIBILITY_VISIBLE
539 : Downloads.VISIBILITY_HIDDEN);
Steve Howarda2709362010-07-02 17:12:48 -0700540
Steve Howardb8e07a52010-07-21 14:53:21 -0700541 values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes);
542 values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed);
Steve Howard90fb15a2010-09-09 16:13:41 -0700543 values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi);
Steve Howardb8e07a52010-07-21 14:53:21 -0700544
Steve Howarda2709362010-07-02 17:12:48 -0700545 return values;
546 }
547
Steve Howardea9147d2010-07-13 19:02:45 -0700548 private void encodeHttpHeaders(ContentValues values) {
549 int index = 0;
Steve Howard4f564cd2010-09-22 15:57:25 -0700550 for (Pair<String, String> header : mRequestHeaders) {
551 String headerString = header.first + ": " + header.second;
Steve Howardea9147d2010-07-13 19:02:45 -0700552 values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString);
553 index++;
554 }
555 }
556
Steve Howard4f564cd2010-09-22 15:57:25 -0700557 private void putIfNonNull(ContentValues contentValues, String key, Object value) {
Steve Howarda2709362010-07-02 17:12:48 -0700558 if (value != null) {
Steve Howard4f564cd2010-09-22 15:57:25 -0700559 contentValues.put(key, value.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700560 }
561 }
562 }
563
564 /**
565 * This class may be used to filter download manager queries.
566 */
567 public static class Query {
Steve Howardf054e192010-09-01 18:26:26 -0700568 /**
569 * Constant for use with {@link #orderBy}
570 * @hide
571 */
572 public static final int ORDER_ASCENDING = 1;
573
574 /**
575 * Constant for use with {@link #orderBy}
576 * @hide
577 */
578 public static final int ORDER_DESCENDING = 2;
579
Steve Howard64c48b82010-10-07 17:53:52 -0700580 private long[] mIds = null;
Steve Howarda2709362010-07-02 17:12:48 -0700581 private Integer mStatusFlags = null;
Steve Howardf054e192010-09-01 18:26:26 -0700582 private String mOrderByColumn = Downloads.COLUMN_LAST_MODIFICATION;
583 private int mOrderDirection = ORDER_DESCENDING;
Steve Howard90fb15a2010-09-09 16:13:41 -0700584 private boolean mOnlyIncludeVisibleInDownloadsUi = false;
Steve Howarda2709362010-07-02 17:12:48 -0700585
586 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700587 * Include only the downloads with the given IDs.
Steve Howarda2709362010-07-02 17:12:48 -0700588 * @return this object
589 */
Steve Howard64c48b82010-10-07 17:53:52 -0700590 public Query setFilterById(long... ids) {
591 mIds = ids;
Steve Howarda2709362010-07-02 17:12:48 -0700592 return this;
593 }
594
595 /**
596 * Include only downloads with status matching any the given status flags.
597 * @param flags any combination of the STATUS_* bit flags
598 * @return this object
599 */
600 public Query setFilterByStatus(int flags) {
601 mStatusFlags = flags;
602 return this;
603 }
604
605 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700606 * Controls whether this query includes downloads not visible in the system's Downloads UI.
607 * @param value if true, this query will only include downloads that should be displayed in
608 * the system's Downloads UI; if false (the default), this query will include
609 * both visible and invisible downloads.
610 * @return this object
611 * @hide
612 */
613 public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) {
614 mOnlyIncludeVisibleInDownloadsUi = value;
615 return this;
616 }
617
618 /**
Steve Howardf054e192010-09-01 18:26:26 -0700619 * Change the sort order of the returned Cursor.
620 *
621 * @param column one of the COLUMN_* constants; currently, only
622 * {@link #COLUMN_LAST_MODIFIED_TIMESTAMP} and {@link #COLUMN_TOTAL_SIZE_BYTES} are
623 * supported.
624 * @param direction either {@link #ORDER_ASCENDING} or {@link #ORDER_DESCENDING}
625 * @return this object
626 * @hide
627 */
628 public Query orderBy(String column, int direction) {
629 if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) {
630 throw new IllegalArgumentException("Invalid direction: " + direction);
631 }
632
633 if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) {
634 mOrderByColumn = Downloads.COLUMN_LAST_MODIFICATION;
635 } else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
636 mOrderByColumn = Downloads.COLUMN_TOTAL_BYTES;
637 } else {
638 throw new IllegalArgumentException("Cannot order by " + column);
639 }
640 mOrderDirection = direction;
641 return this;
642 }
643
644 /**
Steve Howarda2709362010-07-02 17:12:48 -0700645 * Run this query using the given ContentResolver.
646 * @param projection the projection to pass to ContentResolver.query()
647 * @return the Cursor returned by ContentResolver.query()
648 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700649 Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) {
650 Uri uri = baseUri;
Steve Howard90fb15a2010-09-09 16:13:41 -0700651 List<String> selectionParts = new ArrayList<String>();
Steve Howard64c48b82010-10-07 17:53:52 -0700652 String[] selectionArgs = null;
Steve Howarda2709362010-07-02 17:12:48 -0700653
Steve Howard64c48b82010-10-07 17:53:52 -0700654 if (mIds != null) {
655 selectionParts.add(getWhereClauseForIds(mIds));
656 selectionArgs = getWhereArgsForIds(mIds);
Steve Howarda2709362010-07-02 17:12:48 -0700657 }
658
659 if (mStatusFlags != null) {
660 List<String> parts = new ArrayList<String>();
661 if ((mStatusFlags & STATUS_PENDING) != 0) {
662 parts.add(statusClause("=", Downloads.STATUS_PENDING));
663 }
664 if ((mStatusFlags & STATUS_RUNNING) != 0) {
665 parts.add(statusClause("=", Downloads.STATUS_RUNNING));
666 }
667 if ((mStatusFlags & STATUS_PAUSED) != 0) {
Steve Howard3e8c1d32010-09-29 17:03:32 -0700668 parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP));
669 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY));
670 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK));
671 parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI));
Steve Howarda2709362010-07-02 17:12:48 -0700672 }
673 if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) {
674 parts.add(statusClause("=", Downloads.STATUS_SUCCESS));
675 }
676 if ((mStatusFlags & STATUS_FAILED) != 0) {
677 parts.add("(" + statusClause(">=", 400)
678 + " AND " + statusClause("<", 600) + ")");
679 }
Steve Howard90fb15a2010-09-09 16:13:41 -0700680 selectionParts.add(joinStrings(" OR ", parts));
Steve Howarda2709362010-07-02 17:12:48 -0700681 }
Steve Howardf054e192010-09-01 18:26:26 -0700682
Steve Howard90fb15a2010-09-09 16:13:41 -0700683 if (mOnlyIncludeVisibleInDownloadsUi) {
684 selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'");
685 }
686
Vasu Nori216fa222010-10-12 23:08:13 -0700687 // only return rows which are not marked 'deleted = 1'
688 selectionParts.add(Downloads.Impl.COLUMN_DELETED + " != '1'");
689
Steve Howard90fb15a2010-09-09 16:13:41 -0700690 String selection = joinStrings(" AND ", selectionParts);
Steve Howardf054e192010-09-01 18:26:26 -0700691 String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC");
692 String orderBy = mOrderByColumn + " " + orderDirection;
693
Steve Howard64c48b82010-10-07 17:53:52 -0700694 return resolver.query(uri, projection, selection, selectionArgs, orderBy);
Steve Howarda2709362010-07-02 17:12:48 -0700695 }
696
697 private String joinStrings(String joiner, Iterable<String> parts) {
698 StringBuilder builder = new StringBuilder();
699 boolean first = true;
700 for (String part : parts) {
701 if (!first) {
702 builder.append(joiner);
703 }
704 builder.append(part);
705 first = false;
706 }
707 return builder.toString();
708 }
709
710 private String statusClause(String operator, int value) {
711 return Downloads.COLUMN_STATUS + operator + "'" + value + "'";
712 }
713 }
714
715 private ContentResolver mResolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700716 private String mPackageName;
Steve Howardeca77fc2010-09-12 18:49:08 -0700717 private Uri mBaseUri = Downloads.Impl.CONTENT_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700718
719 /**
720 * @hide
721 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700722 public DownloadManager(ContentResolver resolver, String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700723 mResolver = resolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700724 mPackageName = packageName;
Steve Howarda2709362010-07-02 17:12:48 -0700725 }
726
727 /**
Steve Howardeca77fc2010-09-12 18:49:08 -0700728 * Makes this object access the download provider through /all_downloads URIs rather than
729 * /my_downloads URIs, for clients that have permission to do so.
730 * @hide
731 */
732 public void setAccessAllDownloads(boolean accessAllDownloads) {
733 if (accessAllDownloads) {
734 mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI;
735 } else {
736 mBaseUri = Downloads.Impl.CONTENT_URI;
737 }
738 }
739
740 /**
Steve Howarda2709362010-07-02 17:12:48 -0700741 * Enqueue a new download. The download will start automatically once the download manager is
742 * ready to execute it and connectivity is available.
743 *
744 * @param request the parameters specifying this download
745 * @return an ID for the download, unique across the system. This ID is used to make future
746 * calls related to this download.
747 */
748 public long enqueue(Request request) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700749 ContentValues values = request.toContentValues(mPackageName);
Steve Howarda2709362010-07-02 17:12:48 -0700750 Uri downloadUri = mResolver.insert(Downloads.CONTENT_URI, values);
751 long id = Long.parseLong(downloadUri.getLastPathSegment());
752 return id;
753 }
754
755 /**
Vasu Nori216fa222010-10-12 23:08:13 -0700756 * Marks the specified download as 'to be deleted'. This is done when a completed download
757 * is to be removed but the row was stored without enough info to delete the corresponding
758 * metadata from Mediaprovider database. Actual cleanup of this row is done in DownloadService.
759 *
760 * @param ids the IDs of the downloads to be marked 'deleted'
761 * @return the number of downloads actually updated
762 * @hide
763 */
764 public int markRowDeleted(long... ids) {
765 if (ids == null || ids.length == 0) {
766 // called with nothing to remove!
767 throw new IllegalArgumentException("input param 'ids' can't be null");
768 }
769 ContentValues values = new ContentValues();
770 values.put(Downloads.Impl.COLUMN_DELETED, 1);
771 return mResolver.update(mBaseUri, values, getWhereClauseForIds(ids),
772 getWhereArgsForIds(ids));
773 }
774
775 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700776 * Cancel downloads and remove them from the download manager. Each download will be stopped if
Steve Howarda2709362010-07-02 17:12:48 -0700777 * it was running, and it will no longer be accessible through the download manager. If a file
Steve Howard64c48b82010-10-07 17:53:52 -0700778 * was already downloaded to external storage, it will not be deleted.
Steve Howarda2709362010-07-02 17:12:48 -0700779 *
Steve Howard64c48b82010-10-07 17:53:52 -0700780 * @param ids the IDs of the downloads to remove
781 * @return the number of downloads actually removed
Steve Howarda2709362010-07-02 17:12:48 -0700782 */
Steve Howard64c48b82010-10-07 17:53:52 -0700783 public int remove(long... ids) {
Vasu Norie7be6bd2010-10-10 14:58:08 -0700784 if (ids == null || ids.length == 0) {
785 // called with nothing to remove!
786 throw new IllegalArgumentException("input param 'ids' can't be null");
Steve Howarda2709362010-07-02 17:12:48 -0700787 }
Vasu Norie7be6bd2010-10-10 14:58:08 -0700788 return mResolver.delete(mBaseUri, getWhereClauseForIds(ids), getWhereArgsForIds(ids));
Steve Howarda2709362010-07-02 17:12:48 -0700789 }
790
791 /**
792 * Query the download manager about downloads that have been requested.
793 * @param query parameters specifying filters for this query
794 * @return a Cursor over the result set of downloads, with columns consisting of all the
795 * COLUMN_* constants.
796 */
797 public Cursor query(Query query) {
Steve Howardeca77fc2010-09-12 18:49:08 -0700798 Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri);
Steve Howardf054e192010-09-01 18:26:26 -0700799 if (underlyingCursor == null) {
800 return null;
801 }
Steve Howardeca77fc2010-09-12 18:49:08 -0700802 return new CursorTranslator(underlyingCursor, mBaseUri);
Steve Howarda2709362010-07-02 17:12:48 -0700803 }
804
805 /**
806 * Open a downloaded file for reading. The download must have completed.
807 * @param id the ID of the download
808 * @return a read-only {@link ParcelFileDescriptor}
809 * @throws FileNotFoundException if the destination file does not already exist
810 */
811 public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException {
812 return mResolver.openFileDescriptor(getDownloadUri(id), "r");
813 }
814
815 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700816 * Restart the given downloads, which must have already completed (successfully or not). This
Steve Howard90fb15a2010-09-09 16:13:41 -0700817 * method will only work when called from within the download manager's process.
Steve Howard64c48b82010-10-07 17:53:52 -0700818 * @param ids the IDs of the downloads
Steve Howard90fb15a2010-09-09 16:13:41 -0700819 * @hide
820 */
Steve Howard64c48b82010-10-07 17:53:52 -0700821 public void restartDownload(long... ids) {
822 Cursor cursor = query(new Query().setFilterById(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -0700823 try {
Steve Howard64c48b82010-10-07 17:53:52 -0700824 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
825 int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS));
826 if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) {
827 throw new IllegalArgumentException("Cannot restart incomplete download: "
828 + cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
829 }
Steve Howard90fb15a2010-09-09 16:13:41 -0700830 }
831 } finally {
832 cursor.close();
833 }
834
835 ContentValues values = new ContentValues();
836 values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0);
837 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1);
838 values.putNull(Downloads.Impl._DATA);
839 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
Steve Howard64c48b82010-10-07 17:53:52 -0700840 mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -0700841 }
842
843 /**
Steve Howarda2709362010-07-02 17:12:48 -0700844 * Get the DownloadProvider URI for the download with the given ID.
845 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700846 Uri getDownloadUri(long id) {
847 return ContentUris.withAppendedId(mBaseUri, id);
Steve Howarda2709362010-07-02 17:12:48 -0700848 }
849
850 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700851 * Get a parameterized SQL WHERE clause to select a bunch of IDs.
852 */
853 static String getWhereClauseForIds(long[] ids) {
854 StringBuilder whereClause = new StringBuilder();
Vasu Norie7be6bd2010-10-10 14:58:08 -0700855 whereClause.append("(");
Steve Howard64c48b82010-10-07 17:53:52 -0700856 for (int i = 0; i < ids.length; i++) {
857 if (i > 0) {
Vasu Norie7be6bd2010-10-10 14:58:08 -0700858 whereClause.append("OR ");
Steve Howard64c48b82010-10-07 17:53:52 -0700859 }
Vasu Norie7be6bd2010-10-10 14:58:08 -0700860 whereClause.append(Downloads.Impl._ID);
861 whereClause.append(" = ? ");
Steve Howard64c48b82010-10-07 17:53:52 -0700862 }
863 whereClause.append(")");
864 return whereClause.toString();
865 }
866
867 /**
868 * Get the selection args for a clause returned by {@link #getWhereClauseForIds(long[])}.
869 */
870 static String[] getWhereArgsForIds(long[] ids) {
871 String[] whereArgs = new String[ids.length];
872 for (int i = 0; i < ids.length; i++) {
873 whereArgs[i] = Long.toString(ids[i]);
874 }
875 return whereArgs;
876 }
877
878 /**
Steve Howarda2709362010-07-02 17:12:48 -0700879 * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and
880 * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants.
881 * Some columns correspond directly to underlying values while others are computed from
882 * underlying data.
883 */
884 private static class CursorTranslator extends CursorWrapper {
Steve Howardeca77fc2010-09-12 18:49:08 -0700885 private Uri mBaseUri;
886
887 public CursorTranslator(Cursor cursor, Uri baseUri) {
Steve Howarda2709362010-07-02 17:12:48 -0700888 super(cursor);
Steve Howardeca77fc2010-09-12 18:49:08 -0700889 mBaseUri = baseUri;
Steve Howarda2709362010-07-02 17:12:48 -0700890 }
891
892 @Override
893 public int getColumnIndex(String columnName) {
894 return Arrays.asList(COLUMNS).indexOf(columnName);
895 }
896
897 @Override
898 public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {
899 int index = getColumnIndex(columnName);
900 if (index == -1) {
Steve Howardf054e192010-09-01 18:26:26 -0700901 throw new IllegalArgumentException("No such column: " + columnName);
Steve Howarda2709362010-07-02 17:12:48 -0700902 }
903 return index;
904 }
905
906 @Override
907 public String getColumnName(int columnIndex) {
908 int numColumns = COLUMNS.length;
909 if (columnIndex < 0 || columnIndex >= numColumns) {
910 throw new IllegalArgumentException("Invalid column index " + columnIndex + ", "
911 + numColumns + " columns exist");
912 }
913 return COLUMNS[columnIndex];
914 }
915
916 @Override
917 public String[] getColumnNames() {
918 String[] returnColumns = new String[COLUMNS.length];
919 System.arraycopy(COLUMNS, 0, returnColumns, 0, COLUMNS.length);
920 return returnColumns;
921 }
922
923 @Override
924 public int getColumnCount() {
925 return COLUMNS.length;
926 }
927
928 @Override
929 public byte[] getBlob(int columnIndex) {
930 throw new UnsupportedOperationException();
931 }
932
933 @Override
934 public double getDouble(int columnIndex) {
935 return getLong(columnIndex);
936 }
937
938 private boolean isLongColumn(String column) {
939 return LONG_COLUMNS.contains(column);
940 }
941
942 @Override
943 public float getFloat(int columnIndex) {
944 return (float) getDouble(columnIndex);
945 }
946
947 @Override
948 public int getInt(int columnIndex) {
949 return (int) getLong(columnIndex);
950 }
951
952 @Override
953 public long getLong(int columnIndex) {
954 return translateLong(getColumnName(columnIndex));
955 }
956
957 @Override
958 public short getShort(int columnIndex) {
959 return (short) getLong(columnIndex);
960 }
961
962 @Override
963 public String getString(int columnIndex) {
964 return translateString(getColumnName(columnIndex));
965 }
966
967 private String translateString(String column) {
968 if (isLongColumn(column)) {
969 return Long.toString(translateLong(column));
970 }
971 if (column.equals(COLUMN_TITLE)) {
972 return getUnderlyingString(Downloads.COLUMN_TITLE);
973 }
974 if (column.equals(COLUMN_DESCRIPTION)) {
975 return getUnderlyingString(Downloads.COLUMN_DESCRIPTION);
976 }
977 if (column.equals(COLUMN_URI)) {
978 return getUnderlyingString(Downloads.COLUMN_URI);
979 }
980 if (column.equals(COLUMN_MEDIA_TYPE)) {
981 return getUnderlyingString(Downloads.COLUMN_MIME_TYPE);
982 }
Vasu Nori216fa222010-10-12 23:08:13 -0700983 if (column.equals(COLUMN_MEDIAPROVIDER_URI)) {
984 return getUnderlyingString(Downloads.Impl.COLUMN_MEDIAPROVIDER_URI);
985 }
Steve Howard8651bd52010-08-03 12:35:32 -0700986
Steve Howarda2709362010-07-02 17:12:48 -0700987 assert column.equals(COLUMN_LOCAL_URI);
Steve Howardeca77fc2010-09-12 18:49:08 -0700988 return getLocalUri();
989 }
990
991 private String getLocalUri() {
Steve Howardeca77fc2010-09-12 18:49:08 -0700992 long destinationType = getUnderlyingLong(Downloads.Impl.COLUMN_DESTINATION);
993 if (destinationType == Downloads.Impl.DESTINATION_FILE_URI) {
Steve Howarda9e87c92010-09-16 12:02:03 -0700994 // return client-provided file URI for external download
995 return getUnderlyingString(Downloads.Impl.COLUMN_FILE_NAME_HINT);
Steve Howardeca77fc2010-09-12 18:49:08 -0700996 }
997
Steve Howardbb0d23b2010-09-22 18:56:29 -0700998 if (destinationType == Downloads.Impl.DESTINATION_EXTERNAL) {
999 // return stored destination for legacy external download
Steve Howard99047d72010-09-29 17:41:37 -07001000 String localPath = getUnderlyingString(Downloads.Impl._DATA);
1001 if (localPath == null) {
1002 return null;
1003 }
1004 return Uri.fromFile(new File(localPath)).toString();
Steve Howardbb0d23b2010-09-22 18:56:29 -07001005 }
1006
Steve Howardeca77fc2010-09-12 18:49:08 -07001007 // return content URI for cache download
1008 long downloadId = getUnderlyingLong(Downloads.Impl._ID);
1009 return ContentUris.withAppendedId(mBaseUri, downloadId).toString();
Steve Howarda2709362010-07-02 17:12:48 -07001010 }
1011
1012 private long translateLong(String column) {
1013 if (!isLongColumn(column)) {
1014 // mimic behavior of underlying cursor -- most likely, throw NumberFormatException
1015 return Long.valueOf(translateString(column));
1016 }
1017
1018 if (column.equals(COLUMN_ID)) {
1019 return getUnderlyingLong(Downloads.Impl._ID);
1020 }
1021 if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
1022 return getUnderlyingLong(Downloads.COLUMN_TOTAL_BYTES);
1023 }
1024 if (column.equals(COLUMN_STATUS)) {
1025 return translateStatus((int) getUnderlyingLong(Downloads.COLUMN_STATUS));
1026 }
Steve Howard3e8c1d32010-09-29 17:03:32 -07001027 if (column.equals(COLUMN_REASON)) {
1028 return getReason((int) getUnderlyingLong(Downloads.COLUMN_STATUS));
Steve Howarda2709362010-07-02 17:12:48 -07001029 }
1030 if (column.equals(COLUMN_BYTES_DOWNLOADED_SO_FAR)) {
1031 return getUnderlyingLong(Downloads.COLUMN_CURRENT_BYTES);
1032 }
Steve Howardadcb6972010-07-12 17:09:25 -07001033 assert column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP);
1034 return getUnderlyingLong(Downloads.COLUMN_LAST_MODIFICATION);
Steve Howarda2709362010-07-02 17:12:48 -07001035 }
1036
Steve Howard3e8c1d32010-09-29 17:03:32 -07001037 private long getReason(int status) {
1038 switch (translateStatus(status)) {
1039 case STATUS_FAILED:
1040 return getErrorCode(status);
1041
1042 case STATUS_PAUSED:
1043 return getPausedReason(status);
1044
1045 default:
1046 return 0; // arbitrary value when status is not an error
Steve Howarda2709362010-07-02 17:12:48 -07001047 }
Steve Howard3e8c1d32010-09-29 17:03:32 -07001048 }
1049
1050 private long getPausedReason(int status) {
1051 switch (status) {
1052 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1053 return PAUSED_WAITING_TO_RETRY;
1054
1055 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1056 return PAUSED_WAITING_FOR_NETWORK;
1057
1058 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
1059 return PAUSED_QUEUED_FOR_WIFI;
1060
1061 default:
1062 return PAUSED_UNKNOWN;
1063 }
1064 }
1065
1066 private long getErrorCode(int status) {
Steve Howard33bbd122010-08-02 17:51:29 -07001067 if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS)
1068 || (500 <= status && status < 600)) {
Steve Howarda2709362010-07-02 17:12:48 -07001069 // HTTP status code
1070 return status;
1071 }
1072
1073 switch (status) {
1074 case Downloads.STATUS_FILE_ERROR:
1075 return ERROR_FILE_ERROR;
1076
1077 case Downloads.STATUS_UNHANDLED_HTTP_CODE:
1078 case Downloads.STATUS_UNHANDLED_REDIRECT:
1079 return ERROR_UNHANDLED_HTTP_CODE;
1080
1081 case Downloads.STATUS_HTTP_DATA_ERROR:
1082 return ERROR_HTTP_DATA_ERROR;
1083
1084 case Downloads.STATUS_TOO_MANY_REDIRECTS:
1085 return ERROR_TOO_MANY_REDIRECTS;
1086
1087 case Downloads.STATUS_INSUFFICIENT_SPACE_ERROR:
1088 return ERROR_INSUFFICIENT_SPACE;
1089
1090 case Downloads.STATUS_DEVICE_NOT_FOUND_ERROR:
1091 return ERROR_DEVICE_NOT_FOUND;
1092
Steve Howard33bbd122010-08-02 17:51:29 -07001093 case Downloads.Impl.STATUS_CANNOT_RESUME:
1094 return ERROR_CANNOT_RESUME;
1095
Steve Howarda9e87c92010-09-16 12:02:03 -07001096 case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR:
1097 return ERROR_FILE_ALREADY_EXISTS;
1098
Steve Howarda2709362010-07-02 17:12:48 -07001099 default:
1100 return ERROR_UNKNOWN;
1101 }
1102 }
1103
1104 private long getUnderlyingLong(String column) {
1105 return super.getLong(super.getColumnIndex(column));
1106 }
1107
1108 private String getUnderlyingString(String column) {
1109 return super.getString(super.getColumnIndex(column));
1110 }
1111
Steve Howard3e8c1d32010-09-29 17:03:32 -07001112 private int translateStatus(int status) {
Steve Howarda2709362010-07-02 17:12:48 -07001113 switch (status) {
1114 case Downloads.STATUS_PENDING:
1115 return STATUS_PENDING;
1116
1117 case Downloads.STATUS_RUNNING:
1118 return STATUS_RUNNING;
1119
Steve Howard3e8c1d32010-09-29 17:03:32 -07001120 case Downloads.Impl.STATUS_PAUSED_BY_APP:
1121 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1122 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1123 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
Steve Howarda2709362010-07-02 17:12:48 -07001124 return STATUS_PAUSED;
1125
1126 case Downloads.STATUS_SUCCESS:
1127 return STATUS_SUCCESSFUL;
1128
1129 default:
1130 assert Downloads.isStatusError(status);
1131 return STATUS_FAILED;
1132 }
1133 }
1134 }
1135}