blob: b88e5cf713112e539553f304ccb86910536f244f [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
Vasu Norif9e85232011-02-10 14:59:54 -0800376 /**
377 * This download shows in the notifications after completion ONLY.
378 * It is usuable only with
Vasu Nori37281302011-03-07 11:25:01 -0800379 * {@link DownloadManager#addCompletedDownload(String, String,
380 * boolean, String, String, long, boolean)}.
Vasu Norif9e85232011-02-10 14:59:54 -0800381 */
382 public static final int VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION = 3;
383
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700384 /** can take any of the following values: {@link #VISIBILITY_HIDDEN}
Vasu Norif9e85232011-02-10 14:59:54 -0800385 * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}, {@link #VISIBILITY_VISIBLE},
386 * {@link #VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION}
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700387 */
388 private int mNotificationVisibility = VISIBILITY_VISIBLE;
389
390 /**
Steve Howarda2709362010-07-02 17:12:48 -0700391 * @param uri the HTTP URI to download.
392 */
393 public Request(Uri uri) {
394 if (uri == null) {
395 throw new NullPointerException();
396 }
397 String scheme = uri.getScheme();
Paul Westbrook86a60192010-09-15 12:55:49 -0700398 if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) {
399 throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + uri);
Steve Howarda2709362010-07-02 17:12:48 -0700400 }
401 mUri = uri;
402 }
403
Vasu Noric0e50752011-01-20 17:57:54 -0800404 Request(String uriString) {
405 mUri = Uri.parse(uriString);
406 }
407
Steve Howarda2709362010-07-02 17:12:48 -0700408 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700409 * Set the local destination for the downloaded file. Must be a file URI to a path on
Steve Howarda2709362010-07-02 17:12:48 -0700410 * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE
411 * permission.
Vasu Nori5be894e2010-11-02 21:55:30 -0700412 * <p>
413 * The downloaded file is not scanned by MediaScanner.
414 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
415 * <p>
Steve Howard4f564cd2010-09-22 15:57:25 -0700416 * By default, downloads are saved to a generated filename in the shared download cache and
417 * may be deleted by the system at any time to reclaim space.
Steve Howarda2709362010-07-02 17:12:48 -0700418 *
419 * @return this object
420 */
421 public Request setDestinationUri(Uri uri) {
422 mDestinationUri = uri;
423 return this;
424 }
425
426 /**
Vasu Norif83e6e42010-12-13 16:28:31 -0800427 * Set the local destination for the downloaded file to the system cache dir (/cache).
428 * This is only available to System apps with the permission
429 * {@link android.Manifest.permission#ACCESS_CACHE_FILESYSTEM}.
430 * <p>
431 * The downloaded file is not scanned by MediaScanner.
432 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
433 * <p>
434 * Files downloaded to /cache may be deleted by the system at any time to reclaim space.
435 *
436 * @return this object
437 * @hide
438 */
439 public Request setDestinationToSystemCache() {
440 mUseSystemCache = true;
441 return this;
442 }
443
444 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700445 * Set the local destination for the downloaded file to a path within the application's
446 * external files directory (as returned by {@link Context#getExternalFilesDir(String)}.
Vasu Nori5be894e2010-11-02 21:55:30 -0700447 * <p>
448 * The downloaded file is not scanned by MediaScanner.
449 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
Steve Howard4f564cd2010-09-22 15:57:25 -0700450 *
451 * @param context the {@link Context} to use in determining the external files directory
452 * @param dirType the directory type to pass to {@link Context#getExternalFilesDir(String)}
453 * @param subPath the path within the external directory, including the destination filename
454 * @return this object
455 */
456 public Request setDestinationInExternalFilesDir(Context context, String dirType,
457 String subPath) {
458 setDestinationFromBase(context.getExternalFilesDir(dirType), subPath);
459 return this;
460 }
461
462 /**
463 * Set the local destination for the downloaded file to a path within the public external
464 * storage directory (as returned by
465 * {@link Environment#getExternalStoragePublicDirectory(String)}.
Vasu Nori5be894e2010-11-02 21:55:30 -0700466 *<p>
467 * The downloaded file is not scanned by MediaScanner.
468 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
Steve Howard4f564cd2010-09-22 15:57:25 -0700469 *
470 * @param dirType the directory type to pass to
471 * {@link Environment#getExternalStoragePublicDirectory(String)}
472 * @param subPath the path within the external directory, including the destination filename
473 * @return this object
474 */
475 public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
Vasu Nori6916b032010-12-19 20:31:00 -0800476 File file = Environment.getExternalStoragePublicDirectory(dirType);
477 if (file.exists()) {
478 if (!file.isDirectory()) {
479 throw new IllegalStateException(file.getAbsolutePath() +
480 " already exists and is not a directory");
481 }
482 } else {
483 if (!file.mkdir()) {
484 throw new IllegalStateException("Unable to create directory: "+
485 file.getAbsolutePath());
486 }
487 }
488 setDestinationFromBase(file, subPath);
Steve Howard4f564cd2010-09-22 15:57:25 -0700489 return this;
490 }
491
492 private void setDestinationFromBase(File base, String subPath) {
493 if (subPath == null) {
494 throw new NullPointerException("subPath cannot be null");
495 }
496 mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath);
497 }
498
499 /**
Vasu Nori5be894e2010-11-02 21:55:30 -0700500 * If the file to be downloaded is to be scanned by MediaScanner, this method
501 * should be called before {@link DownloadManager#enqueue(Request)} is called.
502 */
503 public void allowScanningByMediaScanner() {
504 mScannable = true;
505 }
506
507 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700508 * Add an HTTP header to be included with the download request. The header will be added to
509 * the end of the list.
Steve Howarda2709362010-07-02 17:12:48 -0700510 * @param header HTTP header name
511 * @param value header value
512 * @return this object
Steve Howard4f564cd2010-09-22 15:57:25 -0700513 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">HTTP/1.1
514 * Message Headers</a>
Steve Howarda2709362010-07-02 17:12:48 -0700515 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700516 public Request addRequestHeader(String header, String value) {
517 if (header == null) {
518 throw new NullPointerException("header cannot be null");
519 }
520 if (header.contains(":")) {
521 throw new IllegalArgumentException("header may not contain ':'");
522 }
523 if (value == null) {
524 value = "";
525 }
526 mRequestHeaders.add(Pair.create(header, value));
Steve Howarda2709362010-07-02 17:12:48 -0700527 return this;
528 }
529
530 /**
Steve Howard610c4352010-09-30 18:30:04 -0700531 * Set the title of this download, to be displayed in notifications (if enabled). If no
532 * title is given, a default one will be assigned based on the download filename, once the
533 * download starts.
Steve Howarda2709362010-07-02 17:12:48 -0700534 * @return this object
535 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700536 public Request setTitle(CharSequence title) {
Steve Howarda2709362010-07-02 17:12:48 -0700537 mTitle = title;
538 return this;
539 }
540
541 /**
542 * Set a description of this download, to be displayed in notifications (if enabled)
543 * @return this object
544 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700545 public Request setDescription(CharSequence description) {
Steve Howarda2709362010-07-02 17:12:48 -0700546 mDescription = description;
547 return this;
548 }
549
550 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700551 * Set the MIME content type of this download. This will override the content type declared
Steve Howarda2709362010-07-02 17:12:48 -0700552 * in the server's response.
Steve Howard4f564cd2010-09-22 15:57:25 -0700553 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1
554 * Media Types</a>
Steve Howarda2709362010-07-02 17:12:48 -0700555 * @return this object
556 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700557 public Request setMimeType(String mimeType) {
558 mMimeType = mimeType;
Steve Howarda2709362010-07-02 17:12:48 -0700559 return this;
560 }
561
562 /**
Steve Howard8e15afe2010-07-28 17:12:40 -0700563 * Control whether a system notification is posted by the download manager while this
564 * download is running. If enabled, the download manager posts notifications about downloads
565 * through the system {@link android.app.NotificationManager}. By default, a notification is
566 * shown.
Steve Howarda2709362010-07-02 17:12:48 -0700567 *
Steve Howard8e15afe2010-07-28 17:12:40 -0700568 * If set to false, this requires the permission
569 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
570 *
571 * @param show whether the download manager should show a notification for this download.
Steve Howarda2709362010-07-02 17:12:48 -0700572 * @return this object
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700573 * @deprecated use {@link #setNotificationVisibility(int)}
Steve Howarda2709362010-07-02 17:12:48 -0700574 */
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700575 @Deprecated
Steve Howard8e15afe2010-07-28 17:12:40 -0700576 public Request setShowRunningNotification(boolean show) {
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700577 return (show) ? setNotificationVisibility(VISIBILITY_VISIBLE) :
578 setNotificationVisibility(VISIBILITY_HIDDEN);
579 }
580
581 /**
582 * Control whether a system notification is posted by the download manager while this
583 * download is running or when it is completed.
584 * If enabled, the download manager posts notifications about downloads
585 * through the system {@link android.app.NotificationManager}.
586 * By default, a notification is shown only when the download is in progress.
587 *<p>
588 * It can take the following values: {@link #VISIBILITY_HIDDEN},
589 * {@link #VISIBILITY_VISIBLE},
590 * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}.
591 *<p>
592 * If set to {@link #VISIBILITY_HIDDEN}, this requires the permission
593 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
594 *
595 * @param visibility the visibility setting value
596 * @return this object
597 */
598 public Request setNotificationVisibility(int visibility) {
599 mNotificationVisibility = visibility;
Steve Howarda2709362010-07-02 17:12:48 -0700600 return this;
601 }
602
Steve Howardb8e07a52010-07-21 14:53:21 -0700603 /**
604 * Restrict the types of networks over which this download may proceed. By default, all
605 * network types are allowed.
606 * @param flags any combination of the NETWORK_* bit flags.
607 * @return this object
608 */
Steve Howarda2709362010-07-02 17:12:48 -0700609 public Request setAllowedNetworkTypes(int flags) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700610 mAllowedNetworkTypes = flags;
611 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700612 }
613
Steve Howardb8e07a52010-07-21 14:53:21 -0700614 /**
615 * Set whether this download may proceed over a roaming connection. By default, roaming is
616 * allowed.
617 * @param allowed whether to allow a roaming connection to be used
618 * @return this object
619 */
Steve Howarda2709362010-07-02 17:12:48 -0700620 public Request setAllowedOverRoaming(boolean allowed) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700621 mRoamingAllowed = allowed;
622 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700623 }
624
625 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700626 * Set whether this download should be displayed in the system's Downloads UI. True by
627 * default.
628 * @param isVisible whether to display this download in the Downloads UI
629 * @return this object
630 */
631 public Request setVisibleInDownloadsUi(boolean isVisible) {
632 mIsVisibleInDownloadsUi = isVisible;
633 return this;
634 }
635
636 /**
Steve Howarda2709362010-07-02 17:12:48 -0700637 * @return ContentValues to be passed to DownloadProvider.insert()
638 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700639 ContentValues toContentValues(String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700640 ContentValues values = new ContentValues();
641 assert mUri != null;
Vasu Norief7e33b2010-10-20 13:26:02 -0700642 values.put(Downloads.Impl.COLUMN_URI, mUri.toString());
Steve Howardb8e07a52010-07-21 14:53:21 -0700643 values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true);
Vasu Norief7e33b2010-10-20 13:26:02 -0700644 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE, packageName);
Steve Howarda2709362010-07-02 17:12:48 -0700645
646 if (mDestinationUri != null) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700647 values.put(Downloads.Impl.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI);
648 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, mDestinationUri.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700649 } else {
Vasu Norief7e33b2010-10-20 13:26:02 -0700650 values.put(Downloads.Impl.COLUMN_DESTINATION,
Vasu Norif83e6e42010-12-13 16:28:31 -0800651 (this.mUseSystemCache) ?
652 Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION :
653 Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE);
Steve Howarda2709362010-07-02 17:12:48 -0700654 }
Vasu Nori5be894e2010-11-02 21:55:30 -0700655 // is the file supposed to be media-scannable?
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700656 values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED, (mScannable) ? SCANNABLE_VALUE_YES :
657 SCANNABLE_VALUE_NO);
Steve Howarda2709362010-07-02 17:12:48 -0700658
659 if (!mRequestHeaders.isEmpty()) {
Steve Howardea9147d2010-07-13 19:02:45 -0700660 encodeHttpHeaders(values);
Steve Howarda2709362010-07-02 17:12:48 -0700661 }
662
Vasu Norief7e33b2010-10-20 13:26:02 -0700663 putIfNonNull(values, Downloads.Impl.COLUMN_TITLE, mTitle);
664 putIfNonNull(values, Downloads.Impl.COLUMN_DESCRIPTION, mDescription);
665 putIfNonNull(values, Downloads.Impl.COLUMN_MIME_TYPE, mMimeType);
Steve Howarda2709362010-07-02 17:12:48 -0700666
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700667 values.put(Downloads.Impl.COLUMN_VISIBILITY, mNotificationVisibility);
Steve Howardb8e07a52010-07-21 14:53:21 -0700668 values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes);
669 values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed);
Steve Howard90fb15a2010-09-09 16:13:41 -0700670 values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi);
Steve Howardb8e07a52010-07-21 14:53:21 -0700671
Steve Howarda2709362010-07-02 17:12:48 -0700672 return values;
673 }
674
Steve Howardea9147d2010-07-13 19:02:45 -0700675 private void encodeHttpHeaders(ContentValues values) {
676 int index = 0;
Steve Howard4f564cd2010-09-22 15:57:25 -0700677 for (Pair<String, String> header : mRequestHeaders) {
678 String headerString = header.first + ": " + header.second;
Steve Howardea9147d2010-07-13 19:02:45 -0700679 values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString);
680 index++;
681 }
682 }
683
Steve Howard4f564cd2010-09-22 15:57:25 -0700684 private void putIfNonNull(ContentValues contentValues, String key, Object value) {
Steve Howarda2709362010-07-02 17:12:48 -0700685 if (value != null) {
Steve Howard4f564cd2010-09-22 15:57:25 -0700686 contentValues.put(key, value.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700687 }
688 }
689 }
690
691 /**
692 * This class may be used to filter download manager queries.
693 */
694 public static class Query {
Steve Howardf054e192010-09-01 18:26:26 -0700695 /**
696 * Constant for use with {@link #orderBy}
697 * @hide
698 */
699 public static final int ORDER_ASCENDING = 1;
700
701 /**
702 * Constant for use with {@link #orderBy}
703 * @hide
704 */
705 public static final int ORDER_DESCENDING = 2;
706
Steve Howard64c48b82010-10-07 17:53:52 -0700707 private long[] mIds = null;
Steve Howarda2709362010-07-02 17:12:48 -0700708 private Integer mStatusFlags = null;
Vasu Norief7e33b2010-10-20 13:26:02 -0700709 private String mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION;
Steve Howardf054e192010-09-01 18:26:26 -0700710 private int mOrderDirection = ORDER_DESCENDING;
Steve Howard90fb15a2010-09-09 16:13:41 -0700711 private boolean mOnlyIncludeVisibleInDownloadsUi = false;
Steve Howarda2709362010-07-02 17:12:48 -0700712
713 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700714 * Include only the downloads with the given IDs.
Steve Howarda2709362010-07-02 17:12:48 -0700715 * @return this object
716 */
Steve Howard64c48b82010-10-07 17:53:52 -0700717 public Query setFilterById(long... ids) {
718 mIds = ids;
Steve Howarda2709362010-07-02 17:12:48 -0700719 return this;
720 }
721
722 /**
723 * Include only downloads with status matching any the given status flags.
724 * @param flags any combination of the STATUS_* bit flags
725 * @return this object
726 */
727 public Query setFilterByStatus(int flags) {
728 mStatusFlags = flags;
729 return this;
730 }
731
732 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700733 * Controls whether this query includes downloads not visible in the system's Downloads UI.
734 * @param value if true, this query will only include downloads that should be displayed in
735 * the system's Downloads UI; if false (the default), this query will include
736 * both visible and invisible downloads.
737 * @return this object
738 * @hide
739 */
740 public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) {
741 mOnlyIncludeVisibleInDownloadsUi = value;
742 return this;
743 }
744
745 /**
Steve Howardf054e192010-09-01 18:26:26 -0700746 * Change the sort order of the returned Cursor.
747 *
748 * @param column one of the COLUMN_* constants; currently, only
749 * {@link #COLUMN_LAST_MODIFIED_TIMESTAMP} and {@link #COLUMN_TOTAL_SIZE_BYTES} are
750 * supported.
751 * @param direction either {@link #ORDER_ASCENDING} or {@link #ORDER_DESCENDING}
752 * @return this object
753 * @hide
754 */
755 public Query orderBy(String column, int direction) {
756 if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) {
757 throw new IllegalArgumentException("Invalid direction: " + direction);
758 }
759
760 if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700761 mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION;
Steve Howardf054e192010-09-01 18:26:26 -0700762 } else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700763 mOrderByColumn = Downloads.Impl.COLUMN_TOTAL_BYTES;
Steve Howardf054e192010-09-01 18:26:26 -0700764 } else {
765 throw new IllegalArgumentException("Cannot order by " + column);
766 }
767 mOrderDirection = direction;
768 return this;
769 }
770
771 /**
Steve Howarda2709362010-07-02 17:12:48 -0700772 * Run this query using the given ContentResolver.
773 * @param projection the projection to pass to ContentResolver.query()
774 * @return the Cursor returned by ContentResolver.query()
775 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700776 Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) {
777 Uri uri = baseUri;
Steve Howard90fb15a2010-09-09 16:13:41 -0700778 List<String> selectionParts = new ArrayList<String>();
Steve Howard64c48b82010-10-07 17:53:52 -0700779 String[] selectionArgs = null;
Steve Howarda2709362010-07-02 17:12:48 -0700780
Steve Howard64c48b82010-10-07 17:53:52 -0700781 if (mIds != null) {
782 selectionParts.add(getWhereClauseForIds(mIds));
783 selectionArgs = getWhereArgsForIds(mIds);
Steve Howarda2709362010-07-02 17:12:48 -0700784 }
785
786 if (mStatusFlags != null) {
787 List<String> parts = new ArrayList<String>();
788 if ((mStatusFlags & STATUS_PENDING) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700789 parts.add(statusClause("=", Downloads.Impl.STATUS_PENDING));
Steve Howarda2709362010-07-02 17:12:48 -0700790 }
791 if ((mStatusFlags & STATUS_RUNNING) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700792 parts.add(statusClause("=", Downloads.Impl.STATUS_RUNNING));
Steve Howarda2709362010-07-02 17:12:48 -0700793 }
794 if ((mStatusFlags & STATUS_PAUSED) != 0) {
Steve Howard3e8c1d32010-09-29 17:03:32 -0700795 parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP));
796 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY));
797 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK));
798 parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI));
Steve Howarda2709362010-07-02 17:12:48 -0700799 }
800 if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700801 parts.add(statusClause("=", Downloads.Impl.STATUS_SUCCESS));
Steve Howarda2709362010-07-02 17:12:48 -0700802 }
803 if ((mStatusFlags & STATUS_FAILED) != 0) {
804 parts.add("(" + statusClause(">=", 400)
805 + " AND " + statusClause("<", 600) + ")");
806 }
Steve Howard90fb15a2010-09-09 16:13:41 -0700807 selectionParts.add(joinStrings(" OR ", parts));
Steve Howarda2709362010-07-02 17:12:48 -0700808 }
Steve Howardf054e192010-09-01 18:26:26 -0700809
Steve Howard90fb15a2010-09-09 16:13:41 -0700810 if (mOnlyIncludeVisibleInDownloadsUi) {
811 selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'");
812 }
813
Vasu Nori216fa222010-10-12 23:08:13 -0700814 // only return rows which are not marked 'deleted = 1'
815 selectionParts.add(Downloads.Impl.COLUMN_DELETED + " != '1'");
816
Steve Howard90fb15a2010-09-09 16:13:41 -0700817 String selection = joinStrings(" AND ", selectionParts);
Steve Howardf054e192010-09-01 18:26:26 -0700818 String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC");
819 String orderBy = mOrderByColumn + " " + orderDirection;
820
Steve Howard64c48b82010-10-07 17:53:52 -0700821 return resolver.query(uri, projection, selection, selectionArgs, orderBy);
Steve Howarda2709362010-07-02 17:12:48 -0700822 }
823
824 private String joinStrings(String joiner, Iterable<String> parts) {
825 StringBuilder builder = new StringBuilder();
826 boolean first = true;
827 for (String part : parts) {
828 if (!first) {
829 builder.append(joiner);
830 }
831 builder.append(part);
832 first = false;
833 }
834 return builder.toString();
835 }
836
837 private String statusClause(String operator, int value) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700838 return Downloads.Impl.COLUMN_STATUS + operator + "'" + value + "'";
Steve Howarda2709362010-07-02 17:12:48 -0700839 }
840 }
841
842 private ContentResolver mResolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700843 private String mPackageName;
Steve Howardeca77fc2010-09-12 18:49:08 -0700844 private Uri mBaseUri = Downloads.Impl.CONTENT_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700845
846 /**
847 * @hide
848 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700849 public DownloadManager(ContentResolver resolver, String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700850 mResolver = resolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700851 mPackageName = packageName;
Steve Howarda2709362010-07-02 17:12:48 -0700852 }
853
854 /**
Steve Howardeca77fc2010-09-12 18:49:08 -0700855 * Makes this object access the download provider through /all_downloads URIs rather than
856 * /my_downloads URIs, for clients that have permission to do so.
857 * @hide
858 */
859 public void setAccessAllDownloads(boolean accessAllDownloads) {
860 if (accessAllDownloads) {
861 mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI;
862 } else {
863 mBaseUri = Downloads.Impl.CONTENT_URI;
864 }
865 }
866
867 /**
Steve Howarda2709362010-07-02 17:12:48 -0700868 * Enqueue a new download. The download will start automatically once the download manager is
869 * ready to execute it and connectivity is available.
870 *
871 * @param request the parameters specifying this download
872 * @return an ID for the download, unique across the system. This ID is used to make future
873 * calls related to this download.
874 */
875 public long enqueue(Request request) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700876 ContentValues values = request.toContentValues(mPackageName);
Vasu Norief7e33b2010-10-20 13:26:02 -0700877 Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values);
Steve Howarda2709362010-07-02 17:12:48 -0700878 long id = Long.parseLong(downloadUri.getLastPathSegment());
879 return id;
880 }
881
882 /**
Vasu Nori216fa222010-10-12 23:08:13 -0700883 * Marks the specified download as 'to be deleted'. This is done when a completed download
884 * is to be removed but the row was stored without enough info to delete the corresponding
885 * metadata from Mediaprovider database. Actual cleanup of this row is done in DownloadService.
886 *
887 * @param ids the IDs of the downloads to be marked 'deleted'
888 * @return the number of downloads actually updated
889 * @hide
890 */
891 public int markRowDeleted(long... ids) {
892 if (ids == null || ids.length == 0) {
893 // called with nothing to remove!
894 throw new IllegalArgumentException("input param 'ids' can't be null");
895 }
896 ContentValues values = new ContentValues();
897 values.put(Downloads.Impl.COLUMN_DELETED, 1);
Vasu Nori8da7a4e2010-11-17 17:14:49 -0800898 // if only one id is passed in, then include it in the uri itself.
899 // this will eliminate a full database scan in the download service.
900 if (ids.length == 1) {
901 return mResolver.update(ContentUris.withAppendedId(mBaseUri, ids[0]), values,
902 null, null);
903 }
Vasu Nori216fa222010-10-12 23:08:13 -0700904 return mResolver.update(mBaseUri, values, getWhereClauseForIds(ids),
905 getWhereArgsForIds(ids));
906 }
907
908 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700909 * Cancel downloads and remove them from the download manager. Each download will be stopped if
Vasu Nori17ee56c2011-02-28 08:35:39 -0800910 * it was running, and it will no longer be accessible through the download manager.
911 * If there is a downloaded file, partial or complete, it is deleted.
Steve Howarda2709362010-07-02 17:12:48 -0700912 *
Steve Howard64c48b82010-10-07 17:53:52 -0700913 * @param ids the IDs of the downloads to remove
914 * @return the number of downloads actually removed
Steve Howarda2709362010-07-02 17:12:48 -0700915 */
Steve Howard64c48b82010-10-07 17:53:52 -0700916 public int remove(long... ids) {
Vasu Norie16c43b2010-11-06 18:48:08 -0700917 return markRowDeleted(ids);
Steve Howarda2709362010-07-02 17:12:48 -0700918 }
919
920 /**
921 * Query the download manager about downloads that have been requested.
922 * @param query parameters specifying filters for this query
923 * @return a Cursor over the result set of downloads, with columns consisting of all the
924 * COLUMN_* constants.
925 */
926 public Cursor query(Query query) {
Steve Howardeca77fc2010-09-12 18:49:08 -0700927 Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri);
Steve Howardf054e192010-09-01 18:26:26 -0700928 if (underlyingCursor == null) {
929 return null;
930 }
Steve Howardeca77fc2010-09-12 18:49:08 -0700931 return new CursorTranslator(underlyingCursor, mBaseUri);
Steve Howarda2709362010-07-02 17:12:48 -0700932 }
933
934 /**
935 * Open a downloaded file for reading. The download must have completed.
936 * @param id the ID of the download
937 * @return a read-only {@link ParcelFileDescriptor}
938 * @throws FileNotFoundException if the destination file does not already exist
939 */
940 public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException {
941 return mResolver.openFileDescriptor(getDownloadUri(id), "r");
942 }
943
944 /**
Vasu Nori5be894e2010-11-02 21:55:30 -0700945 * Returns {@link Uri} for the given downloaded file id, if the file is
946 * downloaded successfully. otherwise, null is returned.
947 *<p>
948 * If the specified downloaded file is in external storage (for example, /sdcard dir),
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800949 * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds
950 * to the filepath on sdcard.
Vasu Nori5be894e2010-11-02 21:55:30 -0700951 *
952 * @param id the id of the downloaded file.
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700953 * @return the {@link Uri} for the given downloaded file id, if download was successful. null
Vasu Nori5be894e2010-11-02 21:55:30 -0700954 * otherwise.
955 */
956 public Uri getUriForDownloadedFile(long id) {
957 // to check if the file is in cache, get its destination from the database
958 Query query = new Query().setFilterById(id);
959 Cursor cursor = null;
960 try {
961 cursor = query(query);
962 if (cursor == null) {
963 return null;
964 }
Leon Scrogginsd75e64a2010-12-13 15:48:40 -0500965 if (cursor.moveToFirst()) {
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800966 int status = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_STATUS));
Vasu Nori5be894e2010-11-02 21:55:30 -0700967 if (DownloadManager.STATUS_SUCCESSFUL == status) {
968 int indx = cursor.getColumnIndexOrThrow(
969 Downloads.Impl.COLUMN_DESTINATION);
970 int destination = cursor.getInt(indx);
971 // TODO: if we ever add API to DownloadManager to let the caller specify
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700972 // non-external storage for a downloaded file, then the following code
Vasu Nori5be894e2010-11-02 21:55:30 -0700973 // should also check for that destination.
974 if (destination == Downloads.Impl.DESTINATION_CACHE_PARTITION ||
Vasu Norif83e6e42010-12-13 16:28:31 -0800975 destination == Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION ||
Vasu Nori5be894e2010-11-02 21:55:30 -0700976 destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_NOROAMING ||
977 destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE) {
978 // return private uri
979 return ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, id);
980 } else {
981 // return public uri
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800982 String path = cursor.getString(
983 cursor.getColumnIndexOrThrow(COLUMN_LOCAL_FILENAME));
984 return Uri.fromFile(new File(path));
Vasu Nori5be894e2010-11-02 21:55:30 -0700985 }
986 }
987 }
988 } finally {
989 if (cursor != null) {
990 cursor.close();
991 }
992 }
993 // downloaded file not found or its status is not 'successfully completed'
994 return null;
995 }
996
997 /**
Vasu Nori6e2b2a62010-11-16 17:58:22 -0800998 * Returns {@link Uri} for the given downloaded file id, if the file is
999 * downloaded successfully. otherwise, null is returned.
1000 *<p>
1001 * If the specified downloaded file is in external storage (for example, /sdcard dir),
1002 * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds
1003 * to the filepath on sdcard.
1004 *
1005 * @param id the id of the downloaded file.
1006 * @return the {@link Uri} for the given downloaded file id, if download was successful. null
1007 * otherwise.
1008 */
1009 public String getMimeTypeForDownloadedFile(long id) {
1010 Query query = new Query().setFilterById(id);
1011 Cursor cursor = null;
1012 try {
1013 cursor = query(query);
1014 if (cursor == null) {
1015 return null;
1016 }
1017 while (cursor.moveToFirst()) {
1018 return cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MEDIA_TYPE));
1019 }
1020 } finally {
1021 if (cursor != null) {
1022 cursor.close();
1023 }
1024 }
1025 // downloaded file not found or its status is not 'successfully completed'
1026 return null;
1027 }
1028
1029 /**
Steve Howard64c48b82010-10-07 17:53:52 -07001030 * Restart the given downloads, which must have already completed (successfully or not). This
Steve Howard90fb15a2010-09-09 16:13:41 -07001031 * method will only work when called from within the download manager's process.
Steve Howard64c48b82010-10-07 17:53:52 -07001032 * @param ids the IDs of the downloads
Steve Howard90fb15a2010-09-09 16:13:41 -07001033 * @hide
1034 */
Steve Howard64c48b82010-10-07 17:53:52 -07001035 public void restartDownload(long... ids) {
1036 Cursor cursor = query(new Query().setFilterById(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -07001037 try {
Steve Howard64c48b82010-10-07 17:53:52 -07001038 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
1039 int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS));
1040 if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) {
1041 throw new IllegalArgumentException("Cannot restart incomplete download: "
1042 + cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
1043 }
Steve Howard90fb15a2010-09-09 16:13:41 -07001044 }
1045 } finally {
1046 cursor.close();
1047 }
1048
1049 ContentValues values = new ContentValues();
1050 values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0);
1051 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1);
1052 values.putNull(Downloads.Impl._DATA);
1053 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
Steve Howard64c48b82010-10-07 17:53:52 -07001054 mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -07001055 }
1056
1057 /**
Vasu Nori0abbf802011-01-17 15:08:14 -08001058 * Returns maximum size, in bytes, of downloads that may go over a mobile connection; or null if
1059 * there's no limit
1060 *
1061 * @param context the {@link Context} to use for accessing the {@link ContentResolver}
1062 * @return maximum size, in bytes, of downloads that may go over a mobile connection; or null if
1063 * there's no limit
1064 */
1065 public static Long getMaxBytesOverMobile(Context context) {
1066 try {
1067 return Settings.Secure.getLong(context.getContentResolver(),
1068 Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE);
1069 } catch (SettingNotFoundException exc) {
1070 return null;
1071 }
1072 }
1073
1074 /**
1075 * Returns recommended maximum size, in bytes, of downloads that may go over a mobile
1076 * connection; or null if there's no recommended limit. The user will have the option to bypass
1077 * this limit.
1078 *
1079 * @param context the {@link Context} to use for accessing the {@link ContentResolver}
1080 * @return recommended maximum size, in bytes, of downloads that may go over a mobile
1081 * connection; or null if there's no recommended limit.
1082 */
1083 public static Long getRecommendedMaxBytesOverMobile(Context context) {
1084 try {
1085 return Settings.Secure.getLong(context.getContentResolver(),
1086 Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE);
1087 } catch (SettingNotFoundException exc) {
1088 return null;
1089 }
1090 }
Vasu Noric0e50752011-01-20 17:57:54 -08001091
1092 /**
1093 * Adds a file to the downloads database system, so it could appear in Downloads App
1094 * (and thus become eligible for management by the Downloads App).
1095 * <p>
1096 * It is helpful to make the file scannable by MediaScanner by setting the param
1097 * isMediaScannerScannable to true. It makes the file visible in media managing
1098 * applications such as Gallery App, which could be a useful purpose of using this API.
1099 *
1100 * @param title the title that would appear for this file in Downloads App.
1101 * @param description the description that would appear for this file in Downloads App.
1102 * @param isMediaScannerScannable true if the file is to be scanned by MediaScanner. Files
1103 * scanned by MediaScanner appear in the applications used to view media (for example,
1104 * Gallery app).
1105 * @param mimeType mimetype of the file.
1106 * @param path absolute pathname to the file. The file should be world-readable, so that it can
1107 * be managed by the Downloads App and any other app that is used to read it (for example,
1108 * Gallery app to display the file, if the file contents represent a video/image).
1109 * @param length length of the downloaded file
Vasu Norif9e85232011-02-10 14:59:54 -08001110 * @param showNotification true if a notification is to be sent, false otherwise
Vasu Noric0e50752011-01-20 17:57:54 -08001111 * @return an ID for the download entry added to the downloads app, unique across the system
1112 * This ID is used to make future calls related to this download.
1113 */
Vasu Nori37281302011-03-07 11:25:01 -08001114 public long addCompletedDownload(String title, String description,
Vasu Norif9e85232011-02-10 14:59:54 -08001115 boolean isMediaScannerScannable, String mimeType, String path, long length,
1116 boolean showNotification) {
Vasu Noric0e50752011-01-20 17:57:54 -08001117 // make sure the input args are non-null/non-zero
1118 validateArgumentIsNonEmpty("title", title);
1119 validateArgumentIsNonEmpty("description", description);
1120 validateArgumentIsNonEmpty("path", path);
1121 validateArgumentIsNonEmpty("mimeType", mimeType);
1122 if (length <= 0) {
1123 throw new IllegalArgumentException(" invalid value for param: totalBytes");
1124 }
1125
1126 // if there is already an entry with the given path name in downloads.db, return its id
1127 Request request = new Request(NON_DOWNLOADMANAGER_DOWNLOAD)
1128 .setTitle(title)
1129 .setDescription(description)
1130 .setMimeType(mimeType);
1131 ContentValues values = request.toContentValues(null);
1132 values.put(Downloads.Impl.COLUMN_DESTINATION,
1133 Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD);
1134 values.put(Downloads.Impl._DATA, path);
1135 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_SUCCESS);
1136 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, length);
1137 values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED,
1138 (isMediaScannerScannable) ? Request.SCANNABLE_VALUE_YES :
1139 Request.SCANNABLE_VALUE_NO);
Vasu Norif9e85232011-02-10 14:59:54 -08001140 values.put(Downloads.Impl.COLUMN_VISIBILITY, (showNotification) ?
1141 Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION : Request.VISIBILITY_HIDDEN);
Vasu Noric0e50752011-01-20 17:57:54 -08001142 Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values);
1143 if (downloadUri == null) {
1144 return -1;
1145 }
1146 return Long.parseLong(downloadUri.getLastPathSegment());
1147 }
1148 private static final String NON_DOWNLOADMANAGER_DOWNLOAD =
1149 "non-dwnldmngr-download-dont-retry2download";
1150
1151 private static void validateArgumentIsNonEmpty(String paramName, String val) {
1152 if (TextUtils.isEmpty(val)) {
1153 throw new IllegalArgumentException(paramName + " can't be null");
1154 }
1155 }
1156
Vasu Nori0abbf802011-01-17 15:08:14 -08001157 /**
Steve Howarda2709362010-07-02 17:12:48 -07001158 * Get the DownloadProvider URI for the download with the given ID.
1159 */
Steve Howardeca77fc2010-09-12 18:49:08 -07001160 Uri getDownloadUri(long id) {
1161 return ContentUris.withAppendedId(mBaseUri, id);
Steve Howarda2709362010-07-02 17:12:48 -07001162 }
1163
1164 /**
Steve Howard64c48b82010-10-07 17:53:52 -07001165 * Get a parameterized SQL WHERE clause to select a bunch of IDs.
1166 */
1167 static String getWhereClauseForIds(long[] ids) {
1168 StringBuilder whereClause = new StringBuilder();
Vasu Norie7be6bd2010-10-10 14:58:08 -07001169 whereClause.append("(");
Steve Howard64c48b82010-10-07 17:53:52 -07001170 for (int i = 0; i < ids.length; i++) {
1171 if (i > 0) {
Vasu Norie7be6bd2010-10-10 14:58:08 -07001172 whereClause.append("OR ");
Steve Howard64c48b82010-10-07 17:53:52 -07001173 }
Vasu Norie7be6bd2010-10-10 14:58:08 -07001174 whereClause.append(Downloads.Impl._ID);
1175 whereClause.append(" = ? ");
Steve Howard64c48b82010-10-07 17:53:52 -07001176 }
1177 whereClause.append(")");
1178 return whereClause.toString();
1179 }
1180
1181 /**
1182 * Get the selection args for a clause returned by {@link #getWhereClauseForIds(long[])}.
1183 */
1184 static String[] getWhereArgsForIds(long[] ids) {
1185 String[] whereArgs = new String[ids.length];
1186 for (int i = 0; i < ids.length; i++) {
1187 whereArgs[i] = Long.toString(ids[i]);
1188 }
1189 return whereArgs;
1190 }
1191
1192 /**
Steve Howarda2709362010-07-02 17:12:48 -07001193 * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and
1194 * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants.
1195 * Some columns correspond directly to underlying values while others are computed from
1196 * underlying data.
1197 */
1198 private static class CursorTranslator extends CursorWrapper {
Steve Howardeca77fc2010-09-12 18:49:08 -07001199 private Uri mBaseUri;
1200
1201 public CursorTranslator(Cursor cursor, Uri baseUri) {
Steve Howarda2709362010-07-02 17:12:48 -07001202 super(cursor);
Steve Howardeca77fc2010-09-12 18:49:08 -07001203 mBaseUri = baseUri;
Steve Howarda2709362010-07-02 17:12:48 -07001204 }
1205
1206 @Override
Steve Howarda2709362010-07-02 17:12:48 -07001207 public int getInt(int columnIndex) {
1208 return (int) getLong(columnIndex);
1209 }
1210
1211 @Override
1212 public long getLong(int columnIndex) {
Vasu Norie16c43b2010-11-06 18:48:08 -07001213 if (getColumnName(columnIndex).equals(COLUMN_REASON)) {
1214 return getReason(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS)));
1215 } else if (getColumnName(columnIndex).equals(COLUMN_STATUS)) {
1216 return translateStatus(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS)));
1217 } else {
1218 return super.getLong(columnIndex);
1219 }
Steve Howarda2709362010-07-02 17:12:48 -07001220 }
1221
1222 @Override
1223 public String getString(int columnIndex) {
Vasu Norie16c43b2010-11-06 18:48:08 -07001224 return (getColumnName(columnIndex).equals(COLUMN_LOCAL_URI)) ? getLocalUri() :
1225 super.getString(columnIndex);
Steve Howardeca77fc2010-09-12 18:49:08 -07001226 }
1227
1228 private String getLocalUri() {
Vasu Norie16c43b2010-11-06 18:48:08 -07001229 long destinationType = getLong(getColumnIndex(Downloads.Impl.COLUMN_DESTINATION));
1230 if (destinationType == Downloads.Impl.DESTINATION_FILE_URI ||
Vasu Noric0e50752011-01-20 17:57:54 -08001231 destinationType == Downloads.Impl.DESTINATION_EXTERNAL ||
1232 destinationType == Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD) {
Vasu Nori98f03072010-11-15 17:22:20 -08001233 String localPath = getString(getColumnIndex(COLUMN_LOCAL_FILENAME));
Steve Howard99047d72010-09-29 17:41:37 -07001234 if (localPath == null) {
1235 return null;
1236 }
1237 return Uri.fromFile(new File(localPath)).toString();
Steve Howardbb0d23b2010-09-22 18:56:29 -07001238 }
1239
Steve Howardeca77fc2010-09-12 18:49:08 -07001240 // return content URI for cache download
Vasu Norie16c43b2010-11-06 18:48:08 -07001241 long downloadId = getLong(getColumnIndex(Downloads.Impl._ID));
Steve Howardeca77fc2010-09-12 18:49:08 -07001242 return ContentUris.withAppendedId(mBaseUri, downloadId).toString();
Steve Howarda2709362010-07-02 17:12:48 -07001243 }
1244
Steve Howard3e8c1d32010-09-29 17:03:32 -07001245 private long getReason(int status) {
1246 switch (translateStatus(status)) {
1247 case STATUS_FAILED:
1248 return getErrorCode(status);
1249
1250 case STATUS_PAUSED:
1251 return getPausedReason(status);
1252
1253 default:
1254 return 0; // arbitrary value when status is not an error
Steve Howarda2709362010-07-02 17:12:48 -07001255 }
Steve Howard3e8c1d32010-09-29 17:03:32 -07001256 }
1257
1258 private long getPausedReason(int status) {
1259 switch (status) {
1260 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1261 return PAUSED_WAITING_TO_RETRY;
1262
1263 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1264 return PAUSED_WAITING_FOR_NETWORK;
1265
1266 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
1267 return PAUSED_QUEUED_FOR_WIFI;
1268
1269 default:
1270 return PAUSED_UNKNOWN;
1271 }
1272 }
1273
1274 private long getErrorCode(int status) {
Steve Howard33bbd122010-08-02 17:51:29 -07001275 if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS)
1276 || (500 <= status && status < 600)) {
Steve Howarda2709362010-07-02 17:12:48 -07001277 // HTTP status code
1278 return status;
1279 }
1280
1281 switch (status) {
Vasu Norief7e33b2010-10-20 13:26:02 -07001282 case Downloads.Impl.STATUS_FILE_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001283 return ERROR_FILE_ERROR;
1284
Vasu Norief7e33b2010-10-20 13:26:02 -07001285 case Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE:
1286 case Downloads.Impl.STATUS_UNHANDLED_REDIRECT:
Steve Howarda2709362010-07-02 17:12:48 -07001287 return ERROR_UNHANDLED_HTTP_CODE;
1288
Vasu Norief7e33b2010-10-20 13:26:02 -07001289 case Downloads.Impl.STATUS_HTTP_DATA_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001290 return ERROR_HTTP_DATA_ERROR;
1291
Vasu Norief7e33b2010-10-20 13:26:02 -07001292 case Downloads.Impl.STATUS_TOO_MANY_REDIRECTS:
Steve Howarda2709362010-07-02 17:12:48 -07001293 return ERROR_TOO_MANY_REDIRECTS;
1294
Vasu Norief7e33b2010-10-20 13:26:02 -07001295 case Downloads.Impl.STATUS_INSUFFICIENT_SPACE_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001296 return ERROR_INSUFFICIENT_SPACE;
1297
Vasu Norief7e33b2010-10-20 13:26:02 -07001298 case Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001299 return ERROR_DEVICE_NOT_FOUND;
1300
Steve Howard33bbd122010-08-02 17:51:29 -07001301 case Downloads.Impl.STATUS_CANNOT_RESUME:
1302 return ERROR_CANNOT_RESUME;
1303
Steve Howarda9e87c92010-09-16 12:02:03 -07001304 case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR:
1305 return ERROR_FILE_ALREADY_EXISTS;
1306
Steve Howarda2709362010-07-02 17:12:48 -07001307 default:
1308 return ERROR_UNKNOWN;
1309 }
1310 }
1311
Steve Howard3e8c1d32010-09-29 17:03:32 -07001312 private int translateStatus(int status) {
Steve Howarda2709362010-07-02 17:12:48 -07001313 switch (status) {
Vasu Norief7e33b2010-10-20 13:26:02 -07001314 case Downloads.Impl.STATUS_PENDING:
Steve Howarda2709362010-07-02 17:12:48 -07001315 return STATUS_PENDING;
1316
Vasu Norief7e33b2010-10-20 13:26:02 -07001317 case Downloads.Impl.STATUS_RUNNING:
Steve Howarda2709362010-07-02 17:12:48 -07001318 return STATUS_RUNNING;
1319
Steve Howard3e8c1d32010-09-29 17:03:32 -07001320 case Downloads.Impl.STATUS_PAUSED_BY_APP:
1321 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1322 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1323 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
Steve Howarda2709362010-07-02 17:12:48 -07001324 return STATUS_PAUSED;
1325
Vasu Norief7e33b2010-10-20 13:26:02 -07001326 case Downloads.Impl.STATUS_SUCCESS:
Steve Howarda2709362010-07-02 17:12:48 -07001327 return STATUS_SUCCESSFUL;
1328
1329 default:
Vasu Norief7e33b2010-10-20 13:26:02 -07001330 assert Downloads.Impl.isStatusError(status);
Steve Howarda2709362010-07-02 17:12:48 -07001331 return STATUS_FAILED;
1332 }
1333 }
1334 }
1335}