blob: 28559cc5029a663f1771953fe12c15a53158b8e1 [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;
Jeff Sharkey1a303952011-06-16 13:04:20 -070026import android.net.NetworkPolicyManager;
Steve Howardd58429f2010-09-27 16:32:39 -070027import android.net.Uri;
Steve Howard4f564cd2010-09-22 15:57:25 -070028import android.os.Environment;
Steve Howarda2709362010-07-02 17:12:48 -070029import android.os.ParcelFileDescriptor;
30import android.provider.Downloads;
Vasu Nori0abbf802011-01-17 15:08:14 -080031import android.provider.Settings;
32import android.provider.Settings.SettingNotFoundException;
Vasu Noric0e50752011-01-20 17:57:54 -080033import android.text.TextUtils;
Steve Howard4f564cd2010-09-22 15:57:25 -070034import android.util.Pair;
Steve Howarda2709362010-07-02 17:12:48 -070035
Steve Howard4f564cd2010-09-22 15:57:25 -070036import java.io.File;
Steve Howarda2709362010-07-02 17:12:48 -070037import java.io.FileNotFoundException;
38import java.util.ArrayList;
Steve Howarda2709362010-07-02 17:12:48 -070039import java.util.List;
Steve Howarda2709362010-07-02 17:12:48 -070040
41/**
42 * The download manager is a system service that handles long-running HTTP downloads. Clients may
43 * request that a URI be downloaded to a particular destination file. The download manager will
44 * conduct the download in the background, taking care of HTTP interactions and retrying downloads
45 * after failures or across connectivity changes and system reboots.
46 *
47 * Instances of this class should be obtained through
48 * {@link android.content.Context#getSystemService(String)} by passing
49 * {@link android.content.Context#DOWNLOAD_SERVICE}.
Steve Howard610c4352010-09-30 18:30:04 -070050 *
51 * Apps that request downloads through this API should register a broadcast receiver for
52 * {@link #ACTION_NOTIFICATION_CLICKED} to appropriately handle when the user clicks on a running
53 * download in a notification or from the downloads UI.
Steve Howarda2709362010-07-02 17:12:48 -070054 */
55public class DownloadManager {
Vasu Norie7be6bd2010-10-10 14:58:08 -070056
Steve Howarda2709362010-07-02 17:12:48 -070057 /**
58 * An identifier for a particular download, unique across the system. Clients use this ID to
59 * make subsequent calls related to the download.
60 */
Vasu Norief7e33b2010-10-20 13:26:02 -070061 public final static String COLUMN_ID = Downloads.Impl._ID;
Steve Howarda2709362010-07-02 17:12:48 -070062
63 /**
Steve Howard8651bd52010-08-03 12:35:32 -070064 * The client-supplied title for this download. This will be displayed in system notifications.
65 * Defaults to the empty string.
Steve Howarda2709362010-07-02 17:12:48 -070066 */
Vasu Norief7e33b2010-10-20 13:26:02 -070067 public final static String COLUMN_TITLE = Downloads.Impl.COLUMN_TITLE;
Steve Howarda2709362010-07-02 17:12:48 -070068
69 /**
70 * The client-supplied description of this download. This will be displayed in system
Steve Howard8651bd52010-08-03 12:35:32 -070071 * notifications. Defaults to the empty string.
Steve Howarda2709362010-07-02 17:12:48 -070072 */
Vasu Norief7e33b2010-10-20 13:26:02 -070073 public final static String COLUMN_DESCRIPTION = Downloads.Impl.COLUMN_DESCRIPTION;
Steve Howarda2709362010-07-02 17:12:48 -070074
75 /**
76 * URI to be downloaded.
77 */
Vasu Norief7e33b2010-10-20 13:26:02 -070078 public final static String COLUMN_URI = Downloads.Impl.COLUMN_URI;
Steve Howarda2709362010-07-02 17:12:48 -070079
80 /**
Steve Howard8651bd52010-08-03 12:35:32 -070081 * Internet Media Type of the downloaded file. If no value is provided upon creation, this will
82 * initially be null and will be filled in based on the server's response once the download has
83 * started.
Steve Howarda2709362010-07-02 17:12:48 -070084 *
85 * @see <a href="http://www.ietf.org/rfc/rfc1590.txt">RFC 1590, defining Media Types</a>
86 */
87 public final static String COLUMN_MEDIA_TYPE = "media_type";
88
89 /**
Steve Howard8651bd52010-08-03 12:35:32 -070090 * Total size of the download in bytes. This will initially be -1 and will be filled in once
91 * the download starts.
Steve Howarda2709362010-07-02 17:12:48 -070092 */
93 public final static String COLUMN_TOTAL_SIZE_BYTES = "total_size";
94
95 /**
96 * Uri where downloaded file will be stored. If a destination is supplied by client, that URI
Steve Howard8651bd52010-08-03 12:35:32 -070097 * will be used here. Otherwise, the value will initially be null and will be filled in with a
98 * generated URI once the download has started.
Steve Howarda2709362010-07-02 17:12:48 -070099 */
100 public final static String COLUMN_LOCAL_URI = "local_uri";
101
102 /**
Doug Zongkeree04af32010-10-08 13:42:16 -0700103 * The pathname of the file where the download is stored.
104 */
105 public final static String COLUMN_LOCAL_FILENAME = "local_filename";
106
107 /**
Steve Howarda2709362010-07-02 17:12:48 -0700108 * Current status of the download, as one of the STATUS_* constants.
109 */
Vasu Norief7e33b2010-10-20 13:26:02 -0700110 public final static String COLUMN_STATUS = Downloads.Impl.COLUMN_STATUS;
Steve Howarda2709362010-07-02 17:12:48 -0700111
112 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700113 * Provides more detail on the status of the download. Its meaning depends on the value of
114 * {@link #COLUMN_STATUS}.
Steve Howarda2709362010-07-02 17:12:48 -0700115 *
Steve Howard3e8c1d32010-09-29 17:03:32 -0700116 * When {@link #COLUMN_STATUS} is {@link #STATUS_FAILED}, this indicates the type of error that
117 * occurred. If an HTTP error occurred, this will hold the HTTP status code as defined in RFC
118 * 2616. Otherwise, it will hold one of the ERROR_* constants.
119 *
120 * When {@link #COLUMN_STATUS} is {@link #STATUS_PAUSED}, this indicates why the download is
121 * paused. It will hold one of the PAUSED_* constants.
122 *
123 * If {@link #COLUMN_STATUS} is neither {@link #STATUS_FAILED} nor {@link #STATUS_PAUSED}, this
124 * column's value is undefined.
Steve Howarda2709362010-07-02 17:12:48 -0700125 *
126 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1">RFC 2616
127 * status codes</a>
128 */
Steve Howard3e8c1d32010-09-29 17:03:32 -0700129 public final static String COLUMN_REASON = "reason";
Steve Howarda2709362010-07-02 17:12:48 -0700130
131 /**
132 * Number of bytes download so far.
133 */
134 public final static String COLUMN_BYTES_DOWNLOADED_SO_FAR = "bytes_so_far";
135
136 /**
Steve Howardadcb6972010-07-12 17:09:25 -0700137 * Timestamp when the download was last modified, in {@link System#currentTimeMillis
Steve Howarda2709362010-07-02 17:12:48 -0700138 * System.currentTimeMillis()} (wall clock time in UTC).
139 */
Steve Howardadcb6972010-07-12 17:09:25 -0700140 public final static String COLUMN_LAST_MODIFIED_TIMESTAMP = "last_modified_timestamp";
Steve Howarda2709362010-07-02 17:12:48 -0700141
Vasu Nori216fa222010-10-12 23:08:13 -0700142 /**
143 * The URI to the corresponding entry in MediaProvider for this downloaded entry. It is
144 * used to delete the entries from MediaProvider database when it is deleted from the
145 * downloaded list.
146 */
Vasu Norief7e33b2010-10-20 13:26:02 -0700147 public static final String COLUMN_MEDIAPROVIDER_URI = Downloads.Impl.COLUMN_MEDIAPROVIDER_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700148
149 /**
150 * Value of {@link #COLUMN_STATUS} when the download is waiting to start.
151 */
152 public final static int STATUS_PENDING = 1 << 0;
153
154 /**
155 * Value of {@link #COLUMN_STATUS} when the download is currently running.
156 */
157 public final static int STATUS_RUNNING = 1 << 1;
158
159 /**
160 * Value of {@link #COLUMN_STATUS} when the download is waiting to retry or resume.
161 */
162 public final static int STATUS_PAUSED = 1 << 2;
163
164 /**
165 * Value of {@link #COLUMN_STATUS} when the download has successfully completed.
166 */
167 public final static int STATUS_SUCCESSFUL = 1 << 3;
168
169 /**
170 * Value of {@link #COLUMN_STATUS} when the download has failed (and will not be retried).
171 */
172 public final static int STATUS_FAILED = 1 << 4;
173
Steve Howarda2709362010-07-02 17:12:48 -0700174 /**
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 /**
Jeff Sharkey1a303952011-06-16 13:04:20 -0700252 * Value of {@link #COLUMN_REASON} when the download has been paused because
253 * of {@link NetworkPolicyManager} controls on the requesting application.
254 *
255 * @hide
256 */
257 public final static int PAUSED_BY_POLICY = 5;
258
259 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700260 * Broadcast intent action sent by the download manager when a download completes.
261 */
262 public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE";
263
264 /**
Steve Howard610c4352010-09-30 18:30:04 -0700265 * Broadcast intent action sent by the download manager when the user clicks on a running
266 * download, either from a system notification or from the downloads UI.
Steve Howardb8e07a52010-07-21 14:53:21 -0700267 */
268 public final static String ACTION_NOTIFICATION_CLICKED =
269 "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
270
271 /**
Steve Howarde78fc182010-09-24 14:59:36 -0700272 * Intent action to launch an activity to display all downloads.
273 */
274 public final static String ACTION_VIEW_DOWNLOADS = "android.intent.action.VIEW_DOWNLOADS";
275
276 /**
Vasu Norie5f92242011-01-24 16:12:20 -0800277 * Intent extra included with {@link #ACTION_VIEW_DOWNLOADS} to start DownloadApp in
278 * sort-by-size mode.
279 */
280 public final static String INTENT_EXTRAS_SORT_BY_SIZE =
281 "android.app.DownloadManager.extra_sortBySize";
282
283 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700284 * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a
285 * long) of the download that just completed.
286 */
287 public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
Steve Howarda2709362010-07-02 17:12:48 -0700288
Vasu Nori71b8c232010-10-27 15:22:19 -0700289 /**
290 * When clicks on multiple notifications are received, the following
291 * provides an array of download ids corresponding to the download notification that was
292 * clicked. It can be retrieved by the receiver of this
293 * Intent using {@link android.content.Intent#getLongArrayExtra(String)}.
294 */
295 public static final String EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS = "extra_click_download_ids";
296
Vasu Norie16c43b2010-11-06 18:48:08 -0700297 /**
298 * columns to request from DownloadProvider.
299 * @hide
300 */
301 public static final String[] UNDERLYING_COLUMNS = new String[] {
Steve Howarda2709362010-07-02 17:12:48 -0700302 Downloads.Impl._ID,
Vasu Norie69924f2010-11-15 13:10:11 -0800303 Downloads.Impl._DATA + " AS " + COLUMN_LOCAL_FILENAME,
Vasu Nori216fa222010-10-12 23:08:13 -0700304 Downloads.Impl.COLUMN_MEDIAPROVIDER_URI,
Vasu Nori5be894e2010-11-02 21:55:30 -0700305 Downloads.Impl.COLUMN_DESTINATION,
Vasu Norief7e33b2010-10-20 13:26:02 -0700306 Downloads.Impl.COLUMN_TITLE,
307 Downloads.Impl.COLUMN_DESCRIPTION,
308 Downloads.Impl.COLUMN_URI,
Vasu Norief7e33b2010-10-20 13:26:02 -0700309 Downloads.Impl.COLUMN_STATUS,
Steve Howarda9e87c92010-09-16 12:02:03 -0700310 Downloads.Impl.COLUMN_FILE_NAME_HINT,
Vasu Norie16c43b2010-11-06 18:48:08 -0700311 Downloads.Impl.COLUMN_MIME_TYPE + " AS " + COLUMN_MEDIA_TYPE,
312 Downloads.Impl.COLUMN_TOTAL_BYTES + " AS " + COLUMN_TOTAL_SIZE_BYTES,
313 Downloads.Impl.COLUMN_LAST_MODIFICATION + " AS " + COLUMN_LAST_MODIFIED_TIMESTAMP,
314 Downloads.Impl.COLUMN_CURRENT_BYTES + " AS " + COLUMN_BYTES_DOWNLOADED_SO_FAR,
315 /* add the following 'computed' columns to the cursor.
316 * they are not 'returned' by the database, but their inclusion
317 * eliminates need to have lot of methods in CursorTranslator
318 */
319 "'placeholder' AS " + COLUMN_LOCAL_URI,
320 "'placeholder' AS " + COLUMN_REASON
Steve Howarda2709362010-07-02 17:12:48 -0700321 };
322
Steve Howarda2709362010-07-02 17:12:48 -0700323 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700324 * This class contains all the information necessary to request a new download. The URI is the
Steve Howarda2709362010-07-02 17:12:48 -0700325 * only required parameter.
Steve Howard4f564cd2010-09-22 15:57:25 -0700326 *
327 * Note that the default download destination is a shared volume where the system might delete
328 * your file if it needs to reclaim space for system use. If this is a problem, use a location
329 * on external storage (see {@link #setDestinationUri(Uri)}.
Steve Howarda2709362010-07-02 17:12:48 -0700330 */
331 public static class Request {
332 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700333 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
334 * {@link ConnectivityManager#TYPE_MOBILE}.
335 */
336 public static final int NETWORK_MOBILE = 1 << 0;
Steve Howarda2709362010-07-02 17:12:48 -0700337
Steve Howardb8e07a52010-07-21 14:53:21 -0700338 /**
339 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
340 * {@link ConnectivityManager#TYPE_WIFI}.
341 */
342 public static final int NETWORK_WIFI = 1 << 1;
343
Steve Howardb8e07a52010-07-21 14:53:21 -0700344 private Uri mUri;
345 private Uri mDestinationUri;
Steve Howard4f564cd2010-09-22 15:57:25 -0700346 private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>();
347 private CharSequence mTitle;
348 private CharSequence mDescription;
Steve Howard4f564cd2010-09-22 15:57:25 -0700349 private String mMimeType;
Steve Howardb8e07a52010-07-21 14:53:21 -0700350 private boolean mRoamingAllowed = true;
351 private int mAllowedNetworkTypes = ~0; // default to all network types allowed
Steve Howard90fb15a2010-09-09 16:13:41 -0700352 private boolean mIsVisibleInDownloadsUi = true;
Vasu Nori5be894e2010-11-02 21:55:30 -0700353 private boolean mScannable = false;
Vasu Norif83e6e42010-12-13 16:28:31 -0800354 private boolean mUseSystemCache = false;
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700355 /** if a file is designated as a MediaScanner scannable file, the following value is
356 * stored in the database column {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}.
357 */
358 private static final int SCANNABLE_VALUE_YES = 0;
359 // value of 1 is stored in the above column by DownloadProvider after it is scanned by
360 // MediaScanner
361 /** if a file is designated as a file that should not be scanned by MediaScanner,
362 * the following value is stored in the database column
363 * {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}.
364 */
365 private static final int SCANNABLE_VALUE_NO = 2;
Steve Howarda2709362010-07-02 17:12:48 -0700366
367 /**
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700368 * This download is visible but only shows in the notifications
369 * while it's in progress.
370 */
371 public static final int VISIBILITY_VISIBLE = 0;
372
373 /**
374 * This download is visible and shows in the notifications while
375 * in progress and after completion.
376 */
377 public static final int VISIBILITY_VISIBLE_NOTIFY_COMPLETED = 1;
378
379 /**
380 * This download doesn't show in the UI or in the notifications.
381 */
382 public static final int VISIBILITY_HIDDEN = 2;
383
Vasu Norif9e85232011-02-10 14:59:54 -0800384 /**
385 * This download shows in the notifications after completion ONLY.
386 * It is usuable only with
Vasu Nori37281302011-03-07 11:25:01 -0800387 * {@link DownloadManager#addCompletedDownload(String, String,
388 * boolean, String, String, long, boolean)}.
Vasu Norif9e85232011-02-10 14:59:54 -0800389 */
390 public static final int VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION = 3;
391
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700392 /** can take any of the following values: {@link #VISIBILITY_HIDDEN}
Vasu Norif9e85232011-02-10 14:59:54 -0800393 * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}, {@link #VISIBILITY_VISIBLE},
394 * {@link #VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION}
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700395 */
396 private int mNotificationVisibility = VISIBILITY_VISIBLE;
397
398 /**
Steve Howarda2709362010-07-02 17:12:48 -0700399 * @param uri the HTTP URI to download.
400 */
401 public Request(Uri uri) {
402 if (uri == null) {
403 throw new NullPointerException();
404 }
405 String scheme = uri.getScheme();
Paul Westbrook86a60192010-09-15 12:55:49 -0700406 if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) {
407 throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + uri);
Steve Howarda2709362010-07-02 17:12:48 -0700408 }
409 mUri = uri;
410 }
411
Vasu Noric0e50752011-01-20 17:57:54 -0800412 Request(String uriString) {
413 mUri = Uri.parse(uriString);
414 }
415
Steve Howarda2709362010-07-02 17:12:48 -0700416 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700417 * Set the local destination for the downloaded file. Must be a file URI to a path on
Steve Howarda2709362010-07-02 17:12:48 -0700418 * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE
419 * permission.
Vasu Nori5be894e2010-11-02 21:55:30 -0700420 * <p>
421 * The downloaded file is not scanned by MediaScanner.
422 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
423 * <p>
Steve Howard4f564cd2010-09-22 15:57:25 -0700424 * By default, downloads are saved to a generated filename in the shared download cache and
425 * may be deleted by the system at any time to reclaim space.
Steve Howarda2709362010-07-02 17:12:48 -0700426 *
427 * @return this object
428 */
429 public Request setDestinationUri(Uri uri) {
430 mDestinationUri = uri;
431 return this;
432 }
433
434 /**
Vasu Norif83e6e42010-12-13 16:28:31 -0800435 * Set the local destination for the downloaded file to the system cache dir (/cache).
436 * This is only available to System apps with the permission
437 * {@link android.Manifest.permission#ACCESS_CACHE_FILESYSTEM}.
438 * <p>
439 * The downloaded file is not scanned by MediaScanner.
440 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
441 * <p>
442 * Files downloaded to /cache may be deleted by the system at any time to reclaim space.
443 *
444 * @return this object
445 * @hide
446 */
447 public Request setDestinationToSystemCache() {
448 mUseSystemCache = true;
449 return this;
450 }
451
452 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700453 * Set the local destination for the downloaded file to a path within the application's
454 * external files directory (as returned by {@link Context#getExternalFilesDir(String)}.
Vasu Nori5be894e2010-11-02 21:55:30 -0700455 * <p>
456 * The downloaded file is not scanned by MediaScanner.
457 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
Steve Howard4f564cd2010-09-22 15:57:25 -0700458 *
459 * @param context the {@link Context} to use in determining the external files directory
460 * @param dirType the directory type to pass to {@link Context#getExternalFilesDir(String)}
461 * @param subPath the path within the external directory, including the destination filename
462 * @return this object
463 */
464 public Request setDestinationInExternalFilesDir(Context context, String dirType,
465 String subPath) {
466 setDestinationFromBase(context.getExternalFilesDir(dirType), subPath);
467 return this;
468 }
469
470 /**
471 * Set the local destination for the downloaded file to a path within the public external
472 * storage directory (as returned by
473 * {@link Environment#getExternalStoragePublicDirectory(String)}.
Vasu Nori5be894e2010-11-02 21:55:30 -0700474 *<p>
475 * The downloaded file is not scanned by MediaScanner.
476 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
Steve Howard4f564cd2010-09-22 15:57:25 -0700477 *
478 * @param dirType the directory type to pass to
479 * {@link Environment#getExternalStoragePublicDirectory(String)}
480 * @param subPath the path within the external directory, including the destination filename
481 * @return this object
482 */
483 public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
Vasu Nori6916b032010-12-19 20:31:00 -0800484 File file = Environment.getExternalStoragePublicDirectory(dirType);
485 if (file.exists()) {
486 if (!file.isDirectory()) {
487 throw new IllegalStateException(file.getAbsolutePath() +
488 " already exists and is not a directory");
489 }
490 } else {
491 if (!file.mkdir()) {
492 throw new IllegalStateException("Unable to create directory: "+
493 file.getAbsolutePath());
494 }
495 }
496 setDestinationFromBase(file, subPath);
Steve Howard4f564cd2010-09-22 15:57:25 -0700497 return this;
498 }
499
500 private void setDestinationFromBase(File base, String subPath) {
501 if (subPath == null) {
502 throw new NullPointerException("subPath cannot be null");
503 }
504 mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath);
505 }
506
507 /**
Vasu Nori5be894e2010-11-02 21:55:30 -0700508 * If the file to be downloaded is to be scanned by MediaScanner, this method
509 * should be called before {@link DownloadManager#enqueue(Request)} is called.
510 */
511 public void allowScanningByMediaScanner() {
512 mScannable = true;
513 }
514
515 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700516 * Add an HTTP header to be included with the download request. The header will be added to
517 * the end of the list.
Steve Howarda2709362010-07-02 17:12:48 -0700518 * @param header HTTP header name
519 * @param value header value
520 * @return this object
Steve Howard4f564cd2010-09-22 15:57:25 -0700521 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">HTTP/1.1
522 * Message Headers</a>
Steve Howarda2709362010-07-02 17:12:48 -0700523 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700524 public Request addRequestHeader(String header, String value) {
525 if (header == null) {
526 throw new NullPointerException("header cannot be null");
527 }
528 if (header.contains(":")) {
529 throw new IllegalArgumentException("header may not contain ':'");
530 }
531 if (value == null) {
532 value = "";
533 }
534 mRequestHeaders.add(Pair.create(header, value));
Steve Howarda2709362010-07-02 17:12:48 -0700535 return this;
536 }
537
538 /**
Steve Howard610c4352010-09-30 18:30:04 -0700539 * Set the title of this download, to be displayed in notifications (if enabled). If no
540 * title is given, a default one will be assigned based on the download filename, once the
541 * download starts.
Steve Howarda2709362010-07-02 17:12:48 -0700542 * @return this object
543 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700544 public Request setTitle(CharSequence title) {
Steve Howarda2709362010-07-02 17:12:48 -0700545 mTitle = title;
546 return this;
547 }
548
549 /**
550 * Set a description of this download, to be displayed in notifications (if enabled)
551 * @return this object
552 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700553 public Request setDescription(CharSequence description) {
Steve Howarda2709362010-07-02 17:12:48 -0700554 mDescription = description;
555 return this;
556 }
557
558 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700559 * Set the MIME content type of this download. This will override the content type declared
Steve Howarda2709362010-07-02 17:12:48 -0700560 * in the server's response.
Steve Howard4f564cd2010-09-22 15:57:25 -0700561 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1
562 * Media Types</a>
Steve Howarda2709362010-07-02 17:12:48 -0700563 * @return this object
564 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700565 public Request setMimeType(String mimeType) {
566 mMimeType = mimeType;
Steve Howarda2709362010-07-02 17:12:48 -0700567 return this;
568 }
569
570 /**
Steve Howard8e15afe2010-07-28 17:12:40 -0700571 * Control whether a system notification is posted by the download manager while this
572 * download is running. If enabled, the download manager posts notifications about downloads
573 * through the system {@link android.app.NotificationManager}. By default, a notification is
574 * shown.
Steve Howarda2709362010-07-02 17:12:48 -0700575 *
Steve Howard8e15afe2010-07-28 17:12:40 -0700576 * If set to false, this requires the permission
577 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
578 *
579 * @param show whether the download manager should show a notification for this download.
Steve Howarda2709362010-07-02 17:12:48 -0700580 * @return this object
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700581 * @deprecated use {@link #setNotificationVisibility(int)}
Steve Howarda2709362010-07-02 17:12:48 -0700582 */
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700583 @Deprecated
Steve Howard8e15afe2010-07-28 17:12:40 -0700584 public Request setShowRunningNotification(boolean show) {
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700585 return (show) ? setNotificationVisibility(VISIBILITY_VISIBLE) :
586 setNotificationVisibility(VISIBILITY_HIDDEN);
587 }
588
589 /**
590 * Control whether a system notification is posted by the download manager while this
591 * download is running or when it is completed.
592 * If enabled, the download manager posts notifications about downloads
593 * through the system {@link android.app.NotificationManager}.
594 * By default, a notification is shown only when the download is in progress.
595 *<p>
596 * It can take the following values: {@link #VISIBILITY_HIDDEN},
597 * {@link #VISIBILITY_VISIBLE},
598 * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}.
599 *<p>
600 * If set to {@link #VISIBILITY_HIDDEN}, this requires the permission
601 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
602 *
603 * @param visibility the visibility setting value
604 * @return this object
605 */
606 public Request setNotificationVisibility(int visibility) {
607 mNotificationVisibility = visibility;
Steve Howarda2709362010-07-02 17:12:48 -0700608 return this;
609 }
610
Steve Howardb8e07a52010-07-21 14:53:21 -0700611 /**
612 * Restrict the types of networks over which this download may proceed. By default, all
613 * network types are allowed.
614 * @param flags any combination of the NETWORK_* bit flags.
615 * @return this object
616 */
Steve Howarda2709362010-07-02 17:12:48 -0700617 public Request setAllowedNetworkTypes(int flags) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700618 mAllowedNetworkTypes = flags;
619 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700620 }
621
Steve Howardb8e07a52010-07-21 14:53:21 -0700622 /**
623 * Set whether this download may proceed over a roaming connection. By default, roaming is
624 * allowed.
625 * @param allowed whether to allow a roaming connection to be used
626 * @return this object
627 */
Steve Howarda2709362010-07-02 17:12:48 -0700628 public Request setAllowedOverRoaming(boolean allowed) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700629 mRoamingAllowed = allowed;
630 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700631 }
632
633 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700634 * Set whether this download should be displayed in the system's Downloads UI. True by
635 * default.
636 * @param isVisible whether to display this download in the Downloads UI
637 * @return this object
638 */
639 public Request setVisibleInDownloadsUi(boolean isVisible) {
640 mIsVisibleInDownloadsUi = isVisible;
641 return this;
642 }
643
644 /**
Steve Howarda2709362010-07-02 17:12:48 -0700645 * @return ContentValues to be passed to DownloadProvider.insert()
646 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700647 ContentValues toContentValues(String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700648 ContentValues values = new ContentValues();
649 assert mUri != null;
Vasu Norief7e33b2010-10-20 13:26:02 -0700650 values.put(Downloads.Impl.COLUMN_URI, mUri.toString());
Steve Howardb8e07a52010-07-21 14:53:21 -0700651 values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true);
Vasu Norief7e33b2010-10-20 13:26:02 -0700652 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE, packageName);
Steve Howarda2709362010-07-02 17:12:48 -0700653
654 if (mDestinationUri != null) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700655 values.put(Downloads.Impl.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI);
656 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, mDestinationUri.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700657 } else {
Vasu Norief7e33b2010-10-20 13:26:02 -0700658 values.put(Downloads.Impl.COLUMN_DESTINATION,
Vasu Norif83e6e42010-12-13 16:28:31 -0800659 (this.mUseSystemCache) ?
660 Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION :
661 Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE);
Steve Howarda2709362010-07-02 17:12:48 -0700662 }
Vasu Nori5be894e2010-11-02 21:55:30 -0700663 // is the file supposed to be media-scannable?
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700664 values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED, (mScannable) ? SCANNABLE_VALUE_YES :
665 SCANNABLE_VALUE_NO);
Steve Howarda2709362010-07-02 17:12:48 -0700666
667 if (!mRequestHeaders.isEmpty()) {
Steve Howardea9147d2010-07-13 19:02:45 -0700668 encodeHttpHeaders(values);
Steve Howarda2709362010-07-02 17:12:48 -0700669 }
670
Vasu Norief7e33b2010-10-20 13:26:02 -0700671 putIfNonNull(values, Downloads.Impl.COLUMN_TITLE, mTitle);
672 putIfNonNull(values, Downloads.Impl.COLUMN_DESCRIPTION, mDescription);
673 putIfNonNull(values, Downloads.Impl.COLUMN_MIME_TYPE, mMimeType);
Steve Howarda2709362010-07-02 17:12:48 -0700674
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700675 values.put(Downloads.Impl.COLUMN_VISIBILITY, mNotificationVisibility);
Steve Howardb8e07a52010-07-21 14:53:21 -0700676 values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes);
677 values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed);
Steve Howard90fb15a2010-09-09 16:13:41 -0700678 values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi);
Steve Howardb8e07a52010-07-21 14:53:21 -0700679
Steve Howarda2709362010-07-02 17:12:48 -0700680 return values;
681 }
682
Steve Howardea9147d2010-07-13 19:02:45 -0700683 private void encodeHttpHeaders(ContentValues values) {
684 int index = 0;
Steve Howard4f564cd2010-09-22 15:57:25 -0700685 for (Pair<String, String> header : mRequestHeaders) {
686 String headerString = header.first + ": " + header.second;
Steve Howardea9147d2010-07-13 19:02:45 -0700687 values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString);
688 index++;
689 }
690 }
691
Steve Howard4f564cd2010-09-22 15:57:25 -0700692 private void putIfNonNull(ContentValues contentValues, String key, Object value) {
Steve Howarda2709362010-07-02 17:12:48 -0700693 if (value != null) {
Steve Howard4f564cd2010-09-22 15:57:25 -0700694 contentValues.put(key, value.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700695 }
696 }
697 }
698
699 /**
700 * This class may be used to filter download manager queries.
701 */
702 public static class Query {
Steve Howardf054e192010-09-01 18:26:26 -0700703 /**
704 * Constant for use with {@link #orderBy}
705 * @hide
706 */
707 public static final int ORDER_ASCENDING = 1;
708
709 /**
710 * Constant for use with {@link #orderBy}
711 * @hide
712 */
713 public static final int ORDER_DESCENDING = 2;
714
Steve Howard64c48b82010-10-07 17:53:52 -0700715 private long[] mIds = null;
Steve Howarda2709362010-07-02 17:12:48 -0700716 private Integer mStatusFlags = null;
Vasu Norief7e33b2010-10-20 13:26:02 -0700717 private String mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION;
Steve Howardf054e192010-09-01 18:26:26 -0700718 private int mOrderDirection = ORDER_DESCENDING;
Steve Howard90fb15a2010-09-09 16:13:41 -0700719 private boolean mOnlyIncludeVisibleInDownloadsUi = false;
Steve Howarda2709362010-07-02 17:12:48 -0700720
721 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700722 * Include only the downloads with the given IDs.
Steve Howarda2709362010-07-02 17:12:48 -0700723 * @return this object
724 */
Steve Howard64c48b82010-10-07 17:53:52 -0700725 public Query setFilterById(long... ids) {
726 mIds = ids;
Steve Howarda2709362010-07-02 17:12:48 -0700727 return this;
728 }
729
730 /**
731 * Include only downloads with status matching any the given status flags.
732 * @param flags any combination of the STATUS_* bit flags
733 * @return this object
734 */
735 public Query setFilterByStatus(int flags) {
736 mStatusFlags = flags;
737 return this;
738 }
739
740 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700741 * Controls whether this query includes downloads not visible in the system's Downloads UI.
742 * @param value if true, this query will only include downloads that should be displayed in
743 * the system's Downloads UI; if false (the default), this query will include
744 * both visible and invisible downloads.
745 * @return this object
746 * @hide
747 */
748 public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) {
749 mOnlyIncludeVisibleInDownloadsUi = value;
750 return this;
751 }
752
753 /**
Steve Howardf054e192010-09-01 18:26:26 -0700754 * Change the sort order of the returned Cursor.
755 *
756 * @param column one of the COLUMN_* constants; currently, only
757 * {@link #COLUMN_LAST_MODIFIED_TIMESTAMP} and {@link #COLUMN_TOTAL_SIZE_BYTES} are
758 * supported.
759 * @param direction either {@link #ORDER_ASCENDING} or {@link #ORDER_DESCENDING}
760 * @return this object
761 * @hide
762 */
763 public Query orderBy(String column, int direction) {
764 if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) {
765 throw new IllegalArgumentException("Invalid direction: " + direction);
766 }
767
768 if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700769 mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION;
Steve Howardf054e192010-09-01 18:26:26 -0700770 } else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700771 mOrderByColumn = Downloads.Impl.COLUMN_TOTAL_BYTES;
Steve Howardf054e192010-09-01 18:26:26 -0700772 } else {
773 throw new IllegalArgumentException("Cannot order by " + column);
774 }
775 mOrderDirection = direction;
776 return this;
777 }
778
779 /**
Steve Howarda2709362010-07-02 17:12:48 -0700780 * Run this query using the given ContentResolver.
781 * @param projection the projection to pass to ContentResolver.query()
782 * @return the Cursor returned by ContentResolver.query()
783 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700784 Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) {
785 Uri uri = baseUri;
Steve Howard90fb15a2010-09-09 16:13:41 -0700786 List<String> selectionParts = new ArrayList<String>();
Steve Howard64c48b82010-10-07 17:53:52 -0700787 String[] selectionArgs = null;
Steve Howarda2709362010-07-02 17:12:48 -0700788
Steve Howard64c48b82010-10-07 17:53:52 -0700789 if (mIds != null) {
790 selectionParts.add(getWhereClauseForIds(mIds));
791 selectionArgs = getWhereArgsForIds(mIds);
Steve Howarda2709362010-07-02 17:12:48 -0700792 }
793
794 if (mStatusFlags != null) {
795 List<String> parts = new ArrayList<String>();
796 if ((mStatusFlags & STATUS_PENDING) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700797 parts.add(statusClause("=", Downloads.Impl.STATUS_PENDING));
Steve Howarda2709362010-07-02 17:12:48 -0700798 }
799 if ((mStatusFlags & STATUS_RUNNING) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700800 parts.add(statusClause("=", Downloads.Impl.STATUS_RUNNING));
Steve Howarda2709362010-07-02 17:12:48 -0700801 }
802 if ((mStatusFlags & STATUS_PAUSED) != 0) {
Steve Howard3e8c1d32010-09-29 17:03:32 -0700803 parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP));
804 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY));
805 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK));
806 parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI));
Jeff Sharkey1a303952011-06-16 13:04:20 -0700807 parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_POLICY));
Steve Howarda2709362010-07-02 17:12:48 -0700808 }
809 if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700810 parts.add(statusClause("=", Downloads.Impl.STATUS_SUCCESS));
Steve Howarda2709362010-07-02 17:12:48 -0700811 }
812 if ((mStatusFlags & STATUS_FAILED) != 0) {
813 parts.add("(" + statusClause(">=", 400)
814 + " AND " + statusClause("<", 600) + ")");
815 }
Steve Howard90fb15a2010-09-09 16:13:41 -0700816 selectionParts.add(joinStrings(" OR ", parts));
Steve Howarda2709362010-07-02 17:12:48 -0700817 }
Steve Howardf054e192010-09-01 18:26:26 -0700818
Steve Howard90fb15a2010-09-09 16:13:41 -0700819 if (mOnlyIncludeVisibleInDownloadsUi) {
820 selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'");
821 }
822
Vasu Nori216fa222010-10-12 23:08:13 -0700823 // only return rows which are not marked 'deleted = 1'
824 selectionParts.add(Downloads.Impl.COLUMN_DELETED + " != '1'");
825
Steve Howard90fb15a2010-09-09 16:13:41 -0700826 String selection = joinStrings(" AND ", selectionParts);
Steve Howardf054e192010-09-01 18:26:26 -0700827 String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC");
828 String orderBy = mOrderByColumn + " " + orderDirection;
829
Steve Howard64c48b82010-10-07 17:53:52 -0700830 return resolver.query(uri, projection, selection, selectionArgs, orderBy);
Steve Howarda2709362010-07-02 17:12:48 -0700831 }
832
833 private String joinStrings(String joiner, Iterable<String> parts) {
834 StringBuilder builder = new StringBuilder();
835 boolean first = true;
836 for (String part : parts) {
837 if (!first) {
838 builder.append(joiner);
839 }
840 builder.append(part);
841 first = false;
842 }
843 return builder.toString();
844 }
845
846 private String statusClause(String operator, int value) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700847 return Downloads.Impl.COLUMN_STATUS + operator + "'" + value + "'";
Steve Howarda2709362010-07-02 17:12:48 -0700848 }
849 }
850
851 private ContentResolver mResolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700852 private String mPackageName;
Steve Howardeca77fc2010-09-12 18:49:08 -0700853 private Uri mBaseUri = Downloads.Impl.CONTENT_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700854
855 /**
856 * @hide
857 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700858 public DownloadManager(ContentResolver resolver, String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700859 mResolver = resolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700860 mPackageName = packageName;
Steve Howarda2709362010-07-02 17:12:48 -0700861 }
862
863 /**
Steve Howardeca77fc2010-09-12 18:49:08 -0700864 * Makes this object access the download provider through /all_downloads URIs rather than
865 * /my_downloads URIs, for clients that have permission to do so.
866 * @hide
867 */
868 public void setAccessAllDownloads(boolean accessAllDownloads) {
869 if (accessAllDownloads) {
870 mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI;
871 } else {
872 mBaseUri = Downloads.Impl.CONTENT_URI;
873 }
874 }
875
876 /**
Steve Howarda2709362010-07-02 17:12:48 -0700877 * Enqueue a new download. The download will start automatically once the download manager is
878 * ready to execute it and connectivity is available.
879 *
880 * @param request the parameters specifying this download
881 * @return an ID for the download, unique across the system. This ID is used to make future
882 * calls related to this download.
883 */
884 public long enqueue(Request request) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700885 ContentValues values = request.toContentValues(mPackageName);
Vasu Norief7e33b2010-10-20 13:26:02 -0700886 Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values);
Steve Howarda2709362010-07-02 17:12:48 -0700887 long id = Long.parseLong(downloadUri.getLastPathSegment());
888 return id;
889 }
890
891 /**
Vasu Nori216fa222010-10-12 23:08:13 -0700892 * Marks the specified download as 'to be deleted'. This is done when a completed download
893 * is to be removed but the row was stored without enough info to delete the corresponding
894 * metadata from Mediaprovider database. Actual cleanup of this row is done in DownloadService.
895 *
896 * @param ids the IDs of the downloads to be marked 'deleted'
897 * @return the number of downloads actually updated
898 * @hide
899 */
900 public int markRowDeleted(long... ids) {
901 if (ids == null || ids.length == 0) {
902 // called with nothing to remove!
903 throw new IllegalArgumentException("input param 'ids' can't be null");
904 }
905 ContentValues values = new ContentValues();
906 values.put(Downloads.Impl.COLUMN_DELETED, 1);
Vasu Nori8da7a4e2010-11-17 17:14:49 -0800907 // if only one id is passed in, then include it in the uri itself.
908 // this will eliminate a full database scan in the download service.
909 if (ids.length == 1) {
910 return mResolver.update(ContentUris.withAppendedId(mBaseUri, ids[0]), values,
911 null, null);
912 }
Vasu Nori216fa222010-10-12 23:08:13 -0700913 return mResolver.update(mBaseUri, values, getWhereClauseForIds(ids),
914 getWhereArgsForIds(ids));
915 }
916
917 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700918 * Cancel downloads and remove them from the download manager. Each download will be stopped if
Vasu Nori17ee56c2011-02-28 08:35:39 -0800919 * it was running, and it will no longer be accessible through the download manager.
920 * If there is a downloaded file, partial or complete, it is deleted.
Steve Howarda2709362010-07-02 17:12:48 -0700921 *
Steve Howard64c48b82010-10-07 17:53:52 -0700922 * @param ids the IDs of the downloads to remove
923 * @return the number of downloads actually removed
Steve Howarda2709362010-07-02 17:12:48 -0700924 */
Steve Howard64c48b82010-10-07 17:53:52 -0700925 public int remove(long... ids) {
Vasu Norie16c43b2010-11-06 18:48:08 -0700926 return markRowDeleted(ids);
Steve Howarda2709362010-07-02 17:12:48 -0700927 }
928
929 /**
930 * Query the download manager about downloads that have been requested.
931 * @param query parameters specifying filters for this query
932 * @return a Cursor over the result set of downloads, with columns consisting of all the
933 * COLUMN_* constants.
934 */
935 public Cursor query(Query query) {
Steve Howardeca77fc2010-09-12 18:49:08 -0700936 Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri);
Steve Howardf054e192010-09-01 18:26:26 -0700937 if (underlyingCursor == null) {
938 return null;
939 }
Steve Howardeca77fc2010-09-12 18:49:08 -0700940 return new CursorTranslator(underlyingCursor, mBaseUri);
Steve Howarda2709362010-07-02 17:12:48 -0700941 }
942
943 /**
944 * Open a downloaded file for reading. The download must have completed.
945 * @param id the ID of the download
946 * @return a read-only {@link ParcelFileDescriptor}
947 * @throws FileNotFoundException if the destination file does not already exist
948 */
949 public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException {
950 return mResolver.openFileDescriptor(getDownloadUri(id), "r");
951 }
952
953 /**
Vasu Nori5be894e2010-11-02 21:55:30 -0700954 * Returns {@link Uri} for the given downloaded file id, if the file is
955 * downloaded successfully. otherwise, null is returned.
956 *<p>
957 * If the specified downloaded file is in external storage (for example, /sdcard dir),
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800958 * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds
959 * to the filepath on sdcard.
Vasu Nori5be894e2010-11-02 21:55:30 -0700960 *
961 * @param id the id of the downloaded file.
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700962 * @return the {@link Uri} for the given downloaded file id, if download was successful. null
Vasu Nori5be894e2010-11-02 21:55:30 -0700963 * otherwise.
964 */
965 public Uri getUriForDownloadedFile(long id) {
966 // to check if the file is in cache, get its destination from the database
967 Query query = new Query().setFilterById(id);
968 Cursor cursor = null;
969 try {
970 cursor = query(query);
971 if (cursor == null) {
972 return null;
973 }
Leon Scrogginsd75e64a2010-12-13 15:48:40 -0500974 if (cursor.moveToFirst()) {
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800975 int status = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_STATUS));
Vasu Nori5be894e2010-11-02 21:55:30 -0700976 if (DownloadManager.STATUS_SUCCESSFUL == status) {
977 int indx = cursor.getColumnIndexOrThrow(
978 Downloads.Impl.COLUMN_DESTINATION);
979 int destination = cursor.getInt(indx);
980 // TODO: if we ever add API to DownloadManager to let the caller specify
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700981 // non-external storage for a downloaded file, then the following code
Vasu Nori5be894e2010-11-02 21:55:30 -0700982 // should also check for that destination.
983 if (destination == Downloads.Impl.DESTINATION_CACHE_PARTITION ||
Vasu Norif83e6e42010-12-13 16:28:31 -0800984 destination == Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION ||
Vasu Nori5be894e2010-11-02 21:55:30 -0700985 destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_NOROAMING ||
986 destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE) {
987 // return private uri
988 return ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, id);
989 } else {
990 // return public uri
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800991 String path = cursor.getString(
992 cursor.getColumnIndexOrThrow(COLUMN_LOCAL_FILENAME));
993 return Uri.fromFile(new File(path));
Vasu Nori5be894e2010-11-02 21:55:30 -0700994 }
995 }
996 }
997 } finally {
998 if (cursor != null) {
999 cursor.close();
1000 }
1001 }
1002 // downloaded file not found or its status is not 'successfully completed'
1003 return null;
1004 }
1005
1006 /**
Vasu Nori6e2b2a62010-11-16 17:58:22 -08001007 * Returns {@link Uri} for the given downloaded file id, if the file is
1008 * downloaded successfully. otherwise, null is returned.
1009 *<p>
1010 * If the specified downloaded file is in external storage (for example, /sdcard dir),
1011 * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds
1012 * to the filepath on sdcard.
1013 *
1014 * @param id the id of the downloaded file.
1015 * @return the {@link Uri} for the given downloaded file id, if download was successful. null
1016 * otherwise.
1017 */
1018 public String getMimeTypeForDownloadedFile(long id) {
1019 Query query = new Query().setFilterById(id);
1020 Cursor cursor = null;
1021 try {
1022 cursor = query(query);
1023 if (cursor == null) {
1024 return null;
1025 }
1026 while (cursor.moveToFirst()) {
1027 return cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MEDIA_TYPE));
1028 }
1029 } finally {
1030 if (cursor != null) {
1031 cursor.close();
1032 }
1033 }
1034 // downloaded file not found or its status is not 'successfully completed'
1035 return null;
1036 }
1037
1038 /**
Steve Howard64c48b82010-10-07 17:53:52 -07001039 * Restart the given downloads, which must have already completed (successfully or not). This
Steve Howard90fb15a2010-09-09 16:13:41 -07001040 * method will only work when called from within the download manager's process.
Steve Howard64c48b82010-10-07 17:53:52 -07001041 * @param ids the IDs of the downloads
Steve Howard90fb15a2010-09-09 16:13:41 -07001042 * @hide
1043 */
Steve Howard64c48b82010-10-07 17:53:52 -07001044 public void restartDownload(long... ids) {
1045 Cursor cursor = query(new Query().setFilterById(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -07001046 try {
Steve Howard64c48b82010-10-07 17:53:52 -07001047 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
1048 int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS));
1049 if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) {
1050 throw new IllegalArgumentException("Cannot restart incomplete download: "
1051 + cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
1052 }
Steve Howard90fb15a2010-09-09 16:13:41 -07001053 }
1054 } finally {
1055 cursor.close();
1056 }
1057
1058 ContentValues values = new ContentValues();
1059 values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0);
1060 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1);
1061 values.putNull(Downloads.Impl._DATA);
1062 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
Steve Howard64c48b82010-10-07 17:53:52 -07001063 mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -07001064 }
1065
1066 /**
Vasu Nori0abbf802011-01-17 15:08:14 -08001067 * Returns maximum size, in bytes, of downloads that may go over a mobile connection; or null if
1068 * there's no limit
1069 *
1070 * @param context the {@link Context} to use for accessing the {@link ContentResolver}
1071 * @return maximum size, in bytes, of downloads that may go over a mobile connection; or null if
1072 * there's no limit
1073 */
1074 public static Long getMaxBytesOverMobile(Context context) {
1075 try {
1076 return Settings.Secure.getLong(context.getContentResolver(),
1077 Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE);
1078 } catch (SettingNotFoundException exc) {
1079 return null;
1080 }
1081 }
1082
1083 /**
1084 * Returns recommended maximum size, in bytes, of downloads that may go over a mobile
1085 * connection; or null if there's no recommended limit. The user will have the option to bypass
1086 * this limit.
1087 *
1088 * @param context the {@link Context} to use for accessing the {@link ContentResolver}
1089 * @return recommended maximum size, in bytes, of downloads that may go over a mobile
1090 * connection; or null if there's no recommended limit.
1091 */
1092 public static Long getRecommendedMaxBytesOverMobile(Context context) {
1093 try {
1094 return Settings.Secure.getLong(context.getContentResolver(),
1095 Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE);
1096 } catch (SettingNotFoundException exc) {
1097 return null;
1098 }
1099 }
Vasu Noric0e50752011-01-20 17:57:54 -08001100
1101 /**
1102 * Adds a file to the downloads database system, so it could appear in Downloads App
1103 * (and thus become eligible for management by the Downloads App).
1104 * <p>
1105 * It is helpful to make the file scannable by MediaScanner by setting the param
1106 * isMediaScannerScannable to true. It makes the file visible in media managing
1107 * applications such as Gallery App, which could be a useful purpose of using this API.
1108 *
1109 * @param title the title that would appear for this file in Downloads App.
1110 * @param description the description that would appear for this file in Downloads App.
1111 * @param isMediaScannerScannable true if the file is to be scanned by MediaScanner. Files
1112 * scanned by MediaScanner appear in the applications used to view media (for example,
1113 * Gallery app).
1114 * @param mimeType mimetype of the file.
1115 * @param path absolute pathname to the file. The file should be world-readable, so that it can
1116 * be managed by the Downloads App and any other app that is used to read it (for example,
1117 * Gallery app to display the file, if the file contents represent a video/image).
1118 * @param length length of the downloaded file
Vasu Norif9e85232011-02-10 14:59:54 -08001119 * @param showNotification true if a notification is to be sent, false otherwise
Vasu Noric0e50752011-01-20 17:57:54 -08001120 * @return an ID for the download entry added to the downloads app, unique across the system
1121 * This ID is used to make future calls related to this download.
1122 */
Vasu Nori37281302011-03-07 11:25:01 -08001123 public long addCompletedDownload(String title, String description,
Vasu Norif9e85232011-02-10 14:59:54 -08001124 boolean isMediaScannerScannable, String mimeType, String path, long length,
1125 boolean showNotification) {
Vasu Noric0e50752011-01-20 17:57:54 -08001126 // make sure the input args are non-null/non-zero
1127 validateArgumentIsNonEmpty("title", title);
1128 validateArgumentIsNonEmpty("description", description);
1129 validateArgumentIsNonEmpty("path", path);
1130 validateArgumentIsNonEmpty("mimeType", mimeType);
1131 if (length <= 0) {
1132 throw new IllegalArgumentException(" invalid value for param: totalBytes");
1133 }
1134
1135 // if there is already an entry with the given path name in downloads.db, return its id
1136 Request request = new Request(NON_DOWNLOADMANAGER_DOWNLOAD)
1137 .setTitle(title)
1138 .setDescription(description)
1139 .setMimeType(mimeType);
1140 ContentValues values = request.toContentValues(null);
1141 values.put(Downloads.Impl.COLUMN_DESTINATION,
1142 Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD);
1143 values.put(Downloads.Impl._DATA, path);
1144 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_SUCCESS);
1145 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, length);
1146 values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED,
1147 (isMediaScannerScannable) ? Request.SCANNABLE_VALUE_YES :
1148 Request.SCANNABLE_VALUE_NO);
Vasu Norif9e85232011-02-10 14:59:54 -08001149 values.put(Downloads.Impl.COLUMN_VISIBILITY, (showNotification) ?
1150 Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION : Request.VISIBILITY_HIDDEN);
Vasu Noric0e50752011-01-20 17:57:54 -08001151 Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values);
1152 if (downloadUri == null) {
1153 return -1;
1154 }
1155 return Long.parseLong(downloadUri.getLastPathSegment());
1156 }
1157 private static final String NON_DOWNLOADMANAGER_DOWNLOAD =
1158 "non-dwnldmngr-download-dont-retry2download";
1159
1160 private static void validateArgumentIsNonEmpty(String paramName, String val) {
1161 if (TextUtils.isEmpty(val)) {
1162 throw new IllegalArgumentException(paramName + " can't be null");
1163 }
1164 }
1165
Vasu Nori0abbf802011-01-17 15:08:14 -08001166 /**
Steve Howarda2709362010-07-02 17:12:48 -07001167 * Get the DownloadProvider URI for the download with the given ID.
1168 */
Steve Howardeca77fc2010-09-12 18:49:08 -07001169 Uri getDownloadUri(long id) {
1170 return ContentUris.withAppendedId(mBaseUri, id);
Steve Howarda2709362010-07-02 17:12:48 -07001171 }
1172
1173 /**
Steve Howard64c48b82010-10-07 17:53:52 -07001174 * Get a parameterized SQL WHERE clause to select a bunch of IDs.
1175 */
1176 static String getWhereClauseForIds(long[] ids) {
1177 StringBuilder whereClause = new StringBuilder();
Vasu Norie7be6bd2010-10-10 14:58:08 -07001178 whereClause.append("(");
Steve Howard64c48b82010-10-07 17:53:52 -07001179 for (int i = 0; i < ids.length; i++) {
1180 if (i > 0) {
Vasu Norie7be6bd2010-10-10 14:58:08 -07001181 whereClause.append("OR ");
Steve Howard64c48b82010-10-07 17:53:52 -07001182 }
Vasu Norie7be6bd2010-10-10 14:58:08 -07001183 whereClause.append(Downloads.Impl._ID);
1184 whereClause.append(" = ? ");
Steve Howard64c48b82010-10-07 17:53:52 -07001185 }
1186 whereClause.append(")");
1187 return whereClause.toString();
1188 }
1189
1190 /**
1191 * Get the selection args for a clause returned by {@link #getWhereClauseForIds(long[])}.
1192 */
1193 static String[] getWhereArgsForIds(long[] ids) {
1194 String[] whereArgs = new String[ids.length];
1195 for (int i = 0; i < ids.length; i++) {
1196 whereArgs[i] = Long.toString(ids[i]);
1197 }
1198 return whereArgs;
1199 }
1200
1201 /**
Steve Howarda2709362010-07-02 17:12:48 -07001202 * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and
1203 * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants.
1204 * Some columns correspond directly to underlying values while others are computed from
1205 * underlying data.
1206 */
1207 private static class CursorTranslator extends CursorWrapper {
Steve Howardeca77fc2010-09-12 18:49:08 -07001208 private Uri mBaseUri;
1209
1210 public CursorTranslator(Cursor cursor, Uri baseUri) {
Steve Howarda2709362010-07-02 17:12:48 -07001211 super(cursor);
Steve Howardeca77fc2010-09-12 18:49:08 -07001212 mBaseUri = baseUri;
Steve Howarda2709362010-07-02 17:12:48 -07001213 }
1214
1215 @Override
Steve Howarda2709362010-07-02 17:12:48 -07001216 public int getInt(int columnIndex) {
1217 return (int) getLong(columnIndex);
1218 }
1219
1220 @Override
1221 public long getLong(int columnIndex) {
Vasu Norie16c43b2010-11-06 18:48:08 -07001222 if (getColumnName(columnIndex).equals(COLUMN_REASON)) {
1223 return getReason(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS)));
1224 } else if (getColumnName(columnIndex).equals(COLUMN_STATUS)) {
1225 return translateStatus(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS)));
1226 } else {
1227 return super.getLong(columnIndex);
1228 }
Steve Howarda2709362010-07-02 17:12:48 -07001229 }
1230
1231 @Override
1232 public String getString(int columnIndex) {
Vasu Norie16c43b2010-11-06 18:48:08 -07001233 return (getColumnName(columnIndex).equals(COLUMN_LOCAL_URI)) ? getLocalUri() :
1234 super.getString(columnIndex);
Steve Howardeca77fc2010-09-12 18:49:08 -07001235 }
1236
1237 private String getLocalUri() {
Vasu Norie16c43b2010-11-06 18:48:08 -07001238 long destinationType = getLong(getColumnIndex(Downloads.Impl.COLUMN_DESTINATION));
1239 if (destinationType == Downloads.Impl.DESTINATION_FILE_URI ||
Vasu Noric0e50752011-01-20 17:57:54 -08001240 destinationType == Downloads.Impl.DESTINATION_EXTERNAL ||
1241 destinationType == Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD) {
Vasu Nori98f03072010-11-15 17:22:20 -08001242 String localPath = getString(getColumnIndex(COLUMN_LOCAL_FILENAME));
Steve Howard99047d72010-09-29 17:41:37 -07001243 if (localPath == null) {
1244 return null;
1245 }
1246 return Uri.fromFile(new File(localPath)).toString();
Steve Howardbb0d23b2010-09-22 18:56:29 -07001247 }
1248
Steve Howardeca77fc2010-09-12 18:49:08 -07001249 // return content URI for cache download
Vasu Norie16c43b2010-11-06 18:48:08 -07001250 long downloadId = getLong(getColumnIndex(Downloads.Impl._ID));
Steve Howardeca77fc2010-09-12 18:49:08 -07001251 return ContentUris.withAppendedId(mBaseUri, downloadId).toString();
Steve Howarda2709362010-07-02 17:12:48 -07001252 }
1253
Steve Howard3e8c1d32010-09-29 17:03:32 -07001254 private long getReason(int status) {
1255 switch (translateStatus(status)) {
1256 case STATUS_FAILED:
1257 return getErrorCode(status);
1258
1259 case STATUS_PAUSED:
1260 return getPausedReason(status);
1261
1262 default:
1263 return 0; // arbitrary value when status is not an error
Steve Howarda2709362010-07-02 17:12:48 -07001264 }
Steve Howard3e8c1d32010-09-29 17:03:32 -07001265 }
1266
1267 private long getPausedReason(int status) {
1268 switch (status) {
1269 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1270 return PAUSED_WAITING_TO_RETRY;
1271
1272 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1273 return PAUSED_WAITING_FOR_NETWORK;
1274
1275 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
1276 return PAUSED_QUEUED_FOR_WIFI;
1277
Jeff Sharkey1a303952011-06-16 13:04:20 -07001278 case Downloads.Impl.STATUS_PAUSED_BY_POLICY:
1279 return PAUSED_BY_POLICY;
1280
Steve Howard3e8c1d32010-09-29 17:03:32 -07001281 default:
1282 return PAUSED_UNKNOWN;
1283 }
1284 }
1285
1286 private long getErrorCode(int status) {
Steve Howard33bbd122010-08-02 17:51:29 -07001287 if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS)
1288 || (500 <= status && status < 600)) {
Steve Howarda2709362010-07-02 17:12:48 -07001289 // HTTP status code
1290 return status;
1291 }
1292
1293 switch (status) {
Vasu Norief7e33b2010-10-20 13:26:02 -07001294 case Downloads.Impl.STATUS_FILE_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001295 return ERROR_FILE_ERROR;
1296
Vasu Norief7e33b2010-10-20 13:26:02 -07001297 case Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE:
1298 case Downloads.Impl.STATUS_UNHANDLED_REDIRECT:
Steve Howarda2709362010-07-02 17:12:48 -07001299 return ERROR_UNHANDLED_HTTP_CODE;
1300
Vasu Norief7e33b2010-10-20 13:26:02 -07001301 case Downloads.Impl.STATUS_HTTP_DATA_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001302 return ERROR_HTTP_DATA_ERROR;
1303
Vasu Norief7e33b2010-10-20 13:26:02 -07001304 case Downloads.Impl.STATUS_TOO_MANY_REDIRECTS:
Steve Howarda2709362010-07-02 17:12:48 -07001305 return ERROR_TOO_MANY_REDIRECTS;
1306
Vasu Norief7e33b2010-10-20 13:26:02 -07001307 case Downloads.Impl.STATUS_INSUFFICIENT_SPACE_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001308 return ERROR_INSUFFICIENT_SPACE;
1309
Vasu Norief7e33b2010-10-20 13:26:02 -07001310 case Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001311 return ERROR_DEVICE_NOT_FOUND;
1312
Steve Howard33bbd122010-08-02 17:51:29 -07001313 case Downloads.Impl.STATUS_CANNOT_RESUME:
1314 return ERROR_CANNOT_RESUME;
1315
Steve Howarda9e87c92010-09-16 12:02:03 -07001316 case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR:
1317 return ERROR_FILE_ALREADY_EXISTS;
1318
Steve Howarda2709362010-07-02 17:12:48 -07001319 default:
1320 return ERROR_UNKNOWN;
1321 }
1322 }
1323
Steve Howard3e8c1d32010-09-29 17:03:32 -07001324 private int translateStatus(int status) {
Steve Howarda2709362010-07-02 17:12:48 -07001325 switch (status) {
Vasu Norief7e33b2010-10-20 13:26:02 -07001326 case Downloads.Impl.STATUS_PENDING:
Steve Howarda2709362010-07-02 17:12:48 -07001327 return STATUS_PENDING;
1328
Vasu Norief7e33b2010-10-20 13:26:02 -07001329 case Downloads.Impl.STATUS_RUNNING:
Steve Howarda2709362010-07-02 17:12:48 -07001330 return STATUS_RUNNING;
1331
Steve Howard3e8c1d32010-09-29 17:03:32 -07001332 case Downloads.Impl.STATUS_PAUSED_BY_APP:
1333 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1334 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1335 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
Jeff Sharkey1a303952011-06-16 13:04:20 -07001336 case Downloads.Impl.STATUS_PAUSED_BY_POLICY:
Steve Howarda2709362010-07-02 17:12:48 -07001337 return STATUS_PAUSED;
1338
Vasu Norief7e33b2010-10-20 13:26:02 -07001339 case Downloads.Impl.STATUS_SUCCESS:
Steve Howarda2709362010-07-02 17:12:48 -07001340 return STATUS_SUCCESSFUL;
1341
1342 default:
Vasu Norief7e33b2010-10-20 13:26:02 -07001343 assert Downloads.Impl.isStatusError(status);
Steve Howarda2709362010-07-02 17:12:48 -07001344 return STATUS_FAILED;
1345 }
1346 }
1347 }
1348}