blob: f0a8dfd7557c88e9e322cf2c67757acf93a7ca57 [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 Howardd58429f2010-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 Howardd58429f2010-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 {
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 */
Steve Howardf054e192010-09-01 18:26:26 -070060 public final static String COLUMN_ID = BaseColumns._ID;
Steve Howarda2709362010-07-02 17:12:48 -070061
62 /**
Steve Howard8651bd52010-08-03 12:35:32 -070063 * The client-supplied title for this download. This will be displayed in system notifications.
64 * Defaults to the empty string.
Steve Howarda2709362010-07-02 17:12:48 -070065 */
66 public final static String COLUMN_TITLE = "title";
67
68 /**
69 * The client-supplied description of this download. This will be displayed in system
Steve Howard8651bd52010-08-03 12:35:32 -070070 * notifications. Defaults to the empty string.
Steve Howarda2709362010-07-02 17:12:48 -070071 */
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 /**
Steve Howard8651bd52010-08-03 12:35:32 -070080 * 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.
Steve Howarda2709362010-07-02 17:12:48 -070083 *
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 /**
Steve Howard8651bd52010-08-03 12:35:32 -070089 * Total size of the download in bytes. This will initially be -1 and will be filled in once
90 * the download starts.
Steve Howarda2709362010-07-02 17:12:48 -070091 */
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
Steve Howard8651bd52010-08-03 12:35:32 -070096 * 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.
Steve Howarda2709362010-07-02 17:12:48 -070098 */
99 public final static String COLUMN_LOCAL_URI = "local_uri";
100
101 /**
Doug Zongkeree04af32010-10-08 13:42:16 -0700102 * The pathname of the file where the download is stored.
103 */
104 public final static String COLUMN_LOCAL_FILENAME = "local_filename";
105
106 /**
Steve Howarda2709362010-07-02 17:12:48 -0700107 * Current status of the download, as one of the STATUS_* constants.
108 */
109 public final static String COLUMN_STATUS = "status";
110
111 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700112 * Provides more detail on the status of the download. Its meaning depends on the value of
113 * {@link #COLUMN_STATUS}.
Steve Howarda2709362010-07-02 17:12:48 -0700114 *
Steve Howard3e8c1d32010-09-29 17:03:32 -0700115 * When {@link #COLUMN_STATUS} is {@link #STATUS_FAILED}, this indicates the type of error that
116 * occurred. If an HTTP error occurred, this will hold the HTTP status code as defined in RFC
117 * 2616. Otherwise, it will hold one of the ERROR_* constants.
118 *
119 * When {@link #COLUMN_STATUS} is {@link #STATUS_PAUSED}, this indicates why the download is
120 * paused. It will hold one of the PAUSED_* constants.
121 *
122 * If {@link #COLUMN_STATUS} is neither {@link #STATUS_FAILED} nor {@link #STATUS_PAUSED}, this
123 * column's value is undefined.
Steve Howarda2709362010-07-02 17:12:48 -0700124 *
125 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1">RFC 2616
126 * status codes</a>
127 */
Steve Howard3e8c1d32010-09-29 17:03:32 -0700128 public final static String COLUMN_REASON = "reason";
Steve Howarda2709362010-07-02 17:12:48 -0700129
130 /**
131 * Number of bytes download so far.
132 */
133 public final static String COLUMN_BYTES_DOWNLOADED_SO_FAR = "bytes_so_far";
134
135 /**
Steve Howardadcb6972010-07-12 17:09:25 -0700136 * Timestamp when the download was last modified, in {@link System#currentTimeMillis
Steve Howarda2709362010-07-02 17:12:48 -0700137 * System.currentTimeMillis()} (wall clock time in UTC).
138 */
Steve Howardadcb6972010-07-12 17:09:25 -0700139 public final static String COLUMN_LAST_MODIFIED_TIMESTAMP = "last_modified_timestamp";
Steve Howarda2709362010-07-02 17:12:48 -0700140
141
142 /**
143 * Value of {@link #COLUMN_STATUS} when the download is waiting to start.
144 */
145 public final static int STATUS_PENDING = 1 << 0;
146
147 /**
148 * Value of {@link #COLUMN_STATUS} when the download is currently running.
149 */
150 public final static int STATUS_RUNNING = 1 << 1;
151
152 /**
153 * Value of {@link #COLUMN_STATUS} when the download is waiting to retry or resume.
154 */
155 public final static int STATUS_PAUSED = 1 << 2;
156
157 /**
158 * Value of {@link #COLUMN_STATUS} when the download has successfully completed.
159 */
160 public final static int STATUS_SUCCESSFUL = 1 << 3;
161
162 /**
163 * Value of {@link #COLUMN_STATUS} when the download has failed (and will not be retried).
164 */
165 public final static int STATUS_FAILED = 1 << 4;
166
167
168 /**
169 * Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit
170 * under any other error code.
171 */
172 public final static int ERROR_UNKNOWN = 1000;
173
174 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700175 * Value of {@link #COLUMN_REASON} when a storage issue arises which doesn't fit under any
Steve Howarda2709362010-07-02 17:12:48 -0700176 * other error code. Use the more specific {@link #ERROR_INSUFFICIENT_SPACE} and
177 * {@link #ERROR_DEVICE_NOT_FOUND} when appropriate.
178 */
179 public final static int ERROR_FILE_ERROR = 1001;
180
181 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700182 * Value of {@link #COLUMN_REASON} when an HTTP code was received that download manager
Steve Howarda2709362010-07-02 17:12:48 -0700183 * can't handle.
184 */
185 public final static int ERROR_UNHANDLED_HTTP_CODE = 1002;
186
187 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700188 * Value of {@link #COLUMN_REASON} when an error receiving or processing data occurred at
Steve Howarda2709362010-07-02 17:12:48 -0700189 * the HTTP level.
190 */
191 public final static int ERROR_HTTP_DATA_ERROR = 1004;
192
193 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700194 * Value of {@link #COLUMN_REASON} when there were too many redirects.
Steve Howarda2709362010-07-02 17:12:48 -0700195 */
196 public final static int ERROR_TOO_MANY_REDIRECTS = 1005;
197
198 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700199 * Value of {@link #COLUMN_REASON} when there was insufficient storage space. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700200 * this is because the SD card is full.
201 */
202 public final static int ERROR_INSUFFICIENT_SPACE = 1006;
203
204 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700205 * Value of {@link #COLUMN_REASON} when no external storage device was found. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700206 * this is because the SD card is not mounted.
207 */
208 public final static int ERROR_DEVICE_NOT_FOUND = 1007;
209
Steve Howardb8e07a52010-07-21 14:53:21 -0700210 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700211 * Value of {@link #COLUMN_REASON} when some possibly transient error occurred but we can't
Steve Howard33bbd122010-08-02 17:51:29 -0700212 * resume the download.
213 */
214 public final static int ERROR_CANNOT_RESUME = 1008;
215
216 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700217 * Value of {@link #COLUMN_REASON} when the requested destination file already exists (the
Steve Howarda9e87c92010-09-16 12:02:03 -0700218 * download manager will not overwrite an existing file).
219 */
220 public final static int ERROR_FILE_ALREADY_EXISTS = 1009;
221
222 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700223 * Value of {@link #COLUMN_REASON} when the download is paused because some network error
224 * occurred and the download manager is waiting before retrying the request.
225 */
226 public final static int PAUSED_WAITING_TO_RETRY = 1;
227
228 /**
229 * Value of {@link #COLUMN_REASON} when the download is waiting for network connectivity to
230 * proceed.
231 */
232 public final static int PAUSED_WAITING_FOR_NETWORK = 2;
233
234 /**
235 * Value of {@link #COLUMN_REASON} when the download exceeds a size limit for downloads over
236 * the mobile network and the download manager is waiting for a Wi-Fi connection to proceed.
237 */
238 public final static int PAUSED_QUEUED_FOR_WIFI = 3;
239
240 /**
241 * Value of {@link #COLUMN_REASON} when the download is paused for some other reason.
242 */
243 public final static int PAUSED_UNKNOWN = 4;
244
245 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700246 * Broadcast intent action sent by the download manager when a download completes.
247 */
248 public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE";
249
250 /**
Steve Howard610c4352010-09-30 18:30:04 -0700251 * Broadcast intent action sent by the download manager when the user clicks on a running
252 * download, either from a system notification or from the downloads UI.
Steve Howardb8e07a52010-07-21 14:53:21 -0700253 */
254 public final static String ACTION_NOTIFICATION_CLICKED =
255 "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
256
257 /**
Steve Howarde78fc182010-09-24 14:59:36 -0700258 * Intent action to launch an activity to display all downloads.
259 */
260 public final static String ACTION_VIEW_DOWNLOADS = "android.intent.action.VIEW_DOWNLOADS";
261
262 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700263 * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a
264 * long) of the download that just completed.
265 */
266 public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
Steve Howarda2709362010-07-02 17:12:48 -0700267
268 // this array must contain all public columns
269 private static final String[] COLUMNS = new String[] {
270 COLUMN_ID,
271 COLUMN_TITLE,
272 COLUMN_DESCRIPTION,
273 COLUMN_URI,
274 COLUMN_MEDIA_TYPE,
275 COLUMN_TOTAL_SIZE_BYTES,
276 COLUMN_LOCAL_URI,
277 COLUMN_STATUS,
Steve Howard3e8c1d32010-09-29 17:03:32 -0700278 COLUMN_REASON,
Steve Howarda2709362010-07-02 17:12:48 -0700279 COLUMN_BYTES_DOWNLOADED_SO_FAR,
Doug Zongkeree04af32010-10-08 13:42:16 -0700280 COLUMN_LAST_MODIFIED_TIMESTAMP,
281 COLUMN_LOCAL_FILENAME
Steve Howarda2709362010-07-02 17:12:48 -0700282 };
283
284 // columns to request from DownloadProvider
285 private static final String[] UNDERLYING_COLUMNS = new String[] {
286 Downloads.Impl._ID,
287 Downloads.COLUMN_TITLE,
288 Downloads.COLUMN_DESCRIPTION,
289 Downloads.COLUMN_URI,
290 Downloads.COLUMN_MIME_TYPE,
291 Downloads.COLUMN_TOTAL_BYTES,
Steve Howarda2709362010-07-02 17:12:48 -0700292 Downloads.COLUMN_STATUS,
Steve Howardadcb6972010-07-12 17:09:25 -0700293 Downloads.COLUMN_CURRENT_BYTES,
294 Downloads.COLUMN_LAST_MODIFICATION,
Steve Howarda9e87c92010-09-16 12:02:03 -0700295 Downloads.COLUMN_DESTINATION,
296 Downloads.Impl.COLUMN_FILE_NAME_HINT,
Steve Howardbb0d23b2010-09-22 18:56:29 -0700297 Downloads.Impl._DATA,
Steve Howarda2709362010-07-02 17:12:48 -0700298 };
299
300 private static final Set<String> LONG_COLUMNS = new HashSet<String>(
Steve Howard3e8c1d32010-09-29 17:03:32 -0700301 Arrays.asList(COLUMN_ID, COLUMN_TOTAL_SIZE_BYTES, COLUMN_STATUS, COLUMN_REASON,
Steve Howardadcb6972010-07-12 17:09:25 -0700302 COLUMN_BYTES_DOWNLOADED_SO_FAR, COLUMN_LAST_MODIFIED_TIMESTAMP));
Steve Howarda2709362010-07-02 17:12:48 -0700303
304 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700305 * This class contains all the information necessary to request a new download. The URI is the
Steve Howarda2709362010-07-02 17:12:48 -0700306 * only required parameter.
Steve Howard4f564cd2010-09-22 15:57:25 -0700307 *
308 * Note that the default download destination is a shared volume where the system might delete
309 * your file if it needs to reclaim space for system use. If this is a problem, use a location
310 * on external storage (see {@link #setDestinationUri(Uri)}.
Steve Howarda2709362010-07-02 17:12:48 -0700311 */
312 public static class Request {
313 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700314 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
315 * {@link ConnectivityManager#TYPE_MOBILE}.
316 */
317 public static final int NETWORK_MOBILE = 1 << 0;
Steve Howarda2709362010-07-02 17:12:48 -0700318
Steve Howardb8e07a52010-07-21 14:53:21 -0700319 /**
320 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
321 * {@link ConnectivityManager#TYPE_WIFI}.
322 */
323 public static final int NETWORK_WIFI = 1 << 1;
324
Steve Howardb8e07a52010-07-21 14:53:21 -0700325 private Uri mUri;
326 private Uri mDestinationUri;
Steve Howard4f564cd2010-09-22 15:57:25 -0700327 private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>();
328 private CharSequence mTitle;
329 private CharSequence mDescription;
Steve Howard8e15afe2010-07-28 17:12:40 -0700330 private boolean mShowNotification = true;
Steve Howard4f564cd2010-09-22 15:57:25 -0700331 private String mMimeType;
Steve Howardb8e07a52010-07-21 14:53:21 -0700332 private boolean mRoamingAllowed = true;
333 private int mAllowedNetworkTypes = ~0; // default to all network types allowed
Steve Howard90fb15a2010-09-09 16:13:41 -0700334 private boolean mIsVisibleInDownloadsUi = true;
Steve Howarda2709362010-07-02 17:12:48 -0700335
336 /**
337 * @param uri the HTTP URI to download.
338 */
339 public Request(Uri uri) {
340 if (uri == null) {
341 throw new NullPointerException();
342 }
343 String scheme = uri.getScheme();
Paul Westbrook86a60192010-09-15 12:55:49 -0700344 if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) {
345 throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + uri);
Steve Howarda2709362010-07-02 17:12:48 -0700346 }
347 mUri = uri;
348 }
349
350 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700351 * Set the local destination for the downloaded file. Must be a file URI to a path on
Steve Howarda2709362010-07-02 17:12:48 -0700352 * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE
353 * permission.
354 *
Steve Howard4f564cd2010-09-22 15:57:25 -0700355 * By default, downloads are saved to a generated filename in the shared download cache and
356 * may be deleted by the system at any time to reclaim space.
Steve Howarda2709362010-07-02 17:12:48 -0700357 *
358 * @return this object
359 */
360 public Request setDestinationUri(Uri uri) {
361 mDestinationUri = uri;
362 return this;
363 }
364
365 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700366 * Set the local destination for the downloaded file to a path within the application's
367 * external files directory (as returned by {@link Context#getExternalFilesDir(String)}.
368 *
369 * @param context the {@link Context} to use in determining the external files directory
370 * @param dirType the directory type to pass to {@link Context#getExternalFilesDir(String)}
371 * @param subPath the path within the external directory, including the destination filename
372 * @return this object
373 */
374 public Request setDestinationInExternalFilesDir(Context context, String dirType,
375 String subPath) {
376 setDestinationFromBase(context.getExternalFilesDir(dirType), subPath);
377 return this;
378 }
379
380 /**
381 * Set the local destination for the downloaded file to a path within the public external
382 * storage directory (as returned by
383 * {@link Environment#getExternalStoragePublicDirectory(String)}.
384 *
385 * @param dirType the directory type to pass to
386 * {@link Environment#getExternalStoragePublicDirectory(String)}
387 * @param subPath the path within the external directory, including the destination filename
388 * @return this object
389 */
390 public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
391 setDestinationFromBase(Environment.getExternalStoragePublicDirectory(dirType), subPath);
392 return this;
393 }
394
395 private void setDestinationFromBase(File base, String subPath) {
396 if (subPath == null) {
397 throw new NullPointerException("subPath cannot be null");
398 }
399 mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath);
400 }
401
402 /**
403 * Add an HTTP header to be included with the download request. The header will be added to
404 * the end of the list.
Steve Howarda2709362010-07-02 17:12:48 -0700405 * @param header HTTP header name
406 * @param value header value
407 * @return this object
Steve Howard4f564cd2010-09-22 15:57:25 -0700408 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">HTTP/1.1
409 * Message Headers</a>
Steve Howarda2709362010-07-02 17:12:48 -0700410 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700411 public Request addRequestHeader(String header, String value) {
412 if (header == null) {
413 throw new NullPointerException("header cannot be null");
414 }
415 if (header.contains(":")) {
416 throw new IllegalArgumentException("header may not contain ':'");
417 }
418 if (value == null) {
419 value = "";
420 }
421 mRequestHeaders.add(Pair.create(header, value));
Steve Howarda2709362010-07-02 17:12:48 -0700422 return this;
423 }
424
425 /**
Steve Howard610c4352010-09-30 18:30:04 -0700426 * Set the title of this download, to be displayed in notifications (if enabled). If no
427 * title is given, a default one will be assigned based on the download filename, once the
428 * download starts.
Steve Howarda2709362010-07-02 17:12:48 -0700429 * @return this object
430 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700431 public Request setTitle(CharSequence title) {
Steve Howarda2709362010-07-02 17:12:48 -0700432 mTitle = title;
433 return this;
434 }
435
436 /**
437 * Set a description of this download, to be displayed in notifications (if enabled)
438 * @return this object
439 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700440 public Request setDescription(CharSequence description) {
Steve Howarda2709362010-07-02 17:12:48 -0700441 mDescription = description;
442 return this;
443 }
444
445 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700446 * Set the MIME content type of this download. This will override the content type declared
Steve Howarda2709362010-07-02 17:12:48 -0700447 * in the server's response.
Steve Howard4f564cd2010-09-22 15:57:25 -0700448 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1
449 * Media Types</a>
Steve Howarda2709362010-07-02 17:12:48 -0700450 * @return this object
451 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700452 public Request setMimeType(String mimeType) {
453 mMimeType = mimeType;
Steve Howarda2709362010-07-02 17:12:48 -0700454 return this;
455 }
456
457 /**
Steve Howard8e15afe2010-07-28 17:12:40 -0700458 * Control whether a system notification is posted by the download manager while this
459 * download is running. If enabled, the download manager posts notifications about downloads
460 * through the system {@link android.app.NotificationManager}. By default, a notification is
461 * shown.
Steve Howarda2709362010-07-02 17:12:48 -0700462 *
Steve Howard8e15afe2010-07-28 17:12:40 -0700463 * If set to false, this requires the permission
464 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
465 *
466 * @param show whether the download manager should show a notification for this download.
Steve Howarda2709362010-07-02 17:12:48 -0700467 * @return this object
468 */
Steve Howard8e15afe2010-07-28 17:12:40 -0700469 public Request setShowRunningNotification(boolean show) {
470 mShowNotification = show;
Steve Howarda2709362010-07-02 17:12:48 -0700471 return this;
472 }
473
Steve Howardb8e07a52010-07-21 14:53:21 -0700474 /**
475 * Restrict the types of networks over which this download may proceed. By default, all
476 * network types are allowed.
477 * @param flags any combination of the NETWORK_* bit flags.
478 * @return this object
479 */
Steve Howarda2709362010-07-02 17:12:48 -0700480 public Request setAllowedNetworkTypes(int flags) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700481 mAllowedNetworkTypes = flags;
482 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700483 }
484
Steve Howardb8e07a52010-07-21 14:53:21 -0700485 /**
486 * Set whether this download may proceed over a roaming connection. By default, roaming is
487 * allowed.
488 * @param allowed whether to allow a roaming connection to be used
489 * @return this object
490 */
Steve Howarda2709362010-07-02 17:12:48 -0700491 public Request setAllowedOverRoaming(boolean allowed) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700492 mRoamingAllowed = allowed;
493 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700494 }
495
496 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700497 * Set whether this download should be displayed in the system's Downloads UI. True by
498 * default.
499 * @param isVisible whether to display this download in the Downloads UI
500 * @return this object
501 */
502 public Request setVisibleInDownloadsUi(boolean isVisible) {
503 mIsVisibleInDownloadsUi = isVisible;
504 return this;
505 }
506
507 /**
Steve Howarda2709362010-07-02 17:12:48 -0700508 * @return ContentValues to be passed to DownloadProvider.insert()
509 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700510 ContentValues toContentValues(String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700511 ContentValues values = new ContentValues();
512 assert mUri != null;
513 values.put(Downloads.COLUMN_URI, mUri.toString());
Steve Howardb8e07a52010-07-21 14:53:21 -0700514 values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true);
515 values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE, packageName);
Steve Howarda2709362010-07-02 17:12:48 -0700516
517 if (mDestinationUri != null) {
Steve Howardadcb6972010-07-12 17:09:25 -0700518 values.put(Downloads.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI);
519 values.put(Downloads.COLUMN_FILE_NAME_HINT, mDestinationUri.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700520 } else {
521 values.put(Downloads.COLUMN_DESTINATION,
522 Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE);
523 }
524
525 if (!mRequestHeaders.isEmpty()) {
Steve Howardea9147d2010-07-13 19:02:45 -0700526 encodeHttpHeaders(values);
Steve Howarda2709362010-07-02 17:12:48 -0700527 }
528
529 putIfNonNull(values, Downloads.COLUMN_TITLE, mTitle);
530 putIfNonNull(values, Downloads.COLUMN_DESCRIPTION, mDescription);
Steve Howard4f564cd2010-09-22 15:57:25 -0700531 putIfNonNull(values, Downloads.COLUMN_MIME_TYPE, mMimeType);
Steve Howarda2709362010-07-02 17:12:48 -0700532
Steve Howard8e15afe2010-07-28 17:12:40 -0700533 values.put(Downloads.COLUMN_VISIBILITY,
534 mShowNotification ? Downloads.VISIBILITY_VISIBLE
535 : Downloads.VISIBILITY_HIDDEN);
Steve Howarda2709362010-07-02 17:12:48 -0700536
Steve Howardb8e07a52010-07-21 14:53:21 -0700537 values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes);
538 values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed);
Steve Howard90fb15a2010-09-09 16:13:41 -0700539 values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi);
Steve Howardb8e07a52010-07-21 14:53:21 -0700540
Steve Howarda2709362010-07-02 17:12:48 -0700541 return values;
542 }
543
Steve Howardea9147d2010-07-13 19:02:45 -0700544 private void encodeHttpHeaders(ContentValues values) {
545 int index = 0;
Steve Howard4f564cd2010-09-22 15:57:25 -0700546 for (Pair<String, String> header : mRequestHeaders) {
547 String headerString = header.first + ": " + header.second;
Steve Howardea9147d2010-07-13 19:02:45 -0700548 values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString);
549 index++;
550 }
551 }
552
Steve Howard4f564cd2010-09-22 15:57:25 -0700553 private void putIfNonNull(ContentValues contentValues, String key, Object value) {
Steve Howarda2709362010-07-02 17:12:48 -0700554 if (value != null) {
Steve Howard4f564cd2010-09-22 15:57:25 -0700555 contentValues.put(key, value.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700556 }
557 }
558 }
559
560 /**
561 * This class may be used to filter download manager queries.
562 */
563 public static class Query {
Steve Howardf054e192010-09-01 18:26:26 -0700564 /**
565 * Constant for use with {@link #orderBy}
566 * @hide
567 */
568 public static final int ORDER_ASCENDING = 1;
569
570 /**
571 * Constant for use with {@link #orderBy}
572 * @hide
573 */
574 public static final int ORDER_DESCENDING = 2;
575
576 private Long mId = null;
Steve Howarda2709362010-07-02 17:12:48 -0700577 private Integer mStatusFlags = null;
Steve Howardf054e192010-09-01 18:26:26 -0700578 private String mOrderByColumn = Downloads.COLUMN_LAST_MODIFICATION;
579 private int mOrderDirection = ORDER_DESCENDING;
Steve Howard90fb15a2010-09-09 16:13:41 -0700580 private boolean mOnlyIncludeVisibleInDownloadsUi = false;
Steve Howarda2709362010-07-02 17:12:48 -0700581
582 /**
583 * Include only the download with the given ID.
584 * @return this object
585 */
586 public Query setFilterById(long id) {
587 mId = id;
588 return this;
589 }
590
591 /**
592 * Include only downloads with status matching any the given status flags.
593 * @param flags any combination of the STATUS_* bit flags
594 * @return this object
595 */
596 public Query setFilterByStatus(int flags) {
597 mStatusFlags = flags;
598 return this;
599 }
600
601 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700602 * Controls whether this query includes downloads not visible in the system's Downloads UI.
603 * @param value if true, this query will only include downloads that should be displayed in
604 * the system's Downloads UI; if false (the default), this query will include
605 * both visible and invisible downloads.
606 * @return this object
607 * @hide
608 */
609 public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) {
610 mOnlyIncludeVisibleInDownloadsUi = value;
611 return this;
612 }
613
614 /**
Steve Howardf054e192010-09-01 18:26:26 -0700615 * Change the sort order of the returned Cursor.
616 *
617 * @param column one of the COLUMN_* constants; currently, only
618 * {@link #COLUMN_LAST_MODIFIED_TIMESTAMP} and {@link #COLUMN_TOTAL_SIZE_BYTES} are
619 * supported.
620 * @param direction either {@link #ORDER_ASCENDING} or {@link #ORDER_DESCENDING}
621 * @return this object
622 * @hide
623 */
624 public Query orderBy(String column, int direction) {
625 if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) {
626 throw new IllegalArgumentException("Invalid direction: " + direction);
627 }
628
629 if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) {
630 mOrderByColumn = Downloads.COLUMN_LAST_MODIFICATION;
631 } else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
632 mOrderByColumn = Downloads.COLUMN_TOTAL_BYTES;
633 } else {
634 throw new IllegalArgumentException("Cannot order by " + column);
635 }
636 mOrderDirection = direction;
637 return this;
638 }
639
640 /**
Steve Howarda2709362010-07-02 17:12:48 -0700641 * Run this query using the given ContentResolver.
642 * @param projection the projection to pass to ContentResolver.query()
643 * @return the Cursor returned by ContentResolver.query()
644 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700645 Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) {
646 Uri uri = baseUri;
Steve Howard90fb15a2010-09-09 16:13:41 -0700647 List<String> selectionParts = new ArrayList<String>();
Steve Howarda2709362010-07-02 17:12:48 -0700648
649 if (mId != null) {
Steve Howardeca77fc2010-09-12 18:49:08 -0700650 uri = ContentUris.withAppendedId(uri, mId);
Steve Howarda2709362010-07-02 17:12:48 -0700651 }
652
653 if (mStatusFlags != null) {
654 List<String> parts = new ArrayList<String>();
655 if ((mStatusFlags & STATUS_PENDING) != 0) {
656 parts.add(statusClause("=", Downloads.STATUS_PENDING));
657 }
658 if ((mStatusFlags & STATUS_RUNNING) != 0) {
659 parts.add(statusClause("=", Downloads.STATUS_RUNNING));
660 }
661 if ((mStatusFlags & STATUS_PAUSED) != 0) {
Steve Howard3e8c1d32010-09-29 17:03:32 -0700662 parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP));
663 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY));
664 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK));
665 parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI));
Steve Howarda2709362010-07-02 17:12:48 -0700666 }
667 if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) {
668 parts.add(statusClause("=", Downloads.STATUS_SUCCESS));
669 }
670 if ((mStatusFlags & STATUS_FAILED) != 0) {
671 parts.add("(" + statusClause(">=", 400)
672 + " AND " + statusClause("<", 600) + ")");
673 }
Steve Howard90fb15a2010-09-09 16:13:41 -0700674 selectionParts.add(joinStrings(" OR ", parts));
Steve Howarda2709362010-07-02 17:12:48 -0700675 }
Steve Howardf054e192010-09-01 18:26:26 -0700676
Steve Howard90fb15a2010-09-09 16:13:41 -0700677 if (mOnlyIncludeVisibleInDownloadsUi) {
678 selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'");
679 }
680
681 String selection = joinStrings(" AND ", selectionParts);
Steve Howardf054e192010-09-01 18:26:26 -0700682 String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC");
683 String orderBy = mOrderByColumn + " " + orderDirection;
684
Steve Howardadcb6972010-07-12 17:09:25 -0700685 return resolver.query(uri, projection, selection, null, orderBy);
Steve Howarda2709362010-07-02 17:12:48 -0700686 }
687
688 private String joinStrings(String joiner, Iterable<String> parts) {
689 StringBuilder builder = new StringBuilder();
690 boolean first = true;
691 for (String part : parts) {
692 if (!first) {
693 builder.append(joiner);
694 }
695 builder.append(part);
696 first = false;
697 }
698 return builder.toString();
699 }
700
701 private String statusClause(String operator, int value) {
702 return Downloads.COLUMN_STATUS + operator + "'" + value + "'";
703 }
704 }
705
706 private ContentResolver mResolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700707 private String mPackageName;
Steve Howardeca77fc2010-09-12 18:49:08 -0700708 private Uri mBaseUri = Downloads.Impl.CONTENT_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700709
710 /**
711 * @hide
712 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700713 public DownloadManager(ContentResolver resolver, String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700714 mResolver = resolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700715 mPackageName = packageName;
Steve Howarda2709362010-07-02 17:12:48 -0700716 }
717
718 /**
Steve Howardeca77fc2010-09-12 18:49:08 -0700719 * Makes this object access the download provider through /all_downloads URIs rather than
720 * /my_downloads URIs, for clients that have permission to do so.
721 * @hide
722 */
723 public void setAccessAllDownloads(boolean accessAllDownloads) {
724 if (accessAllDownloads) {
725 mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI;
726 } else {
727 mBaseUri = Downloads.Impl.CONTENT_URI;
728 }
729 }
730
731 /**
Steve Howarda2709362010-07-02 17:12:48 -0700732 * Enqueue a new download. The download will start automatically once the download manager is
733 * ready to execute it and connectivity is available.
734 *
735 * @param request the parameters specifying this download
736 * @return an ID for the download, unique across the system. This ID is used to make future
737 * calls related to this download.
738 */
739 public long enqueue(Request request) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700740 ContentValues values = request.toContentValues(mPackageName);
Steve Howarda2709362010-07-02 17:12:48 -0700741 Uri downloadUri = mResolver.insert(Downloads.CONTENT_URI, values);
742 long id = Long.parseLong(downloadUri.getLastPathSegment());
743 return id;
744 }
745
746 /**
747 * Cancel a download and remove it from the download manager. The download will be stopped if
748 * it was running, and it will no longer be accessible through the download manager. If a file
749 * was already downloaded, it will not be deleted.
750 *
751 * @param id the ID of the download
752 */
753 public void remove(long id) {
754 int numDeleted = mResolver.delete(getDownloadUri(id), null, null);
755 if (numDeleted == 0) {
756 throw new IllegalArgumentException("Download " + id + " does not exist");
757 }
758 }
759
760 /**
761 * Query the download manager about downloads that have been requested.
762 * @param query parameters specifying filters for this query
763 * @return a Cursor over the result set of downloads, with columns consisting of all the
764 * COLUMN_* constants.
765 */
766 public Cursor query(Query query) {
Steve Howardeca77fc2010-09-12 18:49:08 -0700767 Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri);
Steve Howardf054e192010-09-01 18:26:26 -0700768 if (underlyingCursor == null) {
769 return null;
770 }
Steve Howardeca77fc2010-09-12 18:49:08 -0700771 return new CursorTranslator(underlyingCursor, mBaseUri);
Steve Howarda2709362010-07-02 17:12:48 -0700772 }
773
774 /**
775 * Open a downloaded file for reading. The download must have completed.
776 * @param id the ID of the download
777 * @return a read-only {@link ParcelFileDescriptor}
778 * @throws FileNotFoundException if the destination file does not already exist
779 */
780 public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException {
781 return mResolver.openFileDescriptor(getDownloadUri(id), "r");
782 }
783
784 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700785 * Restart the given download, which must have already completed (successfully or not). This
786 * method will only work when called from within the download manager's process.
787 * @param id the ID of the download
788 * @hide
789 */
790 public void restartDownload(long id) {
791 Cursor cursor = query(new Query().setFilterById(id));
792 try {
793 if (!cursor.moveToFirst()) {
794 throw new IllegalArgumentException("No download with id " + id);
795 }
796 int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS));
797 if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) {
798 throw new IllegalArgumentException("Cannot restart incomplete download: " + id);
799 }
800 } finally {
801 cursor.close();
802 }
803
804 ContentValues values = new ContentValues();
805 values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0);
806 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1);
807 values.putNull(Downloads.Impl._DATA);
808 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
809 mResolver.update(getDownloadUri(id), values, null, null);
810 }
811
812 /**
Steve Howarda2709362010-07-02 17:12:48 -0700813 * Get the DownloadProvider URI for the download with the given ID.
814 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700815 Uri getDownloadUri(long id) {
816 return ContentUris.withAppendedId(mBaseUri, id);
Steve Howarda2709362010-07-02 17:12:48 -0700817 }
818
819 /**
820 * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and
821 * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants.
822 * Some columns correspond directly to underlying values while others are computed from
823 * underlying data.
824 */
825 private static class CursorTranslator extends CursorWrapper {
Steve Howardeca77fc2010-09-12 18:49:08 -0700826 private Uri mBaseUri;
827
828 public CursorTranslator(Cursor cursor, Uri baseUri) {
Steve Howarda2709362010-07-02 17:12:48 -0700829 super(cursor);
Steve Howardeca77fc2010-09-12 18:49:08 -0700830 mBaseUri = baseUri;
Steve Howarda2709362010-07-02 17:12:48 -0700831 }
832
833 @Override
834 public int getColumnIndex(String columnName) {
835 return Arrays.asList(COLUMNS).indexOf(columnName);
836 }
837
838 @Override
839 public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {
840 int index = getColumnIndex(columnName);
841 if (index == -1) {
Steve Howardf054e192010-09-01 18:26:26 -0700842 throw new IllegalArgumentException("No such column: " + columnName);
Steve Howarda2709362010-07-02 17:12:48 -0700843 }
844 return index;
845 }
846
847 @Override
848 public String getColumnName(int columnIndex) {
849 int numColumns = COLUMNS.length;
850 if (columnIndex < 0 || columnIndex >= numColumns) {
851 throw new IllegalArgumentException("Invalid column index " + columnIndex + ", "
852 + numColumns + " columns exist");
853 }
854 return COLUMNS[columnIndex];
855 }
856
857 @Override
858 public String[] getColumnNames() {
859 String[] returnColumns = new String[COLUMNS.length];
860 System.arraycopy(COLUMNS, 0, returnColumns, 0, COLUMNS.length);
861 return returnColumns;
862 }
863
864 @Override
865 public int getColumnCount() {
866 return COLUMNS.length;
867 }
868
869 @Override
870 public byte[] getBlob(int columnIndex) {
871 throw new UnsupportedOperationException();
872 }
873
874 @Override
875 public double getDouble(int columnIndex) {
876 return getLong(columnIndex);
877 }
878
879 private boolean isLongColumn(String column) {
880 return LONG_COLUMNS.contains(column);
881 }
882
883 @Override
884 public float getFloat(int columnIndex) {
885 return (float) getDouble(columnIndex);
886 }
887
888 @Override
889 public int getInt(int columnIndex) {
890 return (int) getLong(columnIndex);
891 }
892
893 @Override
894 public long getLong(int columnIndex) {
895 return translateLong(getColumnName(columnIndex));
896 }
897
898 @Override
899 public short getShort(int columnIndex) {
900 return (short) getLong(columnIndex);
901 }
902
903 @Override
904 public String getString(int columnIndex) {
905 return translateString(getColumnName(columnIndex));
906 }
907
908 private String translateString(String column) {
909 if (isLongColumn(column)) {
910 return Long.toString(translateLong(column));
911 }
912 if (column.equals(COLUMN_TITLE)) {
913 return getUnderlyingString(Downloads.COLUMN_TITLE);
914 }
915 if (column.equals(COLUMN_DESCRIPTION)) {
916 return getUnderlyingString(Downloads.COLUMN_DESCRIPTION);
917 }
918 if (column.equals(COLUMN_URI)) {
919 return getUnderlyingString(Downloads.COLUMN_URI);
920 }
921 if (column.equals(COLUMN_MEDIA_TYPE)) {
922 return getUnderlyingString(Downloads.COLUMN_MIME_TYPE);
923 }
Doug Zongkeree04af32010-10-08 13:42:16 -0700924 if (column.equals(COLUMN_LOCAL_FILENAME)) {
925 return getUnderlyingString(Downloads.Impl._DATA);
926 }
Steve Howard8651bd52010-08-03 12:35:32 -0700927
Steve Howarda2709362010-07-02 17:12:48 -0700928 assert column.equals(COLUMN_LOCAL_URI);
Steve Howardeca77fc2010-09-12 18:49:08 -0700929 return getLocalUri();
930 }
931
932 private String getLocalUri() {
Steve Howardeca77fc2010-09-12 18:49:08 -0700933 long destinationType = getUnderlyingLong(Downloads.Impl.COLUMN_DESTINATION);
934 if (destinationType == Downloads.Impl.DESTINATION_FILE_URI) {
Steve Howarda9e87c92010-09-16 12:02:03 -0700935 // return client-provided file URI for external download
936 return getUnderlyingString(Downloads.Impl.COLUMN_FILE_NAME_HINT);
Steve Howardeca77fc2010-09-12 18:49:08 -0700937 }
938
Steve Howardbb0d23b2010-09-22 18:56:29 -0700939 if (destinationType == Downloads.Impl.DESTINATION_EXTERNAL) {
940 // return stored destination for legacy external download
Steve Howard99047d72010-09-29 17:41:37 -0700941 String localPath = getUnderlyingString(Downloads.Impl._DATA);
942 if (localPath == null) {
943 return null;
944 }
945 return Uri.fromFile(new File(localPath)).toString();
Steve Howardbb0d23b2010-09-22 18:56:29 -0700946 }
947
Steve Howardeca77fc2010-09-12 18:49:08 -0700948 // return content URI for cache download
949 long downloadId = getUnderlyingLong(Downloads.Impl._ID);
950 return ContentUris.withAppendedId(mBaseUri, downloadId).toString();
Steve Howarda2709362010-07-02 17:12:48 -0700951 }
952
953 private long translateLong(String column) {
954 if (!isLongColumn(column)) {
955 // mimic behavior of underlying cursor -- most likely, throw NumberFormatException
956 return Long.valueOf(translateString(column));
957 }
958
959 if (column.equals(COLUMN_ID)) {
960 return getUnderlyingLong(Downloads.Impl._ID);
961 }
962 if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
963 return getUnderlyingLong(Downloads.COLUMN_TOTAL_BYTES);
964 }
965 if (column.equals(COLUMN_STATUS)) {
966 return translateStatus((int) getUnderlyingLong(Downloads.COLUMN_STATUS));
967 }
Steve Howard3e8c1d32010-09-29 17:03:32 -0700968 if (column.equals(COLUMN_REASON)) {
969 return getReason((int) getUnderlyingLong(Downloads.COLUMN_STATUS));
Steve Howarda2709362010-07-02 17:12:48 -0700970 }
971 if (column.equals(COLUMN_BYTES_DOWNLOADED_SO_FAR)) {
972 return getUnderlyingLong(Downloads.COLUMN_CURRENT_BYTES);
973 }
Steve Howardadcb6972010-07-12 17:09:25 -0700974 assert column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP);
975 return getUnderlyingLong(Downloads.COLUMN_LAST_MODIFICATION);
Steve Howarda2709362010-07-02 17:12:48 -0700976 }
977
Steve Howard3e8c1d32010-09-29 17:03:32 -0700978 private long getReason(int status) {
979 switch (translateStatus(status)) {
980 case STATUS_FAILED:
981 return getErrorCode(status);
982
983 case STATUS_PAUSED:
984 return getPausedReason(status);
985
986 default:
987 return 0; // arbitrary value when status is not an error
Steve Howarda2709362010-07-02 17:12:48 -0700988 }
Steve Howard3e8c1d32010-09-29 17:03:32 -0700989 }
990
991 private long getPausedReason(int status) {
992 switch (status) {
993 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
994 return PAUSED_WAITING_TO_RETRY;
995
996 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
997 return PAUSED_WAITING_FOR_NETWORK;
998
999 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
1000 return PAUSED_QUEUED_FOR_WIFI;
1001
1002 default:
1003 return PAUSED_UNKNOWN;
1004 }
1005 }
1006
1007 private long getErrorCode(int status) {
Steve Howard33bbd122010-08-02 17:51:29 -07001008 if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS)
1009 || (500 <= status && status < 600)) {
Steve Howarda2709362010-07-02 17:12:48 -07001010 // HTTP status code
1011 return status;
1012 }
1013
1014 switch (status) {
1015 case Downloads.STATUS_FILE_ERROR:
1016 return ERROR_FILE_ERROR;
1017
1018 case Downloads.STATUS_UNHANDLED_HTTP_CODE:
1019 case Downloads.STATUS_UNHANDLED_REDIRECT:
1020 return ERROR_UNHANDLED_HTTP_CODE;
1021
1022 case Downloads.STATUS_HTTP_DATA_ERROR:
1023 return ERROR_HTTP_DATA_ERROR;
1024
1025 case Downloads.STATUS_TOO_MANY_REDIRECTS:
1026 return ERROR_TOO_MANY_REDIRECTS;
1027
1028 case Downloads.STATUS_INSUFFICIENT_SPACE_ERROR:
1029 return ERROR_INSUFFICIENT_SPACE;
1030
1031 case Downloads.STATUS_DEVICE_NOT_FOUND_ERROR:
1032 return ERROR_DEVICE_NOT_FOUND;
1033
Steve Howard33bbd122010-08-02 17:51:29 -07001034 case Downloads.Impl.STATUS_CANNOT_RESUME:
1035 return ERROR_CANNOT_RESUME;
1036
Steve Howarda9e87c92010-09-16 12:02:03 -07001037 case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR:
1038 return ERROR_FILE_ALREADY_EXISTS;
1039
Steve Howarda2709362010-07-02 17:12:48 -07001040 default:
1041 return ERROR_UNKNOWN;
1042 }
1043 }
1044
1045 private long getUnderlyingLong(String column) {
1046 return super.getLong(super.getColumnIndex(column));
1047 }
1048
1049 private String getUnderlyingString(String column) {
1050 return super.getString(super.getColumnIndex(column));
1051 }
1052
Steve Howard3e8c1d32010-09-29 17:03:32 -07001053 private int translateStatus(int status) {
Steve Howarda2709362010-07-02 17:12:48 -07001054 switch (status) {
1055 case Downloads.STATUS_PENDING:
1056 return STATUS_PENDING;
1057
1058 case Downloads.STATUS_RUNNING:
1059 return STATUS_RUNNING;
1060
Steve Howard3e8c1d32010-09-29 17:03:32 -07001061 case Downloads.Impl.STATUS_PAUSED_BY_APP:
1062 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1063 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1064 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
Steve Howarda2709362010-07-02 17:12:48 -07001065 return STATUS_PAUSED;
1066
1067 case Downloads.STATUS_SUCCESS:
1068 return STATUS_SUCCESSFUL;
1069
1070 default:
1071 assert Downloads.isStatusError(status);
1072 return STATUS_FAILED;
1073 }
1074 }
1075 }
1076}