blob: e82bad70d3b0e253ebd213d17c009bd1b98ff80e [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;
29import android.provider.Downloads;
Vasu Nori0abbf802011-01-17 15:08:14 -080030import android.provider.Settings;
31import android.provider.Settings.SettingNotFoundException;
Vasu Noric0e50752011-01-20 17:57:54 -080032import android.text.TextUtils;
Steve Howard4f564cd2010-09-22 15:57:25 -070033import android.util.Pair;
Steve Howarda2709362010-07-02 17:12:48 -070034
Steve Howard4f564cd2010-09-22 15:57:25 -070035import java.io.File;
Steve Howarda2709362010-07-02 17:12:48 -070036import java.io.FileNotFoundException;
37import java.util.ArrayList;
Steve Howarda2709362010-07-02 17:12:48 -070038import java.util.List;
Steve Howarda2709362010-07-02 17:12:48 -070039
40/**
41 * The download manager is a system service that handles long-running HTTP downloads. Clients may
42 * request that a URI be downloaded to a particular destination file. The download manager will
43 * conduct the download in the background, taking care of HTTP interactions and retrying downloads
44 * after failures or across connectivity changes and system reboots.
45 *
46 * Instances of this class should be obtained through
47 * {@link android.content.Context#getSystemService(String)} by passing
48 * {@link android.content.Context#DOWNLOAD_SERVICE}.
Steve Howard610c4352010-09-30 18:30:04 -070049 *
50 * Apps that request downloads through this API should register a broadcast receiver for
51 * {@link #ACTION_NOTIFICATION_CLICKED} to appropriately handle when the user clicks on a running
52 * download in a notification or from the downloads UI.
Steve Howarda2709362010-07-02 17:12:48 -070053 */
54public class DownloadManager {
Vasu Norie7be6bd2010-10-10 14:58:08 -070055
Steve Howarda2709362010-07-02 17:12:48 -070056 /**
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 */
Vasu Norief7e33b2010-10-20 13:26:02 -070060 public final static String COLUMN_ID = Downloads.Impl._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 */
Vasu Norief7e33b2010-10-20 13:26:02 -070066 public final static String COLUMN_TITLE = Downloads.Impl.COLUMN_TITLE;
Steve Howarda2709362010-07-02 17:12:48 -070067
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 */
Vasu Norief7e33b2010-10-20 13:26:02 -070072 public final static String COLUMN_DESCRIPTION = Downloads.Impl.COLUMN_DESCRIPTION;
Steve Howarda2709362010-07-02 17:12:48 -070073
74 /**
75 * URI to be downloaded.
76 */
Vasu Norief7e33b2010-10-20 13:26:02 -070077 public final static String COLUMN_URI = Downloads.Impl.COLUMN_URI;
Steve Howarda2709362010-07-02 17:12:48 -070078
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 */
Vasu Norief7e33b2010-10-20 13:26:02 -0700109 public final static String COLUMN_STATUS = Downloads.Impl.COLUMN_STATUS;
Steve Howarda2709362010-07-02 17:12:48 -0700110
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
Vasu Nori216fa222010-10-12 23:08:13 -0700141 /**
142 * The URI to the corresponding entry in MediaProvider for this downloaded entry. It is
143 * used to delete the entries from MediaProvider database when it is deleted from the
144 * downloaded list.
145 */
Vasu Norief7e33b2010-10-20 13:26:02 -0700146 public static final String COLUMN_MEDIAPROVIDER_URI = Downloads.Impl.COLUMN_MEDIAPROVIDER_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700147
148 /**
149 * Value of {@link #COLUMN_STATUS} when the download is waiting to start.
150 */
151 public final static int STATUS_PENDING = 1 << 0;
152
153 /**
154 * Value of {@link #COLUMN_STATUS} when the download is currently running.
155 */
156 public final static int STATUS_RUNNING = 1 << 1;
157
158 /**
159 * Value of {@link #COLUMN_STATUS} when the download is waiting to retry or resume.
160 */
161 public final static int STATUS_PAUSED = 1 << 2;
162
163 /**
164 * Value of {@link #COLUMN_STATUS} when the download has successfully completed.
165 */
166 public final static int STATUS_SUCCESSFUL = 1 << 3;
167
168 /**
169 * Value of {@link #COLUMN_STATUS} when the download has failed (and will not be retried).
170 */
171 public final static int STATUS_FAILED = 1 << 4;
172
173
174 /**
175 * Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit
176 * under any other error code.
177 */
178 public final static int ERROR_UNKNOWN = 1000;
179
180 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700181 * Value of {@link #COLUMN_REASON} when a storage issue arises which doesn't fit under any
Steve Howarda2709362010-07-02 17:12:48 -0700182 * other error code. Use the more specific {@link #ERROR_INSUFFICIENT_SPACE} and
183 * {@link #ERROR_DEVICE_NOT_FOUND} when appropriate.
184 */
185 public final static int ERROR_FILE_ERROR = 1001;
186
187 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700188 * Value of {@link #COLUMN_REASON} when an HTTP code was received that download manager
Steve Howarda2709362010-07-02 17:12:48 -0700189 * can't handle.
190 */
191 public final static int ERROR_UNHANDLED_HTTP_CODE = 1002;
192
193 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700194 * Value of {@link #COLUMN_REASON} when an error receiving or processing data occurred at
Steve Howarda2709362010-07-02 17:12:48 -0700195 * the HTTP level.
196 */
197 public final static int ERROR_HTTP_DATA_ERROR = 1004;
198
199 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700200 * Value of {@link #COLUMN_REASON} when there were too many redirects.
Steve Howarda2709362010-07-02 17:12:48 -0700201 */
202 public final static int ERROR_TOO_MANY_REDIRECTS = 1005;
203
204 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700205 * Value of {@link #COLUMN_REASON} when there was insufficient storage space. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700206 * this is because the SD card is full.
207 */
208 public final static int ERROR_INSUFFICIENT_SPACE = 1006;
209
210 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700211 * Value of {@link #COLUMN_REASON} when no external storage device was found. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700212 * this is because the SD card is not mounted.
213 */
214 public final static int ERROR_DEVICE_NOT_FOUND = 1007;
215
Steve Howardb8e07a52010-07-21 14:53:21 -0700216 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700217 * Value of {@link #COLUMN_REASON} when some possibly transient error occurred but we can't
Steve Howard33bbd122010-08-02 17:51:29 -0700218 * resume the download.
219 */
220 public final static int ERROR_CANNOT_RESUME = 1008;
221
222 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700223 * Value of {@link #COLUMN_REASON} when the requested destination file already exists (the
Steve Howarda9e87c92010-09-16 12:02:03 -0700224 * download manager will not overwrite an existing file).
225 */
226 public final static int ERROR_FILE_ALREADY_EXISTS = 1009;
227
228 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700229 * Value of {@link #COLUMN_REASON} when the download is paused because some network error
230 * occurred and the download manager is waiting before retrying the request.
231 */
232 public final static int PAUSED_WAITING_TO_RETRY = 1;
233
234 /**
235 * Value of {@link #COLUMN_REASON} when the download is waiting for network connectivity to
236 * proceed.
237 */
238 public final static int PAUSED_WAITING_FOR_NETWORK = 2;
239
240 /**
241 * Value of {@link #COLUMN_REASON} when the download exceeds a size limit for downloads over
242 * the mobile network and the download manager is waiting for a Wi-Fi connection to proceed.
243 */
244 public final static int PAUSED_QUEUED_FOR_WIFI = 3;
245
246 /**
247 * Value of {@link #COLUMN_REASON} when the download is paused for some other reason.
248 */
249 public final static int PAUSED_UNKNOWN = 4;
250
251 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700252 * Broadcast intent action sent by the download manager when a download completes.
253 */
254 public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE";
255
256 /**
Steve Howard610c4352010-09-30 18:30:04 -0700257 * Broadcast intent action sent by the download manager when the user clicks on a running
258 * download, either from a system notification or from the downloads UI.
Steve Howardb8e07a52010-07-21 14:53:21 -0700259 */
260 public final static String ACTION_NOTIFICATION_CLICKED =
261 "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
262
263 /**
Steve Howarde78fc182010-09-24 14:59:36 -0700264 * Intent action to launch an activity to display all downloads.
265 */
266 public final static String ACTION_VIEW_DOWNLOADS = "android.intent.action.VIEW_DOWNLOADS";
267
268 /**
Vasu Norie5f92242011-01-24 16:12:20 -0800269 * Intent extra included with {@link #ACTION_VIEW_DOWNLOADS} to start DownloadApp in
270 * sort-by-size mode.
271 */
272 public final static String INTENT_EXTRAS_SORT_BY_SIZE =
273 "android.app.DownloadManager.extra_sortBySize";
274
275 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700276 * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a
277 * long) of the download that just completed.
278 */
279 public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
Steve Howarda2709362010-07-02 17:12:48 -0700280
Vasu Nori71b8c232010-10-27 15:22:19 -0700281 /**
282 * When clicks on multiple notifications are received, the following
283 * provides an array of download ids corresponding to the download notification that was
284 * clicked. It can be retrieved by the receiver of this
285 * Intent using {@link android.content.Intent#getLongArrayExtra(String)}.
286 */
287 public static final String EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS = "extra_click_download_ids";
288
Vasu Norie16c43b2010-11-06 18:48:08 -0700289 /**
290 * columns to request from DownloadProvider.
291 * @hide
292 */
293 public static final String[] UNDERLYING_COLUMNS = new String[] {
Steve Howarda2709362010-07-02 17:12:48 -0700294 Downloads.Impl._ID,
Vasu Norie69924f2010-11-15 13:10:11 -0800295 Downloads.Impl._DATA + " AS " + COLUMN_LOCAL_FILENAME,
Vasu Nori216fa222010-10-12 23:08:13 -0700296 Downloads.Impl.COLUMN_MEDIAPROVIDER_URI,
Vasu Nori5be894e2010-11-02 21:55:30 -0700297 Downloads.Impl.COLUMN_DESTINATION,
Vasu Norief7e33b2010-10-20 13:26:02 -0700298 Downloads.Impl.COLUMN_TITLE,
299 Downloads.Impl.COLUMN_DESCRIPTION,
300 Downloads.Impl.COLUMN_URI,
Vasu Norief7e33b2010-10-20 13:26:02 -0700301 Downloads.Impl.COLUMN_STATUS,
Steve Howarda9e87c92010-09-16 12:02:03 -0700302 Downloads.Impl.COLUMN_FILE_NAME_HINT,
Vasu Norie16c43b2010-11-06 18:48:08 -0700303 Downloads.Impl.COLUMN_MIME_TYPE + " AS " + COLUMN_MEDIA_TYPE,
304 Downloads.Impl.COLUMN_TOTAL_BYTES + " AS " + COLUMN_TOTAL_SIZE_BYTES,
305 Downloads.Impl.COLUMN_LAST_MODIFICATION + " AS " + COLUMN_LAST_MODIFIED_TIMESTAMP,
306 Downloads.Impl.COLUMN_CURRENT_BYTES + " AS " + COLUMN_BYTES_DOWNLOADED_SO_FAR,
307 /* add the following 'computed' columns to the cursor.
308 * they are not 'returned' by the database, but their inclusion
309 * eliminates need to have lot of methods in CursorTranslator
310 */
311 "'placeholder' AS " + COLUMN_LOCAL_URI,
312 "'placeholder' AS " + COLUMN_REASON
Steve Howarda2709362010-07-02 17:12:48 -0700313 };
314
Steve Howarda2709362010-07-02 17:12:48 -0700315 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700316 * This class contains all the information necessary to request a new download. The URI is the
Steve Howarda2709362010-07-02 17:12:48 -0700317 * only required parameter.
Steve Howard4f564cd2010-09-22 15:57:25 -0700318 *
319 * Note that the default download destination is a shared volume where the system might delete
320 * your file if it needs to reclaim space for system use. If this is a problem, use a location
321 * on external storage (see {@link #setDestinationUri(Uri)}.
Steve Howarda2709362010-07-02 17:12:48 -0700322 */
323 public static class Request {
324 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700325 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
326 * {@link ConnectivityManager#TYPE_MOBILE}.
327 */
328 public static final int NETWORK_MOBILE = 1 << 0;
Steve Howarda2709362010-07-02 17:12:48 -0700329
Steve Howardb8e07a52010-07-21 14:53:21 -0700330 /**
331 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
332 * {@link ConnectivityManager#TYPE_WIFI}.
333 */
334 public static final int NETWORK_WIFI = 1 << 1;
335
Steve Howardb8e07a52010-07-21 14:53:21 -0700336 private Uri mUri;
337 private Uri mDestinationUri;
Steve Howard4f564cd2010-09-22 15:57:25 -0700338 private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>();
339 private CharSequence mTitle;
340 private CharSequence mDescription;
Steve Howard4f564cd2010-09-22 15:57:25 -0700341 private String mMimeType;
Steve Howardb8e07a52010-07-21 14:53:21 -0700342 private boolean mRoamingAllowed = true;
343 private int mAllowedNetworkTypes = ~0; // default to all network types allowed
Steve Howard90fb15a2010-09-09 16:13:41 -0700344 private boolean mIsVisibleInDownloadsUi = true;
Vasu Nori5be894e2010-11-02 21:55:30 -0700345 private boolean mScannable = false;
Vasu Norif83e6e42010-12-13 16:28:31 -0800346 private boolean mUseSystemCache = false;
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700347 /** if a file is designated as a MediaScanner scannable file, the following value is
348 * stored in the database column {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}.
349 */
350 private static final int SCANNABLE_VALUE_YES = 0;
351 // value of 1 is stored in the above column by DownloadProvider after it is scanned by
352 // MediaScanner
353 /** if a file is designated as a file that should not be scanned by MediaScanner,
354 * the following value is stored in the database column
355 * {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}.
356 */
357 private static final int SCANNABLE_VALUE_NO = 2;
Steve Howarda2709362010-07-02 17:12:48 -0700358
359 /**
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700360 * This download is visible but only shows in the notifications
361 * while it's in progress.
362 */
363 public static final int VISIBILITY_VISIBLE = 0;
364
365 /**
366 * This download is visible and shows in the notifications while
367 * in progress and after completion.
368 */
369 public static final int VISIBILITY_VISIBLE_NOTIFY_COMPLETED = 1;
370
371 /**
372 * This download doesn't show in the UI or in the notifications.
373 */
374 public static final int VISIBILITY_HIDDEN = 2;
375
376 /** can take any of the following values: {@link #VISIBILITY_HIDDEN}
377 * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}, {@link #VISIBILITY_VISIBLE}
378 */
379 private int mNotificationVisibility = VISIBILITY_VISIBLE;
380
381 /**
Steve Howarda2709362010-07-02 17:12:48 -0700382 * @param uri the HTTP URI to download.
383 */
384 public Request(Uri uri) {
385 if (uri == null) {
386 throw new NullPointerException();
387 }
388 String scheme = uri.getScheme();
Paul Westbrook86a60192010-09-15 12:55:49 -0700389 if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) {
390 throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + uri);
Steve Howarda2709362010-07-02 17:12:48 -0700391 }
392 mUri = uri;
393 }
394
Vasu Noric0e50752011-01-20 17:57:54 -0800395 Request(String uriString) {
396 mUri = Uri.parse(uriString);
397 }
398
Steve Howarda2709362010-07-02 17:12:48 -0700399 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700400 * Set the local destination for the downloaded file. Must be a file URI to a path on
Steve Howarda2709362010-07-02 17:12:48 -0700401 * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE
402 * permission.
Vasu Nori5be894e2010-11-02 21:55:30 -0700403 * <p>
404 * The downloaded file is not scanned by MediaScanner.
405 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
406 * <p>
Steve Howard4f564cd2010-09-22 15:57:25 -0700407 * By default, downloads are saved to a generated filename in the shared download cache and
408 * may be deleted by the system at any time to reclaim space.
Steve Howarda2709362010-07-02 17:12:48 -0700409 *
410 * @return this object
411 */
412 public Request setDestinationUri(Uri uri) {
413 mDestinationUri = uri;
414 return this;
415 }
416
417 /**
Vasu Norif83e6e42010-12-13 16:28:31 -0800418 * Set the local destination for the downloaded file to the system cache dir (/cache).
419 * This is only available to System apps with the permission
420 * {@link android.Manifest.permission#ACCESS_CACHE_FILESYSTEM}.
421 * <p>
422 * The downloaded file is not scanned by MediaScanner.
423 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
424 * <p>
425 * Files downloaded to /cache may be deleted by the system at any time to reclaim space.
426 *
427 * @return this object
428 * @hide
429 */
430 public Request setDestinationToSystemCache() {
431 mUseSystemCache = true;
432 return this;
433 }
434
435 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700436 * Set the local destination for the downloaded file to a path within the application's
437 * external files directory (as returned by {@link Context#getExternalFilesDir(String)}.
Vasu Nori5be894e2010-11-02 21:55:30 -0700438 * <p>
439 * The downloaded file is not scanned by MediaScanner.
440 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
Steve Howard4f564cd2010-09-22 15:57:25 -0700441 *
442 * @param context the {@link Context} to use in determining the external files directory
443 * @param dirType the directory type to pass to {@link Context#getExternalFilesDir(String)}
444 * @param subPath the path within the external directory, including the destination filename
445 * @return this object
446 */
447 public Request setDestinationInExternalFilesDir(Context context, String dirType,
448 String subPath) {
449 setDestinationFromBase(context.getExternalFilesDir(dirType), subPath);
450 return this;
451 }
452
453 /**
454 * Set the local destination for the downloaded file to a path within the public external
455 * storage directory (as returned by
456 * {@link Environment#getExternalStoragePublicDirectory(String)}.
Vasu Nori5be894e2010-11-02 21:55:30 -0700457 *<p>
458 * The downloaded file is not scanned by MediaScanner.
459 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
Steve Howard4f564cd2010-09-22 15:57:25 -0700460 *
461 * @param dirType the directory type to pass to
462 * {@link Environment#getExternalStoragePublicDirectory(String)}
463 * @param subPath the path within the external directory, including the destination filename
464 * @return this object
465 */
466 public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
Vasu Nori6916b032010-12-19 20:31:00 -0800467 File file = Environment.getExternalStoragePublicDirectory(dirType);
468 if (file.exists()) {
469 if (!file.isDirectory()) {
470 throw new IllegalStateException(file.getAbsolutePath() +
471 " already exists and is not a directory");
472 }
473 } else {
474 if (!file.mkdir()) {
475 throw new IllegalStateException("Unable to create directory: "+
476 file.getAbsolutePath());
477 }
478 }
479 setDestinationFromBase(file, subPath);
Steve Howard4f564cd2010-09-22 15:57:25 -0700480 return this;
481 }
482
483 private void setDestinationFromBase(File base, String subPath) {
484 if (subPath == null) {
485 throw new NullPointerException("subPath cannot be null");
486 }
487 mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath);
488 }
489
490 /**
Vasu Nori5be894e2010-11-02 21:55:30 -0700491 * If the file to be downloaded is to be scanned by MediaScanner, this method
492 * should be called before {@link DownloadManager#enqueue(Request)} is called.
493 */
494 public void allowScanningByMediaScanner() {
495 mScannable = true;
496 }
497
498 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700499 * Add an HTTP header to be included with the download request. The header will be added to
500 * the end of the list.
Steve Howarda2709362010-07-02 17:12:48 -0700501 * @param header HTTP header name
502 * @param value header value
503 * @return this object
Steve Howard4f564cd2010-09-22 15:57:25 -0700504 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">HTTP/1.1
505 * Message Headers</a>
Steve Howarda2709362010-07-02 17:12:48 -0700506 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700507 public Request addRequestHeader(String header, String value) {
508 if (header == null) {
509 throw new NullPointerException("header cannot be null");
510 }
511 if (header.contains(":")) {
512 throw new IllegalArgumentException("header may not contain ':'");
513 }
514 if (value == null) {
515 value = "";
516 }
517 mRequestHeaders.add(Pair.create(header, value));
Steve Howarda2709362010-07-02 17:12:48 -0700518 return this;
519 }
520
521 /**
Steve Howard610c4352010-09-30 18:30:04 -0700522 * Set the title of this download, to be displayed in notifications (if enabled). If no
523 * title is given, a default one will be assigned based on the download filename, once the
524 * download starts.
Steve Howarda2709362010-07-02 17:12:48 -0700525 * @return this object
526 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700527 public Request setTitle(CharSequence title) {
Steve Howarda2709362010-07-02 17:12:48 -0700528 mTitle = title;
529 return this;
530 }
531
532 /**
533 * Set a description of this download, to be displayed in notifications (if enabled)
534 * @return this object
535 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700536 public Request setDescription(CharSequence description) {
Steve Howarda2709362010-07-02 17:12:48 -0700537 mDescription = description;
538 return this;
539 }
540
541 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700542 * Set the MIME content type of this download. This will override the content type declared
Steve Howarda2709362010-07-02 17:12:48 -0700543 * in the server's response.
Steve Howard4f564cd2010-09-22 15:57:25 -0700544 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1
545 * Media Types</a>
Steve Howarda2709362010-07-02 17:12:48 -0700546 * @return this object
547 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700548 public Request setMimeType(String mimeType) {
549 mMimeType = mimeType;
Steve Howarda2709362010-07-02 17:12:48 -0700550 return this;
551 }
552
553 /**
Steve Howard8e15afe2010-07-28 17:12:40 -0700554 * Control whether a system notification is posted by the download manager while this
555 * download is running. If enabled, the download manager posts notifications about downloads
556 * through the system {@link android.app.NotificationManager}. By default, a notification is
557 * shown.
Steve Howarda2709362010-07-02 17:12:48 -0700558 *
Steve Howard8e15afe2010-07-28 17:12:40 -0700559 * If set to false, this requires the permission
560 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
561 *
562 * @param show whether the download manager should show a notification for this download.
Steve Howarda2709362010-07-02 17:12:48 -0700563 * @return this object
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700564 * @deprecated use {@link #setNotificationVisibility(int)}
Steve Howarda2709362010-07-02 17:12:48 -0700565 */
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700566 @Deprecated
Steve Howard8e15afe2010-07-28 17:12:40 -0700567 public Request setShowRunningNotification(boolean show) {
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700568 return (show) ? setNotificationVisibility(VISIBILITY_VISIBLE) :
569 setNotificationVisibility(VISIBILITY_HIDDEN);
570 }
571
572 /**
573 * Control whether a system notification is posted by the download manager while this
574 * download is running or when it is completed.
575 * If enabled, the download manager posts notifications about downloads
576 * through the system {@link android.app.NotificationManager}.
577 * By default, a notification is shown only when the download is in progress.
578 *<p>
579 * It can take the following values: {@link #VISIBILITY_HIDDEN},
580 * {@link #VISIBILITY_VISIBLE},
581 * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}.
582 *<p>
583 * If set to {@link #VISIBILITY_HIDDEN}, this requires the permission
584 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
585 *
586 * @param visibility the visibility setting value
587 * @return this object
588 */
589 public Request setNotificationVisibility(int visibility) {
590 mNotificationVisibility = visibility;
Steve Howarda2709362010-07-02 17:12:48 -0700591 return this;
592 }
593
Steve Howardb8e07a52010-07-21 14:53:21 -0700594 /**
595 * Restrict the types of networks over which this download may proceed. By default, all
596 * network types are allowed.
597 * @param flags any combination of the NETWORK_* bit flags.
598 * @return this object
599 */
Steve Howarda2709362010-07-02 17:12:48 -0700600 public Request setAllowedNetworkTypes(int flags) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700601 mAllowedNetworkTypes = flags;
602 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700603 }
604
Steve Howardb8e07a52010-07-21 14:53:21 -0700605 /**
606 * Set whether this download may proceed over a roaming connection. By default, roaming is
607 * allowed.
608 * @param allowed whether to allow a roaming connection to be used
609 * @return this object
610 */
Steve Howarda2709362010-07-02 17:12:48 -0700611 public Request setAllowedOverRoaming(boolean allowed) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700612 mRoamingAllowed = allowed;
613 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700614 }
615
616 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700617 * Set whether this download should be displayed in the system's Downloads UI. True by
618 * default.
619 * @param isVisible whether to display this download in the Downloads UI
620 * @return this object
621 */
622 public Request setVisibleInDownloadsUi(boolean isVisible) {
623 mIsVisibleInDownloadsUi = isVisible;
624 return this;
625 }
626
627 /**
Steve Howarda2709362010-07-02 17:12:48 -0700628 * @return ContentValues to be passed to DownloadProvider.insert()
629 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700630 ContentValues toContentValues(String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700631 ContentValues values = new ContentValues();
632 assert mUri != null;
Vasu Norief7e33b2010-10-20 13:26:02 -0700633 values.put(Downloads.Impl.COLUMN_URI, mUri.toString());
Steve Howardb8e07a52010-07-21 14:53:21 -0700634 values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true);
Vasu Norief7e33b2010-10-20 13:26:02 -0700635 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE, packageName);
Steve Howarda2709362010-07-02 17:12:48 -0700636
637 if (mDestinationUri != null) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700638 values.put(Downloads.Impl.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI);
639 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, mDestinationUri.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700640 } else {
Vasu Norief7e33b2010-10-20 13:26:02 -0700641 values.put(Downloads.Impl.COLUMN_DESTINATION,
Vasu Norif83e6e42010-12-13 16:28:31 -0800642 (this.mUseSystemCache) ?
643 Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION :
644 Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE);
Steve Howarda2709362010-07-02 17:12:48 -0700645 }
Vasu Nori5be894e2010-11-02 21:55:30 -0700646 // is the file supposed to be media-scannable?
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700647 values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED, (mScannable) ? SCANNABLE_VALUE_YES :
648 SCANNABLE_VALUE_NO);
Steve Howarda2709362010-07-02 17:12:48 -0700649
650 if (!mRequestHeaders.isEmpty()) {
Steve Howardea9147d2010-07-13 19:02:45 -0700651 encodeHttpHeaders(values);
Steve Howarda2709362010-07-02 17:12:48 -0700652 }
653
Vasu Norief7e33b2010-10-20 13:26:02 -0700654 putIfNonNull(values, Downloads.Impl.COLUMN_TITLE, mTitle);
655 putIfNonNull(values, Downloads.Impl.COLUMN_DESCRIPTION, mDescription);
656 putIfNonNull(values, Downloads.Impl.COLUMN_MIME_TYPE, mMimeType);
Steve Howarda2709362010-07-02 17:12:48 -0700657
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700658 values.put(Downloads.Impl.COLUMN_VISIBILITY, mNotificationVisibility);
Steve Howardb8e07a52010-07-21 14:53:21 -0700659 values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes);
660 values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed);
Steve Howard90fb15a2010-09-09 16:13:41 -0700661 values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi);
Steve Howardb8e07a52010-07-21 14:53:21 -0700662
Steve Howarda2709362010-07-02 17:12:48 -0700663 return values;
664 }
665
Steve Howardea9147d2010-07-13 19:02:45 -0700666 private void encodeHttpHeaders(ContentValues values) {
667 int index = 0;
Steve Howard4f564cd2010-09-22 15:57:25 -0700668 for (Pair<String, String> header : mRequestHeaders) {
669 String headerString = header.first + ": " + header.second;
Steve Howardea9147d2010-07-13 19:02:45 -0700670 values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString);
671 index++;
672 }
673 }
674
Steve Howard4f564cd2010-09-22 15:57:25 -0700675 private void putIfNonNull(ContentValues contentValues, String key, Object value) {
Steve Howarda2709362010-07-02 17:12:48 -0700676 if (value != null) {
Steve Howard4f564cd2010-09-22 15:57:25 -0700677 contentValues.put(key, value.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700678 }
679 }
680 }
681
682 /**
683 * This class may be used to filter download manager queries.
684 */
685 public static class Query {
Steve Howardf054e192010-09-01 18:26:26 -0700686 /**
687 * Constant for use with {@link #orderBy}
688 * @hide
689 */
690 public static final int ORDER_ASCENDING = 1;
691
692 /**
693 * Constant for use with {@link #orderBy}
694 * @hide
695 */
696 public static final int ORDER_DESCENDING = 2;
697
Steve Howard64c48b82010-10-07 17:53:52 -0700698 private long[] mIds = null;
Steve Howarda2709362010-07-02 17:12:48 -0700699 private Integer mStatusFlags = null;
Vasu Norief7e33b2010-10-20 13:26:02 -0700700 private String mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION;
Steve Howardf054e192010-09-01 18:26:26 -0700701 private int mOrderDirection = ORDER_DESCENDING;
Steve Howard90fb15a2010-09-09 16:13:41 -0700702 private boolean mOnlyIncludeVisibleInDownloadsUi = false;
Steve Howarda2709362010-07-02 17:12:48 -0700703
704 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700705 * Include only the downloads with the given IDs.
Steve Howarda2709362010-07-02 17:12:48 -0700706 * @return this object
707 */
Steve Howard64c48b82010-10-07 17:53:52 -0700708 public Query setFilterById(long... ids) {
709 mIds = ids;
Steve Howarda2709362010-07-02 17:12:48 -0700710 return this;
711 }
712
713 /**
714 * Include only downloads with status matching any the given status flags.
715 * @param flags any combination of the STATUS_* bit flags
716 * @return this object
717 */
718 public Query setFilterByStatus(int flags) {
719 mStatusFlags = flags;
720 return this;
721 }
722
723 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700724 * Controls whether this query includes downloads not visible in the system's Downloads UI.
725 * @param value if true, this query will only include downloads that should be displayed in
726 * the system's Downloads UI; if false (the default), this query will include
727 * both visible and invisible downloads.
728 * @return this object
729 * @hide
730 */
731 public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) {
732 mOnlyIncludeVisibleInDownloadsUi = value;
733 return this;
734 }
735
736 /**
Steve Howardf054e192010-09-01 18:26:26 -0700737 * Change the sort order of the returned Cursor.
738 *
739 * @param column one of the COLUMN_* constants; currently, only
740 * {@link #COLUMN_LAST_MODIFIED_TIMESTAMP} and {@link #COLUMN_TOTAL_SIZE_BYTES} are
741 * supported.
742 * @param direction either {@link #ORDER_ASCENDING} or {@link #ORDER_DESCENDING}
743 * @return this object
744 * @hide
745 */
746 public Query orderBy(String column, int direction) {
747 if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) {
748 throw new IllegalArgumentException("Invalid direction: " + direction);
749 }
750
751 if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700752 mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION;
Steve Howardf054e192010-09-01 18:26:26 -0700753 } else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700754 mOrderByColumn = Downloads.Impl.COLUMN_TOTAL_BYTES;
Steve Howardf054e192010-09-01 18:26:26 -0700755 } else {
756 throw new IllegalArgumentException("Cannot order by " + column);
757 }
758 mOrderDirection = direction;
759 return this;
760 }
761
762 /**
Steve Howarda2709362010-07-02 17:12:48 -0700763 * Run this query using the given ContentResolver.
764 * @param projection the projection to pass to ContentResolver.query()
765 * @return the Cursor returned by ContentResolver.query()
766 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700767 Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) {
768 Uri uri = baseUri;
Steve Howard90fb15a2010-09-09 16:13:41 -0700769 List<String> selectionParts = new ArrayList<String>();
Steve Howard64c48b82010-10-07 17:53:52 -0700770 String[] selectionArgs = null;
Steve Howarda2709362010-07-02 17:12:48 -0700771
Steve Howard64c48b82010-10-07 17:53:52 -0700772 if (mIds != null) {
773 selectionParts.add(getWhereClauseForIds(mIds));
774 selectionArgs = getWhereArgsForIds(mIds);
Steve Howarda2709362010-07-02 17:12:48 -0700775 }
776
777 if (mStatusFlags != null) {
778 List<String> parts = new ArrayList<String>();
779 if ((mStatusFlags & STATUS_PENDING) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700780 parts.add(statusClause("=", Downloads.Impl.STATUS_PENDING));
Steve Howarda2709362010-07-02 17:12:48 -0700781 }
782 if ((mStatusFlags & STATUS_RUNNING) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700783 parts.add(statusClause("=", Downloads.Impl.STATUS_RUNNING));
Steve Howarda2709362010-07-02 17:12:48 -0700784 }
785 if ((mStatusFlags & STATUS_PAUSED) != 0) {
Steve Howard3e8c1d32010-09-29 17:03:32 -0700786 parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP));
787 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY));
788 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK));
789 parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI));
Steve Howarda2709362010-07-02 17:12:48 -0700790 }
791 if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700792 parts.add(statusClause("=", Downloads.Impl.STATUS_SUCCESS));
Steve Howarda2709362010-07-02 17:12:48 -0700793 }
794 if ((mStatusFlags & STATUS_FAILED) != 0) {
795 parts.add("(" + statusClause(">=", 400)
796 + " AND " + statusClause("<", 600) + ")");
797 }
Steve Howard90fb15a2010-09-09 16:13:41 -0700798 selectionParts.add(joinStrings(" OR ", parts));
Steve Howarda2709362010-07-02 17:12:48 -0700799 }
Steve Howardf054e192010-09-01 18:26:26 -0700800
Steve Howard90fb15a2010-09-09 16:13:41 -0700801 if (mOnlyIncludeVisibleInDownloadsUi) {
802 selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'");
803 }
804
Vasu Nori216fa222010-10-12 23:08:13 -0700805 // only return rows which are not marked 'deleted = 1'
806 selectionParts.add(Downloads.Impl.COLUMN_DELETED + " != '1'");
807
Steve Howard90fb15a2010-09-09 16:13:41 -0700808 String selection = joinStrings(" AND ", selectionParts);
Steve Howardf054e192010-09-01 18:26:26 -0700809 String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC");
810 String orderBy = mOrderByColumn + " " + orderDirection;
811
Steve Howard64c48b82010-10-07 17:53:52 -0700812 return resolver.query(uri, projection, selection, selectionArgs, orderBy);
Steve Howarda2709362010-07-02 17:12:48 -0700813 }
814
815 private String joinStrings(String joiner, Iterable<String> parts) {
816 StringBuilder builder = new StringBuilder();
817 boolean first = true;
818 for (String part : parts) {
819 if (!first) {
820 builder.append(joiner);
821 }
822 builder.append(part);
823 first = false;
824 }
825 return builder.toString();
826 }
827
828 private String statusClause(String operator, int value) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700829 return Downloads.Impl.COLUMN_STATUS + operator + "'" + value + "'";
Steve Howarda2709362010-07-02 17:12:48 -0700830 }
831 }
832
833 private ContentResolver mResolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700834 private String mPackageName;
Steve Howardeca77fc2010-09-12 18:49:08 -0700835 private Uri mBaseUri = Downloads.Impl.CONTENT_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700836
837 /**
838 * @hide
839 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700840 public DownloadManager(ContentResolver resolver, String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700841 mResolver = resolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700842 mPackageName = packageName;
Steve Howarda2709362010-07-02 17:12:48 -0700843 }
844
845 /**
Steve Howardeca77fc2010-09-12 18:49:08 -0700846 * Makes this object access the download provider through /all_downloads URIs rather than
847 * /my_downloads URIs, for clients that have permission to do so.
848 * @hide
849 */
850 public void setAccessAllDownloads(boolean accessAllDownloads) {
851 if (accessAllDownloads) {
852 mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI;
853 } else {
854 mBaseUri = Downloads.Impl.CONTENT_URI;
855 }
856 }
857
858 /**
Steve Howarda2709362010-07-02 17:12:48 -0700859 * Enqueue a new download. The download will start automatically once the download manager is
860 * ready to execute it and connectivity is available.
861 *
862 * @param request the parameters specifying this download
863 * @return an ID for the download, unique across the system. This ID is used to make future
864 * calls related to this download.
865 */
866 public long enqueue(Request request) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700867 ContentValues values = request.toContentValues(mPackageName);
Vasu Norief7e33b2010-10-20 13:26:02 -0700868 Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values);
Steve Howarda2709362010-07-02 17:12:48 -0700869 long id = Long.parseLong(downloadUri.getLastPathSegment());
870 return id;
871 }
872
873 /**
Vasu Nori216fa222010-10-12 23:08:13 -0700874 * Marks the specified download as 'to be deleted'. This is done when a completed download
875 * is to be removed but the row was stored without enough info to delete the corresponding
876 * metadata from Mediaprovider database. Actual cleanup of this row is done in DownloadService.
877 *
878 * @param ids the IDs of the downloads to be marked 'deleted'
879 * @return the number of downloads actually updated
880 * @hide
881 */
882 public int markRowDeleted(long... ids) {
883 if (ids == null || ids.length == 0) {
884 // called with nothing to remove!
885 throw new IllegalArgumentException("input param 'ids' can't be null");
886 }
887 ContentValues values = new ContentValues();
888 values.put(Downloads.Impl.COLUMN_DELETED, 1);
Vasu Nori8da7a4e2010-11-17 17:14:49 -0800889 // if only one id is passed in, then include it in the uri itself.
890 // this will eliminate a full database scan in the download service.
891 if (ids.length == 1) {
892 return mResolver.update(ContentUris.withAppendedId(mBaseUri, ids[0]), values,
893 null, null);
894 }
Vasu Nori216fa222010-10-12 23:08:13 -0700895 return mResolver.update(mBaseUri, values, getWhereClauseForIds(ids),
896 getWhereArgsForIds(ids));
897 }
898
899 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700900 * Cancel downloads and remove them from the download manager. Each download will be stopped if
Steve Howarda2709362010-07-02 17:12:48 -0700901 * it was running, and it will no longer be accessible through the download manager. If a file
Steve Howard64c48b82010-10-07 17:53:52 -0700902 * was already downloaded to external storage, it will not be deleted.
Steve Howarda2709362010-07-02 17:12:48 -0700903 *
Steve Howard64c48b82010-10-07 17:53:52 -0700904 * @param ids the IDs of the downloads to remove
905 * @return the number of downloads actually removed
Steve Howarda2709362010-07-02 17:12:48 -0700906 */
Steve Howard64c48b82010-10-07 17:53:52 -0700907 public int remove(long... ids) {
Vasu Norie16c43b2010-11-06 18:48:08 -0700908 return markRowDeleted(ids);
Steve Howarda2709362010-07-02 17:12:48 -0700909 }
910
911 /**
912 * Query the download manager about downloads that have been requested.
913 * @param query parameters specifying filters for this query
914 * @return a Cursor over the result set of downloads, with columns consisting of all the
915 * COLUMN_* constants.
916 */
917 public Cursor query(Query query) {
Steve Howardeca77fc2010-09-12 18:49:08 -0700918 Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri);
Steve Howardf054e192010-09-01 18:26:26 -0700919 if (underlyingCursor == null) {
920 return null;
921 }
Steve Howardeca77fc2010-09-12 18:49:08 -0700922 return new CursorTranslator(underlyingCursor, mBaseUri);
Steve Howarda2709362010-07-02 17:12:48 -0700923 }
924
925 /**
926 * Open a downloaded file for reading. The download must have completed.
927 * @param id the ID of the download
928 * @return a read-only {@link ParcelFileDescriptor}
929 * @throws FileNotFoundException if the destination file does not already exist
930 */
931 public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException {
932 return mResolver.openFileDescriptor(getDownloadUri(id), "r");
933 }
934
935 /**
Vasu Nori5be894e2010-11-02 21:55:30 -0700936 * Returns {@link Uri} for the given downloaded file id, if the file is
937 * downloaded successfully. otherwise, null is returned.
938 *<p>
939 * If the specified downloaded file is in external storage (for example, /sdcard dir),
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800940 * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds
941 * to the filepath on sdcard.
Vasu Nori5be894e2010-11-02 21:55:30 -0700942 *
943 * @param id the id of the downloaded file.
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700944 * @return the {@link Uri} for the given downloaded file id, if download was successful. null
Vasu Nori5be894e2010-11-02 21:55:30 -0700945 * otherwise.
946 */
947 public Uri getUriForDownloadedFile(long id) {
948 // to check if the file is in cache, get its destination from the database
949 Query query = new Query().setFilterById(id);
950 Cursor cursor = null;
951 try {
952 cursor = query(query);
953 if (cursor == null) {
954 return null;
955 }
Leon Scrogginsd75e64a2010-12-13 15:48:40 -0500956 if (cursor.moveToFirst()) {
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800957 int status = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_STATUS));
Vasu Nori5be894e2010-11-02 21:55:30 -0700958 if (DownloadManager.STATUS_SUCCESSFUL == status) {
959 int indx = cursor.getColumnIndexOrThrow(
960 Downloads.Impl.COLUMN_DESTINATION);
961 int destination = cursor.getInt(indx);
962 // TODO: if we ever add API to DownloadManager to let the caller specify
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700963 // non-external storage for a downloaded file, then the following code
Vasu Nori5be894e2010-11-02 21:55:30 -0700964 // should also check for that destination.
965 if (destination == Downloads.Impl.DESTINATION_CACHE_PARTITION ||
Vasu Norif83e6e42010-12-13 16:28:31 -0800966 destination == Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION ||
Vasu Nori5be894e2010-11-02 21:55:30 -0700967 destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_NOROAMING ||
968 destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE) {
969 // return private uri
970 return ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, id);
971 } else {
972 // return public uri
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800973 String path = cursor.getString(
974 cursor.getColumnIndexOrThrow(COLUMN_LOCAL_FILENAME));
975 return Uri.fromFile(new File(path));
Vasu Nori5be894e2010-11-02 21:55:30 -0700976 }
977 }
978 }
979 } finally {
980 if (cursor != null) {
981 cursor.close();
982 }
983 }
984 // downloaded file not found or its status is not 'successfully completed'
985 return null;
986 }
987
988 /**
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800989 * Returns {@link Uri} for the given downloaded file id, if the file is
990 * downloaded successfully. otherwise, null is returned.
991 *<p>
992 * If the specified downloaded file is in external storage (for example, /sdcard dir),
993 * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds
994 * to the filepath on sdcard.
995 *
996 * @param id the id of the downloaded file.
997 * @return the {@link Uri} for the given downloaded file id, if download was successful. null
998 * otherwise.
999 */
1000 public String getMimeTypeForDownloadedFile(long id) {
1001 Query query = new Query().setFilterById(id);
1002 Cursor cursor = null;
1003 try {
1004 cursor = query(query);
1005 if (cursor == null) {
1006 return null;
1007 }
1008 while (cursor.moveToFirst()) {
1009 return cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MEDIA_TYPE));
1010 }
1011 } finally {
1012 if (cursor != null) {
1013 cursor.close();
1014 }
1015 }
1016 // downloaded file not found or its status is not 'successfully completed'
1017 return null;
1018 }
1019
1020 /**
Steve Howard64c48b82010-10-07 17:53:52 -07001021 * Restart the given downloads, which must have already completed (successfully or not). This
Steve Howard90fb15a2010-09-09 16:13:41 -07001022 * method will only work when called from within the download manager's process.
Steve Howard64c48b82010-10-07 17:53:52 -07001023 * @param ids the IDs of the downloads
Steve Howard90fb15a2010-09-09 16:13:41 -07001024 * @hide
1025 */
Steve Howard64c48b82010-10-07 17:53:52 -07001026 public void restartDownload(long... ids) {
1027 Cursor cursor = query(new Query().setFilterById(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -07001028 try {
Steve Howard64c48b82010-10-07 17:53:52 -07001029 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
1030 int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS));
1031 if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) {
1032 throw new IllegalArgumentException("Cannot restart incomplete download: "
1033 + cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
1034 }
Steve Howard90fb15a2010-09-09 16:13:41 -07001035 }
1036 } finally {
1037 cursor.close();
1038 }
1039
1040 ContentValues values = new ContentValues();
1041 values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0);
1042 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1);
1043 values.putNull(Downloads.Impl._DATA);
1044 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
Steve Howard64c48b82010-10-07 17:53:52 -07001045 mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -07001046 }
1047
1048 /**
Vasu Nori0abbf802011-01-17 15:08:14 -08001049 * Returns maximum size, in bytes, of downloads that may go over a mobile connection; or null if
1050 * there's no limit
1051 *
1052 * @param context the {@link Context} to use for accessing the {@link ContentResolver}
1053 * @return maximum size, in bytes, of downloads that may go over a mobile connection; or null if
1054 * there's no limit
1055 */
1056 public static Long getMaxBytesOverMobile(Context context) {
1057 try {
1058 return Settings.Secure.getLong(context.getContentResolver(),
1059 Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE);
1060 } catch (SettingNotFoundException exc) {
1061 return null;
1062 }
1063 }
1064
1065 /**
1066 * Returns recommended maximum size, in bytes, of downloads that may go over a mobile
1067 * connection; or null if there's no recommended limit. The user will have the option to bypass
1068 * this limit.
1069 *
1070 * @param context the {@link Context} to use for accessing the {@link ContentResolver}
1071 * @return recommended maximum size, in bytes, of downloads that may go over a mobile
1072 * connection; or null if there's no recommended limit.
1073 */
1074 public static Long getRecommendedMaxBytesOverMobile(Context context) {
1075 try {
1076 return Settings.Secure.getLong(context.getContentResolver(),
1077 Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE);
1078 } catch (SettingNotFoundException exc) {
1079 return null;
1080 }
1081 }
Vasu Noric0e50752011-01-20 17:57:54 -08001082
1083 /**
1084 * Adds a file to the downloads database system, so it could appear in Downloads App
1085 * (and thus become eligible for management by the Downloads App).
1086 * <p>
1087 * It is helpful to make the file scannable by MediaScanner by setting the param
1088 * isMediaScannerScannable to true. It makes the file visible in media managing
1089 * applications such as Gallery App, which could be a useful purpose of using this API.
1090 *
1091 * @param title the title that would appear for this file in Downloads App.
1092 * @param description the description that would appear for this file in Downloads App.
1093 * @param isMediaScannerScannable true if the file is to be scanned by MediaScanner. Files
1094 * scanned by MediaScanner appear in the applications used to view media (for example,
1095 * Gallery app).
1096 * @param mimeType mimetype of the file.
1097 * @param path absolute pathname to the file. The file should be world-readable, so that it can
1098 * be managed by the Downloads App and any other app that is used to read it (for example,
1099 * Gallery app to display the file, if the file contents represent a video/image).
1100 * @param length length of the downloaded file
1101 * @return an ID for the download entry added to the downloads app, unique across the system
1102 * This ID is used to make future calls related to this download.
1103 */
1104 public long completedDownload(String title, String description,
1105 boolean isMediaScannerScannable, String mimeType, String path, long length) {
1106 // make sure the input args are non-null/non-zero
1107 validateArgumentIsNonEmpty("title", title);
1108 validateArgumentIsNonEmpty("description", description);
1109 validateArgumentIsNonEmpty("path", path);
1110 validateArgumentIsNonEmpty("mimeType", mimeType);
1111 if (length <= 0) {
1112 throw new IllegalArgumentException(" invalid value for param: totalBytes");
1113 }
1114
1115 // if there is already an entry with the given path name in downloads.db, return its id
1116 Request request = new Request(NON_DOWNLOADMANAGER_DOWNLOAD)
1117 .setTitle(title)
1118 .setDescription(description)
1119 .setMimeType(mimeType);
1120 ContentValues values = request.toContentValues(null);
1121 values.put(Downloads.Impl.COLUMN_DESTINATION,
1122 Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD);
1123 values.put(Downloads.Impl._DATA, path);
1124 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_SUCCESS);
1125 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, length);
1126 values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED,
1127 (isMediaScannerScannable) ? Request.SCANNABLE_VALUE_YES :
1128 Request.SCANNABLE_VALUE_NO);
1129 Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values);
1130 if (downloadUri == null) {
1131 return -1;
1132 }
1133 return Long.parseLong(downloadUri.getLastPathSegment());
1134 }
1135 private static final String NON_DOWNLOADMANAGER_DOWNLOAD =
1136 "non-dwnldmngr-download-dont-retry2download";
1137
1138 private static void validateArgumentIsNonEmpty(String paramName, String val) {
1139 if (TextUtils.isEmpty(val)) {
1140 throw new IllegalArgumentException(paramName + " can't be null");
1141 }
1142 }
1143
Vasu Nori0abbf802011-01-17 15:08:14 -08001144 /**
Steve Howarda2709362010-07-02 17:12:48 -07001145 * Get the DownloadProvider URI for the download with the given ID.
1146 */
Steve Howardeca77fc2010-09-12 18:49:08 -07001147 Uri getDownloadUri(long id) {
1148 return ContentUris.withAppendedId(mBaseUri, id);
Steve Howarda2709362010-07-02 17:12:48 -07001149 }
1150
1151 /**
Steve Howard64c48b82010-10-07 17:53:52 -07001152 * Get a parameterized SQL WHERE clause to select a bunch of IDs.
1153 */
1154 static String getWhereClauseForIds(long[] ids) {
1155 StringBuilder whereClause = new StringBuilder();
Vasu Norie7be6bd2010-10-10 14:58:08 -07001156 whereClause.append("(");
Steve Howard64c48b82010-10-07 17:53:52 -07001157 for (int i = 0; i < ids.length; i++) {
1158 if (i > 0) {
Vasu Norie7be6bd2010-10-10 14:58:08 -07001159 whereClause.append("OR ");
Steve Howard64c48b82010-10-07 17:53:52 -07001160 }
Vasu Norie7be6bd2010-10-10 14:58:08 -07001161 whereClause.append(Downloads.Impl._ID);
1162 whereClause.append(" = ? ");
Steve Howard64c48b82010-10-07 17:53:52 -07001163 }
1164 whereClause.append(")");
1165 return whereClause.toString();
1166 }
1167
1168 /**
1169 * Get the selection args for a clause returned by {@link #getWhereClauseForIds(long[])}.
1170 */
1171 static String[] getWhereArgsForIds(long[] ids) {
1172 String[] whereArgs = new String[ids.length];
1173 for (int i = 0; i < ids.length; i++) {
1174 whereArgs[i] = Long.toString(ids[i]);
1175 }
1176 return whereArgs;
1177 }
1178
1179 /**
Steve Howarda2709362010-07-02 17:12:48 -07001180 * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and
1181 * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants.
1182 * Some columns correspond directly to underlying values while others are computed from
1183 * underlying data.
1184 */
1185 private static class CursorTranslator extends CursorWrapper {
Steve Howardeca77fc2010-09-12 18:49:08 -07001186 private Uri mBaseUri;
1187
1188 public CursorTranslator(Cursor cursor, Uri baseUri) {
Steve Howarda2709362010-07-02 17:12:48 -07001189 super(cursor);
Steve Howardeca77fc2010-09-12 18:49:08 -07001190 mBaseUri = baseUri;
Steve Howarda2709362010-07-02 17:12:48 -07001191 }
1192
1193 @Override
Steve Howarda2709362010-07-02 17:12:48 -07001194 public int getInt(int columnIndex) {
1195 return (int) getLong(columnIndex);
1196 }
1197
1198 @Override
1199 public long getLong(int columnIndex) {
Vasu Norie16c43b2010-11-06 18:48:08 -07001200 if (getColumnName(columnIndex).equals(COLUMN_REASON)) {
1201 return getReason(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS)));
1202 } else if (getColumnName(columnIndex).equals(COLUMN_STATUS)) {
1203 return translateStatus(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS)));
1204 } else {
1205 return super.getLong(columnIndex);
1206 }
Steve Howarda2709362010-07-02 17:12:48 -07001207 }
1208
1209 @Override
1210 public String getString(int columnIndex) {
Vasu Norie16c43b2010-11-06 18:48:08 -07001211 return (getColumnName(columnIndex).equals(COLUMN_LOCAL_URI)) ? getLocalUri() :
1212 super.getString(columnIndex);
Steve Howardeca77fc2010-09-12 18:49:08 -07001213 }
1214
1215 private String getLocalUri() {
Vasu Norie16c43b2010-11-06 18:48:08 -07001216 long destinationType = getLong(getColumnIndex(Downloads.Impl.COLUMN_DESTINATION));
1217 if (destinationType == Downloads.Impl.DESTINATION_FILE_URI ||
Vasu Noric0e50752011-01-20 17:57:54 -08001218 destinationType == Downloads.Impl.DESTINATION_EXTERNAL ||
1219 destinationType == Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD) {
Vasu Nori98f03072010-11-15 17:22:20 -08001220 String localPath = getString(getColumnIndex(COLUMN_LOCAL_FILENAME));
Steve Howard99047d72010-09-29 17:41:37 -07001221 if (localPath == null) {
1222 return null;
1223 }
1224 return Uri.fromFile(new File(localPath)).toString();
Steve Howardbb0d23b2010-09-22 18:56:29 -07001225 }
1226
Steve Howardeca77fc2010-09-12 18:49:08 -07001227 // return content URI for cache download
Vasu Norie16c43b2010-11-06 18:48:08 -07001228 long downloadId = getLong(getColumnIndex(Downloads.Impl._ID));
Steve Howardeca77fc2010-09-12 18:49:08 -07001229 return ContentUris.withAppendedId(mBaseUri, downloadId).toString();
Steve Howarda2709362010-07-02 17:12:48 -07001230 }
1231
Steve Howard3e8c1d32010-09-29 17:03:32 -07001232 private long getReason(int status) {
1233 switch (translateStatus(status)) {
1234 case STATUS_FAILED:
1235 return getErrorCode(status);
1236
1237 case STATUS_PAUSED:
1238 return getPausedReason(status);
1239
1240 default:
1241 return 0; // arbitrary value when status is not an error
Steve Howarda2709362010-07-02 17:12:48 -07001242 }
Steve Howard3e8c1d32010-09-29 17:03:32 -07001243 }
1244
1245 private long getPausedReason(int status) {
1246 switch (status) {
1247 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1248 return PAUSED_WAITING_TO_RETRY;
1249
1250 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1251 return PAUSED_WAITING_FOR_NETWORK;
1252
1253 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
1254 return PAUSED_QUEUED_FOR_WIFI;
1255
1256 default:
1257 return PAUSED_UNKNOWN;
1258 }
1259 }
1260
1261 private long getErrorCode(int status) {
Steve Howard33bbd122010-08-02 17:51:29 -07001262 if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS)
1263 || (500 <= status && status < 600)) {
Steve Howarda2709362010-07-02 17:12:48 -07001264 // HTTP status code
1265 return status;
1266 }
1267
1268 switch (status) {
Vasu Norief7e33b2010-10-20 13:26:02 -07001269 case Downloads.Impl.STATUS_FILE_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001270 return ERROR_FILE_ERROR;
1271
Vasu Norief7e33b2010-10-20 13:26:02 -07001272 case Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE:
1273 case Downloads.Impl.STATUS_UNHANDLED_REDIRECT:
Steve Howarda2709362010-07-02 17:12:48 -07001274 return ERROR_UNHANDLED_HTTP_CODE;
1275
Vasu Norief7e33b2010-10-20 13:26:02 -07001276 case Downloads.Impl.STATUS_HTTP_DATA_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001277 return ERROR_HTTP_DATA_ERROR;
1278
Vasu Norief7e33b2010-10-20 13:26:02 -07001279 case Downloads.Impl.STATUS_TOO_MANY_REDIRECTS:
Steve Howarda2709362010-07-02 17:12:48 -07001280 return ERROR_TOO_MANY_REDIRECTS;
1281
Vasu Norief7e33b2010-10-20 13:26:02 -07001282 case Downloads.Impl.STATUS_INSUFFICIENT_SPACE_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001283 return ERROR_INSUFFICIENT_SPACE;
1284
Vasu Norief7e33b2010-10-20 13:26:02 -07001285 case Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001286 return ERROR_DEVICE_NOT_FOUND;
1287
Steve Howard33bbd122010-08-02 17:51:29 -07001288 case Downloads.Impl.STATUS_CANNOT_RESUME:
1289 return ERROR_CANNOT_RESUME;
1290
Steve Howarda9e87c92010-09-16 12:02:03 -07001291 case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR:
1292 return ERROR_FILE_ALREADY_EXISTS;
1293
Steve Howarda2709362010-07-02 17:12:48 -07001294 default:
1295 return ERROR_UNKNOWN;
1296 }
1297 }
1298
Steve Howard3e8c1d32010-09-29 17:03:32 -07001299 private int translateStatus(int status) {
Steve Howarda2709362010-07-02 17:12:48 -07001300 switch (status) {
Vasu Norief7e33b2010-10-20 13:26:02 -07001301 case Downloads.Impl.STATUS_PENDING:
Steve Howarda2709362010-07-02 17:12:48 -07001302 return STATUS_PENDING;
1303
Vasu Norief7e33b2010-10-20 13:26:02 -07001304 case Downloads.Impl.STATUS_RUNNING:
Steve Howarda2709362010-07-02 17:12:48 -07001305 return STATUS_RUNNING;
1306
Steve Howard3e8c1d32010-09-29 17:03:32 -07001307 case Downloads.Impl.STATUS_PAUSED_BY_APP:
1308 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1309 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1310 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
Steve Howarda2709362010-07-02 17:12:48 -07001311 return STATUS_PAUSED;
1312
Vasu Norief7e33b2010-10-20 13:26:02 -07001313 case Downloads.Impl.STATUS_SUCCESS:
Steve Howarda2709362010-07-02 17:12:48 -07001314 return STATUS_SUCCESSFUL;
1315
1316 default:
Vasu Norief7e33b2010-10-20 13:26:02 -07001317 assert Downloads.Impl.isStatusError(status);
Steve Howarda2709362010-07-02 17:12:48 -07001318 return STATUS_FAILED;
1319 }
1320 }
1321 }
1322}