blob: c476b8fcaf0b4ddb3a1f885a4e5b38efc065a2f4 [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 {
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 /**
102 * Current status of the download, as one of the STATUS_* constants.
103 */
104 public final static String COLUMN_STATUS = "status";
105
106 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700107 * Provides more detail on the status of the download. Its meaning depends on the value of
108 * {@link #COLUMN_STATUS}.
Steve Howarda2709362010-07-02 17:12:48 -0700109 *
Steve Howard3e8c1d32010-09-29 17:03:32 -0700110 * When {@link #COLUMN_STATUS} is {@link #STATUS_FAILED}, this indicates the type of error that
111 * occurred. If an HTTP error occurred, this will hold the HTTP status code as defined in RFC
112 * 2616. Otherwise, it will hold one of the ERROR_* constants.
113 *
114 * When {@link #COLUMN_STATUS} is {@link #STATUS_PAUSED}, this indicates why the download is
115 * paused. It will hold one of the PAUSED_* constants.
116 *
117 * If {@link #COLUMN_STATUS} is neither {@link #STATUS_FAILED} nor {@link #STATUS_PAUSED}, this
118 * column's value is undefined.
Steve Howarda2709362010-07-02 17:12:48 -0700119 *
120 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1">RFC 2616
121 * status codes</a>
122 */
Steve Howard3e8c1d32010-09-29 17:03:32 -0700123 public final static String COLUMN_REASON = "reason";
Steve Howarda2709362010-07-02 17:12:48 -0700124
125 /**
126 * Number of bytes download so far.
127 */
128 public final static String COLUMN_BYTES_DOWNLOADED_SO_FAR = "bytes_so_far";
129
130 /**
Steve Howardadcb6972010-07-12 17:09:25 -0700131 * Timestamp when the download was last modified, in {@link System#currentTimeMillis
Steve Howarda2709362010-07-02 17:12:48 -0700132 * System.currentTimeMillis()} (wall clock time in UTC).
133 */
Steve Howardadcb6972010-07-12 17:09:25 -0700134 public final static String COLUMN_LAST_MODIFIED_TIMESTAMP = "last_modified_timestamp";
Steve Howarda2709362010-07-02 17:12:48 -0700135
136
137 /**
138 * Value of {@link #COLUMN_STATUS} when the download is waiting to start.
139 */
140 public final static int STATUS_PENDING = 1 << 0;
141
142 /**
143 * Value of {@link #COLUMN_STATUS} when the download is currently running.
144 */
145 public final static int STATUS_RUNNING = 1 << 1;
146
147 /**
148 * Value of {@link #COLUMN_STATUS} when the download is waiting to retry or resume.
149 */
150 public final static int STATUS_PAUSED = 1 << 2;
151
152 /**
153 * Value of {@link #COLUMN_STATUS} when the download has successfully completed.
154 */
155 public final static int STATUS_SUCCESSFUL = 1 << 3;
156
157 /**
158 * Value of {@link #COLUMN_STATUS} when the download has failed (and will not be retried).
159 */
160 public final static int STATUS_FAILED = 1 << 4;
161
162
163 /**
164 * Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit
165 * under any other error code.
166 */
167 public final static int ERROR_UNKNOWN = 1000;
168
169 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700170 * Value of {@link #COLUMN_REASON} when a storage issue arises which doesn't fit under any
Steve Howarda2709362010-07-02 17:12:48 -0700171 * other error code. Use the more specific {@link #ERROR_INSUFFICIENT_SPACE} and
172 * {@link #ERROR_DEVICE_NOT_FOUND} when appropriate.
173 */
174 public final static int ERROR_FILE_ERROR = 1001;
175
176 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700177 * Value of {@link #COLUMN_REASON} when an HTTP code was received that download manager
Steve Howarda2709362010-07-02 17:12:48 -0700178 * can't handle.
179 */
180 public final static int ERROR_UNHANDLED_HTTP_CODE = 1002;
181
182 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700183 * Value of {@link #COLUMN_REASON} when an error receiving or processing data occurred at
Steve Howarda2709362010-07-02 17:12:48 -0700184 * the HTTP level.
185 */
186 public final static int ERROR_HTTP_DATA_ERROR = 1004;
187
188 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700189 * Value of {@link #COLUMN_REASON} when there were too many redirects.
Steve Howarda2709362010-07-02 17:12:48 -0700190 */
191 public final static int ERROR_TOO_MANY_REDIRECTS = 1005;
192
193 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700194 * Value of {@link #COLUMN_REASON} when there was insufficient storage space. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700195 * this is because the SD card is full.
196 */
197 public final static int ERROR_INSUFFICIENT_SPACE = 1006;
198
199 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700200 * Value of {@link #COLUMN_REASON} when no external storage device was found. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700201 * this is because the SD card is not mounted.
202 */
203 public final static int ERROR_DEVICE_NOT_FOUND = 1007;
204
Steve Howardb8e07a52010-07-21 14:53:21 -0700205 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700206 * Value of {@link #COLUMN_REASON} when some possibly transient error occurred but we can't
Steve Howard33bbd122010-08-02 17:51:29 -0700207 * resume the download.
208 */
209 public final static int ERROR_CANNOT_RESUME = 1008;
210
211 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700212 * Value of {@link #COLUMN_REASON} when the requested destination file already exists (the
Steve Howarda9e87c92010-09-16 12:02:03 -0700213 * download manager will not overwrite an existing file).
214 */
215 public final static int ERROR_FILE_ALREADY_EXISTS = 1009;
216
217 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700218 * Value of {@link #COLUMN_REASON} when the download is paused because some network error
219 * occurred and the download manager is waiting before retrying the request.
220 */
221 public final static int PAUSED_WAITING_TO_RETRY = 1;
222
223 /**
224 * Value of {@link #COLUMN_REASON} when the download is waiting for network connectivity to
225 * proceed.
226 */
227 public final static int PAUSED_WAITING_FOR_NETWORK = 2;
228
229 /**
230 * Value of {@link #COLUMN_REASON} when the download exceeds a size limit for downloads over
231 * the mobile network and the download manager is waiting for a Wi-Fi connection to proceed.
232 */
233 public final static int PAUSED_QUEUED_FOR_WIFI = 3;
234
235 /**
236 * Value of {@link #COLUMN_REASON} when the download is paused for some other reason.
237 */
238 public final static int PAUSED_UNKNOWN = 4;
239
240 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700241 * Broadcast intent action sent by the download manager when a download completes.
242 */
243 public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE";
244
245 /**
Steve Howard610c4352010-09-30 18:30:04 -0700246 * Broadcast intent action sent by the download manager when the user clicks on a running
247 * download, either from a system notification or from the downloads UI.
Steve Howardb8e07a52010-07-21 14:53:21 -0700248 */
249 public final static String ACTION_NOTIFICATION_CLICKED =
250 "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
251
252 /**
Steve Howarde78fc182010-09-24 14:59:36 -0700253 * Intent action to launch an activity to display all downloads.
254 */
255 public final static String ACTION_VIEW_DOWNLOADS = "android.intent.action.VIEW_DOWNLOADS";
256
257 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700258 * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a
259 * long) of the download that just completed.
260 */
261 public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
Steve Howarda2709362010-07-02 17:12:48 -0700262
263 // this array must contain all public columns
264 private static final String[] COLUMNS = new String[] {
265 COLUMN_ID,
266 COLUMN_TITLE,
267 COLUMN_DESCRIPTION,
268 COLUMN_URI,
269 COLUMN_MEDIA_TYPE,
270 COLUMN_TOTAL_SIZE_BYTES,
271 COLUMN_LOCAL_URI,
272 COLUMN_STATUS,
Steve Howard3e8c1d32010-09-29 17:03:32 -0700273 COLUMN_REASON,
Steve Howarda2709362010-07-02 17:12:48 -0700274 COLUMN_BYTES_DOWNLOADED_SO_FAR,
Steve Howardadcb6972010-07-12 17:09:25 -0700275 COLUMN_LAST_MODIFIED_TIMESTAMP
Steve Howarda2709362010-07-02 17:12:48 -0700276 };
277
278 // columns to request from DownloadProvider
279 private static final String[] UNDERLYING_COLUMNS = new String[] {
280 Downloads.Impl._ID,
281 Downloads.COLUMN_TITLE,
282 Downloads.COLUMN_DESCRIPTION,
283 Downloads.COLUMN_URI,
284 Downloads.COLUMN_MIME_TYPE,
285 Downloads.COLUMN_TOTAL_BYTES,
Steve Howarda2709362010-07-02 17:12:48 -0700286 Downloads.COLUMN_STATUS,
Steve Howardadcb6972010-07-12 17:09:25 -0700287 Downloads.COLUMN_CURRENT_BYTES,
288 Downloads.COLUMN_LAST_MODIFICATION,
Steve Howarda9e87c92010-09-16 12:02:03 -0700289 Downloads.COLUMN_DESTINATION,
290 Downloads.Impl.COLUMN_FILE_NAME_HINT,
Steve Howardbb0d23b2010-09-22 18:56:29 -0700291 Downloads.Impl._DATA,
Steve Howarda2709362010-07-02 17:12:48 -0700292 };
293
294 private static final Set<String> LONG_COLUMNS = new HashSet<String>(
Steve Howard3e8c1d32010-09-29 17:03:32 -0700295 Arrays.asList(COLUMN_ID, COLUMN_TOTAL_SIZE_BYTES, COLUMN_STATUS, COLUMN_REASON,
Steve Howardadcb6972010-07-12 17:09:25 -0700296 COLUMN_BYTES_DOWNLOADED_SO_FAR, COLUMN_LAST_MODIFIED_TIMESTAMP));
Steve Howarda2709362010-07-02 17:12:48 -0700297
298 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700299 * This class contains all the information necessary to request a new download. The URI is the
Steve Howarda2709362010-07-02 17:12:48 -0700300 * only required parameter.
Steve Howard4f564cd2010-09-22 15:57:25 -0700301 *
302 * Note that the default download destination is a shared volume where the system might delete
303 * your file if it needs to reclaim space for system use. If this is a problem, use a location
304 * on external storage (see {@link #setDestinationUri(Uri)}.
Steve Howarda2709362010-07-02 17:12:48 -0700305 */
306 public static class Request {
307 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700308 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
309 * {@link ConnectivityManager#TYPE_MOBILE}.
310 */
311 public static final int NETWORK_MOBILE = 1 << 0;
Steve Howarda2709362010-07-02 17:12:48 -0700312
Steve Howardb8e07a52010-07-21 14:53:21 -0700313 /**
314 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
315 * {@link ConnectivityManager#TYPE_WIFI}.
316 */
317 public static final int NETWORK_WIFI = 1 << 1;
318
Steve Howardb8e07a52010-07-21 14:53:21 -0700319 private Uri mUri;
320 private Uri mDestinationUri;
Steve Howard4f564cd2010-09-22 15:57:25 -0700321 private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>();
322 private CharSequence mTitle;
323 private CharSequence mDescription;
Steve Howard8e15afe2010-07-28 17:12:40 -0700324 private boolean mShowNotification = true;
Steve Howard4f564cd2010-09-22 15:57:25 -0700325 private String mMimeType;
Steve Howardb8e07a52010-07-21 14:53:21 -0700326 private boolean mRoamingAllowed = true;
327 private int mAllowedNetworkTypes = ~0; // default to all network types allowed
Steve Howard90fb15a2010-09-09 16:13:41 -0700328 private boolean mIsVisibleInDownloadsUi = true;
Steve Howarda2709362010-07-02 17:12:48 -0700329
330 /**
331 * @param uri the HTTP URI to download.
332 */
333 public Request(Uri uri) {
334 if (uri == null) {
335 throw new NullPointerException();
336 }
337 String scheme = uri.getScheme();
338 if (scheme == null || !scheme.equals("http")) {
339 throw new IllegalArgumentException("Can only download HTTP URIs: " + uri);
340 }
341 mUri = uri;
342 }
343
344 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700345 * Set the local destination for the downloaded file. Must be a file URI to a path on
Steve Howarda2709362010-07-02 17:12:48 -0700346 * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE
347 * permission.
348 *
Steve Howard4f564cd2010-09-22 15:57:25 -0700349 * By default, downloads are saved to a generated filename in the shared download cache and
350 * may be deleted by the system at any time to reclaim space.
Steve Howarda2709362010-07-02 17:12:48 -0700351 *
352 * @return this object
353 */
354 public Request setDestinationUri(Uri uri) {
355 mDestinationUri = uri;
356 return this;
357 }
358
359 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700360 * Set the local destination for the downloaded file to a path within the application's
361 * external files directory (as returned by {@link Context#getExternalFilesDir(String)}.
362 *
363 * @param context the {@link Context} to use in determining the external files directory
364 * @param dirType the directory type to pass to {@link Context#getExternalFilesDir(String)}
365 * @param subPath the path within the external directory, including the destination filename
366 * @return this object
367 */
368 public Request setDestinationInExternalFilesDir(Context context, String dirType,
369 String subPath) {
370 setDestinationFromBase(context.getExternalFilesDir(dirType), subPath);
371 return this;
372 }
373
374 /**
375 * Set the local destination for the downloaded file to a path within the public external
376 * storage directory (as returned by
377 * {@link Environment#getExternalStoragePublicDirectory(String)}.
378 *
379 * @param dirType the directory type to pass to
380 * {@link Environment#getExternalStoragePublicDirectory(String)}
381 * @param subPath the path within the external directory, including the destination filename
382 * @return this object
383 */
384 public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
385 setDestinationFromBase(Environment.getExternalStoragePublicDirectory(dirType), subPath);
386 return this;
387 }
388
389 private void setDestinationFromBase(File base, String subPath) {
390 if (subPath == null) {
391 throw new NullPointerException("subPath cannot be null");
392 }
393 mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath);
394 }
395
396 /**
397 * Add an HTTP header to be included with the download request. The header will be added to
398 * the end of the list.
Steve Howarda2709362010-07-02 17:12:48 -0700399 * @param header HTTP header name
400 * @param value header value
401 * @return this object
Steve Howard4f564cd2010-09-22 15:57:25 -0700402 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">HTTP/1.1
403 * Message Headers</a>
Steve Howarda2709362010-07-02 17:12:48 -0700404 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700405 public Request addRequestHeader(String header, String value) {
406 if (header == null) {
407 throw new NullPointerException("header cannot be null");
408 }
409 if (header.contains(":")) {
410 throw new IllegalArgumentException("header may not contain ':'");
411 }
412 if (value == null) {
413 value = "";
414 }
415 mRequestHeaders.add(Pair.create(header, value));
Steve Howarda2709362010-07-02 17:12:48 -0700416 return this;
417 }
418
419 /**
Steve Howard610c4352010-09-30 18:30:04 -0700420 * Set the title of this download, to be displayed in notifications (if enabled). If no
421 * title is given, a default one will be assigned based on the download filename, once the
422 * download starts.
Steve Howarda2709362010-07-02 17:12:48 -0700423 * @return this object
424 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700425 public Request setTitle(CharSequence title) {
Steve Howarda2709362010-07-02 17:12:48 -0700426 mTitle = title;
427 return this;
428 }
429
430 /**
431 * Set a description of this download, to be displayed in notifications (if enabled)
432 * @return this object
433 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700434 public Request setDescription(CharSequence description) {
Steve Howarda2709362010-07-02 17:12:48 -0700435 mDescription = description;
436 return this;
437 }
438
439 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700440 * Set the MIME content type of this download. This will override the content type declared
Steve Howarda2709362010-07-02 17:12:48 -0700441 * in the server's response.
Steve Howard4f564cd2010-09-22 15:57:25 -0700442 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1
443 * Media Types</a>
Steve Howarda2709362010-07-02 17:12:48 -0700444 * @return this object
445 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700446 public Request setMimeType(String mimeType) {
447 mMimeType = mimeType;
Steve Howarda2709362010-07-02 17:12:48 -0700448 return this;
449 }
450
451 /**
Steve Howard8e15afe2010-07-28 17:12:40 -0700452 * Control whether a system notification is posted by the download manager while this
453 * download is running. If enabled, the download manager posts notifications about downloads
454 * through the system {@link android.app.NotificationManager}. By default, a notification is
455 * shown.
Steve Howarda2709362010-07-02 17:12:48 -0700456 *
Steve Howard8e15afe2010-07-28 17:12:40 -0700457 * If set to false, this requires the permission
458 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
459 *
460 * @param show whether the download manager should show a notification for this download.
Steve Howarda2709362010-07-02 17:12:48 -0700461 * @return this object
462 */
Steve Howard8e15afe2010-07-28 17:12:40 -0700463 public Request setShowRunningNotification(boolean show) {
464 mShowNotification = show;
Steve Howarda2709362010-07-02 17:12:48 -0700465 return this;
466 }
467
Steve Howardb8e07a52010-07-21 14:53:21 -0700468 /**
469 * Restrict the types of networks over which this download may proceed. By default, all
470 * network types are allowed.
471 * @param flags any combination of the NETWORK_* bit flags.
472 * @return this object
473 */
Steve Howarda2709362010-07-02 17:12:48 -0700474 public Request setAllowedNetworkTypes(int flags) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700475 mAllowedNetworkTypes = flags;
476 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700477 }
478
Steve Howardb8e07a52010-07-21 14:53:21 -0700479 /**
480 * Set whether this download may proceed over a roaming connection. By default, roaming is
481 * allowed.
482 * @param allowed whether to allow a roaming connection to be used
483 * @return this object
484 */
Steve Howarda2709362010-07-02 17:12:48 -0700485 public Request setAllowedOverRoaming(boolean allowed) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700486 mRoamingAllowed = allowed;
487 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700488 }
489
490 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700491 * Set whether this download should be displayed in the system's Downloads UI. True by
492 * default.
493 * @param isVisible whether to display this download in the Downloads UI
494 * @return this object
495 */
496 public Request setVisibleInDownloadsUi(boolean isVisible) {
497 mIsVisibleInDownloadsUi = isVisible;
498 return this;
499 }
500
501 /**
Steve Howarda2709362010-07-02 17:12:48 -0700502 * @return ContentValues to be passed to DownloadProvider.insert()
503 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700504 ContentValues toContentValues(String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700505 ContentValues values = new ContentValues();
506 assert mUri != null;
507 values.put(Downloads.COLUMN_URI, mUri.toString());
Steve Howardb8e07a52010-07-21 14:53:21 -0700508 values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true);
509 values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE, packageName);
Steve Howarda2709362010-07-02 17:12:48 -0700510
511 if (mDestinationUri != null) {
Steve Howardadcb6972010-07-12 17:09:25 -0700512 values.put(Downloads.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI);
513 values.put(Downloads.COLUMN_FILE_NAME_HINT, mDestinationUri.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700514 } else {
515 values.put(Downloads.COLUMN_DESTINATION,
516 Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE);
517 }
518
519 if (!mRequestHeaders.isEmpty()) {
Steve Howardea9147d2010-07-13 19:02:45 -0700520 encodeHttpHeaders(values);
Steve Howarda2709362010-07-02 17:12:48 -0700521 }
522
523 putIfNonNull(values, Downloads.COLUMN_TITLE, mTitle);
524 putIfNonNull(values, Downloads.COLUMN_DESCRIPTION, mDescription);
Steve Howard4f564cd2010-09-22 15:57:25 -0700525 putIfNonNull(values, Downloads.COLUMN_MIME_TYPE, mMimeType);
Steve Howarda2709362010-07-02 17:12:48 -0700526
Steve Howard8e15afe2010-07-28 17:12:40 -0700527 values.put(Downloads.COLUMN_VISIBILITY,
528 mShowNotification ? Downloads.VISIBILITY_VISIBLE
529 : Downloads.VISIBILITY_HIDDEN);
Steve Howarda2709362010-07-02 17:12:48 -0700530
Steve Howardb8e07a52010-07-21 14:53:21 -0700531 values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes);
532 values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed);
Steve Howard90fb15a2010-09-09 16:13:41 -0700533 values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi);
Steve Howardb8e07a52010-07-21 14:53:21 -0700534
Steve Howarda2709362010-07-02 17:12:48 -0700535 return values;
536 }
537
Steve Howardea9147d2010-07-13 19:02:45 -0700538 private void encodeHttpHeaders(ContentValues values) {
539 int index = 0;
Steve Howard4f564cd2010-09-22 15:57:25 -0700540 for (Pair<String, String> header : mRequestHeaders) {
541 String headerString = header.first + ": " + header.second;
Steve Howardea9147d2010-07-13 19:02:45 -0700542 values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString);
543 index++;
544 }
545 }
546
Steve Howard4f564cd2010-09-22 15:57:25 -0700547 private void putIfNonNull(ContentValues contentValues, String key, Object value) {
Steve Howarda2709362010-07-02 17:12:48 -0700548 if (value != null) {
Steve Howard4f564cd2010-09-22 15:57:25 -0700549 contentValues.put(key, value.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700550 }
551 }
552 }
553
554 /**
555 * This class may be used to filter download manager queries.
556 */
557 public static class Query {
Steve Howardf054e192010-09-01 18:26:26 -0700558 /**
559 * Constant for use with {@link #orderBy}
560 * @hide
561 */
562 public static final int ORDER_ASCENDING = 1;
563
564 /**
565 * Constant for use with {@link #orderBy}
566 * @hide
567 */
568 public static final int ORDER_DESCENDING = 2;
569
570 private Long mId = null;
Steve Howarda2709362010-07-02 17:12:48 -0700571 private Integer mStatusFlags = null;
Steve Howardf054e192010-09-01 18:26:26 -0700572 private String mOrderByColumn = Downloads.COLUMN_LAST_MODIFICATION;
573 private int mOrderDirection = ORDER_DESCENDING;
Steve Howard90fb15a2010-09-09 16:13:41 -0700574 private boolean mOnlyIncludeVisibleInDownloadsUi = false;
Steve Howarda2709362010-07-02 17:12:48 -0700575
576 /**
577 * Include only the download with the given ID.
578 * @return this object
579 */
580 public Query setFilterById(long id) {
581 mId = id;
582 return this;
583 }
584
585 /**
586 * Include only downloads with status matching any the given status flags.
587 * @param flags any combination of the STATUS_* bit flags
588 * @return this object
589 */
590 public Query setFilterByStatus(int flags) {
591 mStatusFlags = flags;
592 return this;
593 }
594
595 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700596 * Controls whether this query includes downloads not visible in the system's Downloads UI.
597 * @param value if true, this query will only include downloads that should be displayed in
598 * the system's Downloads UI; if false (the default), this query will include
599 * both visible and invisible downloads.
600 * @return this object
601 * @hide
602 */
603 public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) {
604 mOnlyIncludeVisibleInDownloadsUi = value;
605 return this;
606 }
607
608 /**
Steve Howardf054e192010-09-01 18:26:26 -0700609 * Change the sort order of the returned Cursor.
610 *
611 * @param column one of the COLUMN_* constants; currently, only
612 * {@link #COLUMN_LAST_MODIFIED_TIMESTAMP} and {@link #COLUMN_TOTAL_SIZE_BYTES} are
613 * supported.
614 * @param direction either {@link #ORDER_ASCENDING} or {@link #ORDER_DESCENDING}
615 * @return this object
616 * @hide
617 */
618 public Query orderBy(String column, int direction) {
619 if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) {
620 throw new IllegalArgumentException("Invalid direction: " + direction);
621 }
622
623 if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) {
624 mOrderByColumn = Downloads.COLUMN_LAST_MODIFICATION;
625 } else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
626 mOrderByColumn = Downloads.COLUMN_TOTAL_BYTES;
627 } else {
628 throw new IllegalArgumentException("Cannot order by " + column);
629 }
630 mOrderDirection = direction;
631 return this;
632 }
633
634 /**
Steve Howarda2709362010-07-02 17:12:48 -0700635 * Run this query using the given ContentResolver.
636 * @param projection the projection to pass to ContentResolver.query()
637 * @return the Cursor returned by ContentResolver.query()
638 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700639 Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) {
640 Uri uri = baseUri;
Steve Howard90fb15a2010-09-09 16:13:41 -0700641 List<String> selectionParts = new ArrayList<String>();
Steve Howarda2709362010-07-02 17:12:48 -0700642
643 if (mId != null) {
Steve Howardeca77fc2010-09-12 18:49:08 -0700644 uri = ContentUris.withAppendedId(uri, mId);
Steve Howarda2709362010-07-02 17:12:48 -0700645 }
646
647 if (mStatusFlags != null) {
648 List<String> parts = new ArrayList<String>();
649 if ((mStatusFlags & STATUS_PENDING) != 0) {
650 parts.add(statusClause("=", Downloads.STATUS_PENDING));
651 }
652 if ((mStatusFlags & STATUS_RUNNING) != 0) {
653 parts.add(statusClause("=", Downloads.STATUS_RUNNING));
654 }
655 if ((mStatusFlags & STATUS_PAUSED) != 0) {
Steve Howard3e8c1d32010-09-29 17:03:32 -0700656 parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP));
657 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY));
658 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK));
659 parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI));
Steve Howarda2709362010-07-02 17:12:48 -0700660 }
661 if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) {
662 parts.add(statusClause("=", Downloads.STATUS_SUCCESS));
663 }
664 if ((mStatusFlags & STATUS_FAILED) != 0) {
665 parts.add("(" + statusClause(">=", 400)
666 + " AND " + statusClause("<", 600) + ")");
667 }
Steve Howard90fb15a2010-09-09 16:13:41 -0700668 selectionParts.add(joinStrings(" OR ", parts));
Steve Howarda2709362010-07-02 17:12:48 -0700669 }
Steve Howardf054e192010-09-01 18:26:26 -0700670
Steve Howard90fb15a2010-09-09 16:13:41 -0700671 if (mOnlyIncludeVisibleInDownloadsUi) {
672 selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'");
673 }
674
675 String selection = joinStrings(" AND ", selectionParts);
Steve Howardf054e192010-09-01 18:26:26 -0700676 String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC");
677 String orderBy = mOrderByColumn + " " + orderDirection;
678
Steve Howardadcb6972010-07-12 17:09:25 -0700679 return resolver.query(uri, projection, selection, null, orderBy);
Steve Howarda2709362010-07-02 17:12:48 -0700680 }
681
682 private String joinStrings(String joiner, Iterable<String> parts) {
683 StringBuilder builder = new StringBuilder();
684 boolean first = true;
685 for (String part : parts) {
686 if (!first) {
687 builder.append(joiner);
688 }
689 builder.append(part);
690 first = false;
691 }
692 return builder.toString();
693 }
694
695 private String statusClause(String operator, int value) {
696 return Downloads.COLUMN_STATUS + operator + "'" + value + "'";
697 }
698 }
699
700 private ContentResolver mResolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700701 private String mPackageName;
Steve Howardeca77fc2010-09-12 18:49:08 -0700702 private Uri mBaseUri = Downloads.Impl.CONTENT_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700703
704 /**
705 * @hide
706 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700707 public DownloadManager(ContentResolver resolver, String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700708 mResolver = resolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700709 mPackageName = packageName;
Steve Howarda2709362010-07-02 17:12:48 -0700710 }
711
712 /**
Steve Howardeca77fc2010-09-12 18:49:08 -0700713 * Makes this object access the download provider through /all_downloads URIs rather than
714 * /my_downloads URIs, for clients that have permission to do so.
715 * @hide
716 */
717 public void setAccessAllDownloads(boolean accessAllDownloads) {
718 if (accessAllDownloads) {
719 mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI;
720 } else {
721 mBaseUri = Downloads.Impl.CONTENT_URI;
722 }
723 }
724
725 /**
Steve Howarda2709362010-07-02 17:12:48 -0700726 * Enqueue a new download. The download will start automatically once the download manager is
727 * ready to execute it and connectivity is available.
728 *
729 * @param request the parameters specifying this download
730 * @return an ID for the download, unique across the system. This ID is used to make future
731 * calls related to this download.
732 */
733 public long enqueue(Request request) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700734 ContentValues values = request.toContentValues(mPackageName);
Steve Howarda2709362010-07-02 17:12:48 -0700735 Uri downloadUri = mResolver.insert(Downloads.CONTENT_URI, values);
736 long id = Long.parseLong(downloadUri.getLastPathSegment());
737 return id;
738 }
739
740 /**
741 * Cancel a download and remove it from the download manager. The download will be stopped if
742 * it was running, and it will no longer be accessible through the download manager. If a file
743 * was already downloaded, it will not be deleted.
744 *
745 * @param id the ID of the download
746 */
747 public void remove(long id) {
748 int numDeleted = mResolver.delete(getDownloadUri(id), null, null);
749 if (numDeleted == 0) {
750 throw new IllegalArgumentException("Download " + id + " does not exist");
751 }
752 }
753
754 /**
755 * Query the download manager about downloads that have been requested.
756 * @param query parameters specifying filters for this query
757 * @return a Cursor over the result set of downloads, with columns consisting of all the
758 * COLUMN_* constants.
759 */
760 public Cursor query(Query query) {
Steve Howardeca77fc2010-09-12 18:49:08 -0700761 Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri);
Steve Howardf054e192010-09-01 18:26:26 -0700762 if (underlyingCursor == null) {
763 return null;
764 }
Steve Howardeca77fc2010-09-12 18:49:08 -0700765 return new CursorTranslator(underlyingCursor, mBaseUri);
Steve Howarda2709362010-07-02 17:12:48 -0700766 }
767
768 /**
769 * Open a downloaded file for reading. The download must have completed.
770 * @param id the ID of the download
771 * @return a read-only {@link ParcelFileDescriptor}
772 * @throws FileNotFoundException if the destination file does not already exist
773 */
774 public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException {
775 return mResolver.openFileDescriptor(getDownloadUri(id), "r");
776 }
777
778 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700779 * Restart the given download, which must have already completed (successfully or not). This
780 * method will only work when called from within the download manager's process.
781 * @param id the ID of the download
782 * @hide
783 */
784 public void restartDownload(long id) {
785 Cursor cursor = query(new Query().setFilterById(id));
786 try {
787 if (!cursor.moveToFirst()) {
788 throw new IllegalArgumentException("No download with id " + id);
789 }
790 int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS));
791 if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) {
792 throw new IllegalArgumentException("Cannot restart incomplete download: " + id);
793 }
794 } finally {
795 cursor.close();
796 }
797
798 ContentValues values = new ContentValues();
799 values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0);
800 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1);
801 values.putNull(Downloads.Impl._DATA);
802 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
803 mResolver.update(getDownloadUri(id), values, null, null);
804 }
805
806 /**
Steve Howarda2709362010-07-02 17:12:48 -0700807 * Get the DownloadProvider URI for the download with the given ID.
808 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700809 Uri getDownloadUri(long id) {
810 return ContentUris.withAppendedId(mBaseUri, id);
Steve Howarda2709362010-07-02 17:12:48 -0700811 }
812
813 /**
814 * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and
815 * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants.
816 * Some columns correspond directly to underlying values while others are computed from
817 * underlying data.
818 */
819 private static class CursorTranslator extends CursorWrapper {
Steve Howardeca77fc2010-09-12 18:49:08 -0700820 private Uri mBaseUri;
821
822 public CursorTranslator(Cursor cursor, Uri baseUri) {
Steve Howarda2709362010-07-02 17:12:48 -0700823 super(cursor);
Steve Howardeca77fc2010-09-12 18:49:08 -0700824 mBaseUri = baseUri;
Steve Howarda2709362010-07-02 17:12:48 -0700825 }
826
827 @Override
828 public int getColumnIndex(String columnName) {
829 return Arrays.asList(COLUMNS).indexOf(columnName);
830 }
831
832 @Override
833 public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {
834 int index = getColumnIndex(columnName);
835 if (index == -1) {
Steve Howardf054e192010-09-01 18:26:26 -0700836 throw new IllegalArgumentException("No such column: " + columnName);
Steve Howarda2709362010-07-02 17:12:48 -0700837 }
838 return index;
839 }
840
841 @Override
842 public String getColumnName(int columnIndex) {
843 int numColumns = COLUMNS.length;
844 if (columnIndex < 0 || columnIndex >= numColumns) {
845 throw new IllegalArgumentException("Invalid column index " + columnIndex + ", "
846 + numColumns + " columns exist");
847 }
848 return COLUMNS[columnIndex];
849 }
850
851 @Override
852 public String[] getColumnNames() {
853 String[] returnColumns = new String[COLUMNS.length];
854 System.arraycopy(COLUMNS, 0, returnColumns, 0, COLUMNS.length);
855 return returnColumns;
856 }
857
858 @Override
859 public int getColumnCount() {
860 return COLUMNS.length;
861 }
862
863 @Override
864 public byte[] getBlob(int columnIndex) {
865 throw new UnsupportedOperationException();
866 }
867
868 @Override
869 public double getDouble(int columnIndex) {
870 return getLong(columnIndex);
871 }
872
873 private boolean isLongColumn(String column) {
874 return LONG_COLUMNS.contains(column);
875 }
876
877 @Override
878 public float getFloat(int columnIndex) {
879 return (float) getDouble(columnIndex);
880 }
881
882 @Override
883 public int getInt(int columnIndex) {
884 return (int) getLong(columnIndex);
885 }
886
887 @Override
888 public long getLong(int columnIndex) {
889 return translateLong(getColumnName(columnIndex));
890 }
891
892 @Override
893 public short getShort(int columnIndex) {
894 return (short) getLong(columnIndex);
895 }
896
897 @Override
898 public String getString(int columnIndex) {
899 return translateString(getColumnName(columnIndex));
900 }
901
902 private String translateString(String column) {
903 if (isLongColumn(column)) {
904 return Long.toString(translateLong(column));
905 }
906 if (column.equals(COLUMN_TITLE)) {
907 return getUnderlyingString(Downloads.COLUMN_TITLE);
908 }
909 if (column.equals(COLUMN_DESCRIPTION)) {
910 return getUnderlyingString(Downloads.COLUMN_DESCRIPTION);
911 }
912 if (column.equals(COLUMN_URI)) {
913 return getUnderlyingString(Downloads.COLUMN_URI);
914 }
915 if (column.equals(COLUMN_MEDIA_TYPE)) {
916 return getUnderlyingString(Downloads.COLUMN_MIME_TYPE);
917 }
Steve Howard8651bd52010-08-03 12:35:32 -0700918
Steve Howarda2709362010-07-02 17:12:48 -0700919 assert column.equals(COLUMN_LOCAL_URI);
Steve Howardeca77fc2010-09-12 18:49:08 -0700920 return getLocalUri();
921 }
922
923 private String getLocalUri() {
Steve Howardeca77fc2010-09-12 18:49:08 -0700924 long destinationType = getUnderlyingLong(Downloads.Impl.COLUMN_DESTINATION);
925 if (destinationType == Downloads.Impl.DESTINATION_FILE_URI) {
Steve Howarda9e87c92010-09-16 12:02:03 -0700926 // return client-provided file URI for external download
927 return getUnderlyingString(Downloads.Impl.COLUMN_FILE_NAME_HINT);
Steve Howardeca77fc2010-09-12 18:49:08 -0700928 }
929
Steve Howardbb0d23b2010-09-22 18:56:29 -0700930 if (destinationType == Downloads.Impl.DESTINATION_EXTERNAL) {
931 // return stored destination for legacy external download
Steve Howard99047d72010-09-29 17:41:37 -0700932 String localPath = getUnderlyingString(Downloads.Impl._DATA);
933 if (localPath == null) {
934 return null;
935 }
936 return Uri.fromFile(new File(localPath)).toString();
Steve Howardbb0d23b2010-09-22 18:56:29 -0700937 }
938
Steve Howardeca77fc2010-09-12 18:49:08 -0700939 // return content URI for cache download
940 long downloadId = getUnderlyingLong(Downloads.Impl._ID);
941 return ContentUris.withAppendedId(mBaseUri, downloadId).toString();
Steve Howarda2709362010-07-02 17:12:48 -0700942 }
943
944 private long translateLong(String column) {
945 if (!isLongColumn(column)) {
946 // mimic behavior of underlying cursor -- most likely, throw NumberFormatException
947 return Long.valueOf(translateString(column));
948 }
949
950 if (column.equals(COLUMN_ID)) {
951 return getUnderlyingLong(Downloads.Impl._ID);
952 }
953 if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
954 return getUnderlyingLong(Downloads.COLUMN_TOTAL_BYTES);
955 }
956 if (column.equals(COLUMN_STATUS)) {
957 return translateStatus((int) getUnderlyingLong(Downloads.COLUMN_STATUS));
958 }
Steve Howard3e8c1d32010-09-29 17:03:32 -0700959 if (column.equals(COLUMN_REASON)) {
960 return getReason((int) getUnderlyingLong(Downloads.COLUMN_STATUS));
Steve Howarda2709362010-07-02 17:12:48 -0700961 }
962 if (column.equals(COLUMN_BYTES_DOWNLOADED_SO_FAR)) {
963 return getUnderlyingLong(Downloads.COLUMN_CURRENT_BYTES);
964 }
Steve Howardadcb6972010-07-12 17:09:25 -0700965 assert column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP);
966 return getUnderlyingLong(Downloads.COLUMN_LAST_MODIFICATION);
Steve Howarda2709362010-07-02 17:12:48 -0700967 }
968
Steve Howard3e8c1d32010-09-29 17:03:32 -0700969 private long getReason(int status) {
970 switch (translateStatus(status)) {
971 case STATUS_FAILED:
972 return getErrorCode(status);
973
974 case STATUS_PAUSED:
975 return getPausedReason(status);
976
977 default:
978 return 0; // arbitrary value when status is not an error
Steve Howarda2709362010-07-02 17:12:48 -0700979 }
Steve Howard3e8c1d32010-09-29 17:03:32 -0700980 }
981
982 private long getPausedReason(int status) {
983 switch (status) {
984 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
985 return PAUSED_WAITING_TO_RETRY;
986
987 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
988 return PAUSED_WAITING_FOR_NETWORK;
989
990 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
991 return PAUSED_QUEUED_FOR_WIFI;
992
993 default:
994 return PAUSED_UNKNOWN;
995 }
996 }
997
998 private long getErrorCode(int status) {
Steve Howard33bbd122010-08-02 17:51:29 -0700999 if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS)
1000 || (500 <= status && status < 600)) {
Steve Howarda2709362010-07-02 17:12:48 -07001001 // HTTP status code
1002 return status;
1003 }
1004
1005 switch (status) {
1006 case Downloads.STATUS_FILE_ERROR:
1007 return ERROR_FILE_ERROR;
1008
1009 case Downloads.STATUS_UNHANDLED_HTTP_CODE:
1010 case Downloads.STATUS_UNHANDLED_REDIRECT:
1011 return ERROR_UNHANDLED_HTTP_CODE;
1012
1013 case Downloads.STATUS_HTTP_DATA_ERROR:
1014 return ERROR_HTTP_DATA_ERROR;
1015
1016 case Downloads.STATUS_TOO_MANY_REDIRECTS:
1017 return ERROR_TOO_MANY_REDIRECTS;
1018
1019 case Downloads.STATUS_INSUFFICIENT_SPACE_ERROR:
1020 return ERROR_INSUFFICIENT_SPACE;
1021
1022 case Downloads.STATUS_DEVICE_NOT_FOUND_ERROR:
1023 return ERROR_DEVICE_NOT_FOUND;
1024
Steve Howard33bbd122010-08-02 17:51:29 -07001025 case Downloads.Impl.STATUS_CANNOT_RESUME:
1026 return ERROR_CANNOT_RESUME;
1027
Steve Howarda9e87c92010-09-16 12:02:03 -07001028 case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR:
1029 return ERROR_FILE_ALREADY_EXISTS;
1030
Steve Howarda2709362010-07-02 17:12:48 -07001031 default:
1032 return ERROR_UNKNOWN;
1033 }
1034 }
1035
1036 private long getUnderlyingLong(String column) {
1037 return super.getLong(super.getColumnIndex(column));
1038 }
1039
1040 private String getUnderlyingString(String column) {
1041 return super.getString(super.getColumnIndex(column));
1042 }
1043
Steve Howard3e8c1d32010-09-29 17:03:32 -07001044 private int translateStatus(int status) {
Steve Howarda2709362010-07-02 17:12:48 -07001045 switch (status) {
1046 case Downloads.STATUS_PENDING:
1047 return STATUS_PENDING;
1048
1049 case Downloads.STATUS_RUNNING:
1050 return STATUS_RUNNING;
1051
Steve Howard3e8c1d32010-09-29 17:03:32 -07001052 case Downloads.Impl.STATUS_PAUSED_BY_APP:
1053 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1054 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1055 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
Steve Howarda2709362010-07-02 17:12:48 -07001056 return STATUS_PAUSED;
1057
1058 case Downloads.STATUS_SUCCESS:
1059 return STATUS_SUCCESSFUL;
1060
1061 default:
1062 assert Downloads.isStatusError(status);
1063 return STATUS_FAILED;
1064 }
1065 }
1066 }
1067}