blob: 165c3dbd3e0d0419599fa7715715b4a3a8866799 [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.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -070054 *
55 * Note that the application must have the {@link android.Manifest.permission#INTERNET}
56 * permission to use this class.
Steve Howarda2709362010-07-02 17:12:48 -070057 */
58public class DownloadManager {
Vasu Norie7be6bd2010-10-10 14:58:08 -070059
Steve Howarda2709362010-07-02 17:12:48 -070060 /**
61 * An identifier for a particular download, unique across the system. Clients use this ID to
62 * make subsequent calls related to the download.
63 */
Vasu Norief7e33b2010-10-20 13:26:02 -070064 public final static String COLUMN_ID = Downloads.Impl._ID;
Steve Howarda2709362010-07-02 17:12:48 -070065
66 /**
Steve Howard8651bd52010-08-03 12:35:32 -070067 * The client-supplied title for this download. This will be displayed in system notifications.
68 * Defaults to the empty string.
Steve Howarda2709362010-07-02 17:12:48 -070069 */
Vasu Norief7e33b2010-10-20 13:26:02 -070070 public final static String COLUMN_TITLE = Downloads.Impl.COLUMN_TITLE;
Steve Howarda2709362010-07-02 17:12:48 -070071
72 /**
73 * The client-supplied description of this download. This will be displayed in system
Steve Howard8651bd52010-08-03 12:35:32 -070074 * notifications. Defaults to the empty string.
Steve Howarda2709362010-07-02 17:12:48 -070075 */
Vasu Norief7e33b2010-10-20 13:26:02 -070076 public final static String COLUMN_DESCRIPTION = Downloads.Impl.COLUMN_DESCRIPTION;
Steve Howarda2709362010-07-02 17:12:48 -070077
78 /**
79 * URI to be downloaded.
80 */
Vasu Norief7e33b2010-10-20 13:26:02 -070081 public final static String COLUMN_URI = Downloads.Impl.COLUMN_URI;
Steve Howarda2709362010-07-02 17:12:48 -070082
83 /**
Steve Howard8651bd52010-08-03 12:35:32 -070084 * Internet Media Type of the downloaded file. If no value is provided upon creation, this will
85 * initially be null and will be filled in based on the server's response once the download has
86 * started.
Steve Howarda2709362010-07-02 17:12:48 -070087 *
88 * @see <a href="http://www.ietf.org/rfc/rfc1590.txt">RFC 1590, defining Media Types</a>
89 */
90 public final static String COLUMN_MEDIA_TYPE = "media_type";
91
92 /**
Steve Howard8651bd52010-08-03 12:35:32 -070093 * Total size of the download in bytes. This will initially be -1 and will be filled in once
94 * the download starts.
Steve Howarda2709362010-07-02 17:12:48 -070095 */
96 public final static String COLUMN_TOTAL_SIZE_BYTES = "total_size";
97
98 /**
99 * Uri where downloaded file will be stored. If a destination is supplied by client, that URI
Steve Howard8651bd52010-08-03 12:35:32 -0700100 * will be used here. Otherwise, the value will initially be null and will be filled in with a
101 * generated URI once the download has started.
Steve Howarda2709362010-07-02 17:12:48 -0700102 */
103 public final static String COLUMN_LOCAL_URI = "local_uri";
104
105 /**
Doug Zongkeree04af32010-10-08 13:42:16 -0700106 * The pathname of the file where the download is stored.
107 */
108 public final static String COLUMN_LOCAL_FILENAME = "local_filename";
109
110 /**
Steve Howarda2709362010-07-02 17:12:48 -0700111 * Current status of the download, as one of the STATUS_* constants.
112 */
Vasu Norief7e33b2010-10-20 13:26:02 -0700113 public final static String COLUMN_STATUS = Downloads.Impl.COLUMN_STATUS;
Steve Howarda2709362010-07-02 17:12:48 -0700114
115 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700116 * Provides more detail on the status of the download. Its meaning depends on the value of
117 * {@link #COLUMN_STATUS}.
Steve Howarda2709362010-07-02 17:12:48 -0700118 *
Steve Howard3e8c1d32010-09-29 17:03:32 -0700119 * When {@link #COLUMN_STATUS} is {@link #STATUS_FAILED}, this indicates the type of error that
120 * occurred. If an HTTP error occurred, this will hold the HTTP status code as defined in RFC
121 * 2616. Otherwise, it will hold one of the ERROR_* constants.
122 *
123 * When {@link #COLUMN_STATUS} is {@link #STATUS_PAUSED}, this indicates why the download is
124 * paused. It will hold one of the PAUSED_* constants.
125 *
126 * If {@link #COLUMN_STATUS} is neither {@link #STATUS_FAILED} nor {@link #STATUS_PAUSED}, this
127 * column's value is undefined.
Steve Howarda2709362010-07-02 17:12:48 -0700128 *
129 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1">RFC 2616
130 * status codes</a>
131 */
Steve Howard3e8c1d32010-09-29 17:03:32 -0700132 public final static String COLUMN_REASON = "reason";
Steve Howarda2709362010-07-02 17:12:48 -0700133
134 /**
135 * Number of bytes download so far.
136 */
137 public final static String COLUMN_BYTES_DOWNLOADED_SO_FAR = "bytes_so_far";
138
139 /**
Steve Howardadcb6972010-07-12 17:09:25 -0700140 * Timestamp when the download was last modified, in {@link System#currentTimeMillis
Steve Howarda2709362010-07-02 17:12:48 -0700141 * System.currentTimeMillis()} (wall clock time in UTC).
142 */
Steve Howardadcb6972010-07-12 17:09:25 -0700143 public final static String COLUMN_LAST_MODIFIED_TIMESTAMP = "last_modified_timestamp";
Steve Howarda2709362010-07-02 17:12:48 -0700144
Vasu Nori216fa222010-10-12 23:08:13 -0700145 /**
146 * The URI to the corresponding entry in MediaProvider for this downloaded entry. It is
147 * used to delete the entries from MediaProvider database when it is deleted from the
148 * downloaded list.
149 */
Vasu Norief7e33b2010-10-20 13:26:02 -0700150 public static final String COLUMN_MEDIAPROVIDER_URI = Downloads.Impl.COLUMN_MEDIAPROVIDER_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700151
152 /**
153 * Value of {@link #COLUMN_STATUS} when the download is waiting to start.
154 */
155 public final static int STATUS_PENDING = 1 << 0;
156
157 /**
158 * Value of {@link #COLUMN_STATUS} when the download is currently running.
159 */
160 public final static int STATUS_RUNNING = 1 << 1;
161
162 /**
163 * Value of {@link #COLUMN_STATUS} when the download is waiting to retry or resume.
164 */
165 public final static int STATUS_PAUSED = 1 << 2;
166
167 /**
168 * Value of {@link #COLUMN_STATUS} when the download has successfully completed.
169 */
170 public final static int STATUS_SUCCESSFUL = 1 << 3;
171
172 /**
173 * Value of {@link #COLUMN_STATUS} when the download has failed (and will not be retried).
174 */
175 public final static int STATUS_FAILED = 1 << 4;
176
Steve Howarda2709362010-07-02 17:12:48 -0700177 /**
178 * Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit
179 * under any other error code.
180 */
181 public final static int ERROR_UNKNOWN = 1000;
182
183 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700184 * Value of {@link #COLUMN_REASON} when a storage issue arises which doesn't fit under any
Steve Howarda2709362010-07-02 17:12:48 -0700185 * other error code. Use the more specific {@link #ERROR_INSUFFICIENT_SPACE} and
186 * {@link #ERROR_DEVICE_NOT_FOUND} when appropriate.
187 */
188 public final static int ERROR_FILE_ERROR = 1001;
189
190 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700191 * Value of {@link #COLUMN_REASON} when an HTTP code was received that download manager
Steve Howarda2709362010-07-02 17:12:48 -0700192 * can't handle.
193 */
194 public final static int ERROR_UNHANDLED_HTTP_CODE = 1002;
195
196 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700197 * Value of {@link #COLUMN_REASON} when an error receiving or processing data occurred at
Steve Howarda2709362010-07-02 17:12:48 -0700198 * the HTTP level.
199 */
200 public final static int ERROR_HTTP_DATA_ERROR = 1004;
201
202 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700203 * Value of {@link #COLUMN_REASON} when there were too many redirects.
Steve Howarda2709362010-07-02 17:12:48 -0700204 */
205 public final static int ERROR_TOO_MANY_REDIRECTS = 1005;
206
207 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700208 * Value of {@link #COLUMN_REASON} when there was insufficient storage space. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700209 * this is because the SD card is full.
210 */
211 public final static int ERROR_INSUFFICIENT_SPACE = 1006;
212
213 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700214 * Value of {@link #COLUMN_REASON} when no external storage device was found. Typically,
Steve Howarda2709362010-07-02 17:12:48 -0700215 * this is because the SD card is not mounted.
216 */
217 public final static int ERROR_DEVICE_NOT_FOUND = 1007;
218
Steve Howardb8e07a52010-07-21 14:53:21 -0700219 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700220 * Value of {@link #COLUMN_REASON} when some possibly transient error occurred but we can't
Steve Howard33bbd122010-08-02 17:51:29 -0700221 * resume the download.
222 */
223 public final static int ERROR_CANNOT_RESUME = 1008;
224
225 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700226 * Value of {@link #COLUMN_REASON} when the requested destination file already exists (the
Steve Howarda9e87c92010-09-16 12:02:03 -0700227 * download manager will not overwrite an existing file).
228 */
229 public final static int ERROR_FILE_ALREADY_EXISTS = 1009;
230
231 /**
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700232 * Value of {@link #COLUMN_REASON} when the download has failed because of
233 * {@link NetworkPolicyManager} controls on the requesting application.
234 *
235 * @hide
236 */
237 public final static int ERROR_BLOCKED = 1010;
238
239 /**
Steve Howard3e8c1d32010-09-29 17:03:32 -0700240 * Value of {@link #COLUMN_REASON} when the download is paused because some network error
241 * occurred and the download manager is waiting before retrying the request.
242 */
243 public final static int PAUSED_WAITING_TO_RETRY = 1;
244
245 /**
246 * Value of {@link #COLUMN_REASON} when the download is waiting for network connectivity to
247 * proceed.
248 */
249 public final static int PAUSED_WAITING_FOR_NETWORK = 2;
250
251 /**
252 * Value of {@link #COLUMN_REASON} when the download exceeds a size limit for downloads over
253 * the mobile network and the download manager is waiting for a Wi-Fi connection to proceed.
254 */
255 public final static int PAUSED_QUEUED_FOR_WIFI = 3;
256
257 /**
258 * Value of {@link #COLUMN_REASON} when the download is paused for some other reason.
259 */
260 public final static int PAUSED_UNKNOWN = 4;
261
262 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700263 * Broadcast intent action sent by the download manager when a download completes.
264 */
265 public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE";
266
267 /**
Steve Howard610c4352010-09-30 18:30:04 -0700268 * Broadcast intent action sent by the download manager when the user clicks on a running
269 * download, either from a system notification or from the downloads UI.
Steve Howardb8e07a52010-07-21 14:53:21 -0700270 */
271 public final static String ACTION_NOTIFICATION_CLICKED =
272 "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED";
273
274 /**
Steve Howarde78fc182010-09-24 14:59:36 -0700275 * Intent action to launch an activity to display all downloads.
276 */
277 public final static String ACTION_VIEW_DOWNLOADS = "android.intent.action.VIEW_DOWNLOADS";
278
279 /**
Vasu Norie5f92242011-01-24 16:12:20 -0800280 * Intent extra included with {@link #ACTION_VIEW_DOWNLOADS} to start DownloadApp in
281 * sort-by-size mode.
282 */
283 public final static String INTENT_EXTRAS_SORT_BY_SIZE =
284 "android.app.DownloadManager.extra_sortBySize";
285
286 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700287 * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a
288 * long) of the download that just completed.
289 */
290 public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
Steve Howarda2709362010-07-02 17:12:48 -0700291
Vasu Nori71b8c232010-10-27 15:22:19 -0700292 /**
293 * When clicks on multiple notifications are received, the following
294 * provides an array of download ids corresponding to the download notification that was
295 * clicked. It can be retrieved by the receiver of this
296 * Intent using {@link android.content.Intent#getLongArrayExtra(String)}.
297 */
298 public static final String EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS = "extra_click_download_ids";
299
Vasu Norie16c43b2010-11-06 18:48:08 -0700300 /**
301 * columns to request from DownloadProvider.
302 * @hide
303 */
304 public static final String[] UNDERLYING_COLUMNS = new String[] {
Steve Howarda2709362010-07-02 17:12:48 -0700305 Downloads.Impl._ID,
Vasu Norie69924f2010-11-15 13:10:11 -0800306 Downloads.Impl._DATA + " AS " + COLUMN_LOCAL_FILENAME,
Vasu Nori216fa222010-10-12 23:08:13 -0700307 Downloads.Impl.COLUMN_MEDIAPROVIDER_URI,
Vasu Nori5be894e2010-11-02 21:55:30 -0700308 Downloads.Impl.COLUMN_DESTINATION,
Vasu Norief7e33b2010-10-20 13:26:02 -0700309 Downloads.Impl.COLUMN_TITLE,
310 Downloads.Impl.COLUMN_DESCRIPTION,
311 Downloads.Impl.COLUMN_URI,
Vasu Norief7e33b2010-10-20 13:26:02 -0700312 Downloads.Impl.COLUMN_STATUS,
Steve Howarda9e87c92010-09-16 12:02:03 -0700313 Downloads.Impl.COLUMN_FILE_NAME_HINT,
Vasu Norie16c43b2010-11-06 18:48:08 -0700314 Downloads.Impl.COLUMN_MIME_TYPE + " AS " + COLUMN_MEDIA_TYPE,
315 Downloads.Impl.COLUMN_TOTAL_BYTES + " AS " + COLUMN_TOTAL_SIZE_BYTES,
316 Downloads.Impl.COLUMN_LAST_MODIFICATION + " AS " + COLUMN_LAST_MODIFIED_TIMESTAMP,
317 Downloads.Impl.COLUMN_CURRENT_BYTES + " AS " + COLUMN_BYTES_DOWNLOADED_SO_FAR,
318 /* add the following 'computed' columns to the cursor.
319 * they are not 'returned' by the database, but their inclusion
320 * eliminates need to have lot of methods in CursorTranslator
321 */
322 "'placeholder' AS " + COLUMN_LOCAL_URI,
323 "'placeholder' AS " + COLUMN_REASON
Steve Howarda2709362010-07-02 17:12:48 -0700324 };
325
Steve Howarda2709362010-07-02 17:12:48 -0700326 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700327 * This class contains all the information necessary to request a new download. The URI is the
Steve Howarda2709362010-07-02 17:12:48 -0700328 * only required parameter.
Steve Howard4f564cd2010-09-22 15:57:25 -0700329 *
330 * Note that the default download destination is a shared volume where the system might delete
331 * your file if it needs to reclaim space for system use. If this is a problem, use a location
332 * on external storage (see {@link #setDestinationUri(Uri)}.
Steve Howarda2709362010-07-02 17:12:48 -0700333 */
334 public static class Request {
335 /**
Steve Howardb8e07a52010-07-21 14:53:21 -0700336 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
337 * {@link ConnectivityManager#TYPE_MOBILE}.
338 */
339 public static final int NETWORK_MOBILE = 1 << 0;
Steve Howarda2709362010-07-02 17:12:48 -0700340
Steve Howardb8e07a52010-07-21 14:53:21 -0700341 /**
342 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
343 * {@link ConnectivityManager#TYPE_WIFI}.
344 */
345 public static final int NETWORK_WIFI = 1 << 1;
346
HÃ¥kan3 Johansson011238b2012-02-08 21:13:35 +0100347 /**
348 * Bit flag for {@link #setAllowedNetworkTypes} corresponding to
349 * {@link ConnectivityManager#TYPE_BLUETOOTH}.
350 * @hide
351 */
352 public static final int NETWORK_BLUETOOTH = 1 << 2;
353
Steve Howardb8e07a52010-07-21 14:53:21 -0700354 private Uri mUri;
355 private Uri mDestinationUri;
Steve Howard4f564cd2010-09-22 15:57:25 -0700356 private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>();
357 private CharSequence mTitle;
358 private CharSequence mDescription;
Steve Howard4f564cd2010-09-22 15:57:25 -0700359 private String mMimeType;
Steve Howardb8e07a52010-07-21 14:53:21 -0700360 private int mAllowedNetworkTypes = ~0; // default to all network types allowed
Jeff Sharkey15ec7d62012-04-17 12:23:40 -0700361 private boolean mRoamingAllowed = true;
362 private boolean mMeteredAllowed = true;
Steve Howard90fb15a2010-09-09 16:13:41 -0700363 private boolean mIsVisibleInDownloadsUi = true;
Vasu Nori5be894e2010-11-02 21:55:30 -0700364 private boolean mScannable = false;
Vasu Norif83e6e42010-12-13 16:28:31 -0800365 private boolean mUseSystemCache = false;
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700366 /** if a file is designated as a MediaScanner scannable file, the following value is
367 * stored in the database column {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}.
368 */
369 private static final int SCANNABLE_VALUE_YES = 0;
370 // value of 1 is stored in the above column by DownloadProvider after it is scanned by
371 // MediaScanner
372 /** if a file is designated as a file that should not be scanned by MediaScanner,
373 * the following value is stored in the database column
374 * {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}.
375 */
376 private static final int SCANNABLE_VALUE_NO = 2;
Steve Howarda2709362010-07-02 17:12:48 -0700377
378 /**
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700379 * This download is visible but only shows in the notifications
380 * while it's in progress.
381 */
382 public static final int VISIBILITY_VISIBLE = 0;
383
384 /**
385 * This download is visible and shows in the notifications while
386 * in progress and after completion.
387 */
388 public static final int VISIBILITY_VISIBLE_NOTIFY_COMPLETED = 1;
389
390 /**
391 * This download doesn't show in the UI or in the notifications.
392 */
393 public static final int VISIBILITY_HIDDEN = 2;
394
Vasu Norif9e85232011-02-10 14:59:54 -0800395 /**
396 * This download shows in the notifications after completion ONLY.
397 * It is usuable only with
Vasu Nori37281302011-03-07 11:25:01 -0800398 * {@link DownloadManager#addCompletedDownload(String, String,
399 * boolean, String, String, long, boolean)}.
Vasu Norif9e85232011-02-10 14:59:54 -0800400 */
401 public static final int VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION = 3;
402
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700403 /** can take any of the following values: {@link #VISIBILITY_HIDDEN}
Vasu Norif9e85232011-02-10 14:59:54 -0800404 * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}, {@link #VISIBILITY_VISIBLE},
405 * {@link #VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION}
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700406 */
407 private int mNotificationVisibility = VISIBILITY_VISIBLE;
408
409 /**
Steve Howarda2709362010-07-02 17:12:48 -0700410 * @param uri the HTTP URI to download.
411 */
412 public Request(Uri uri) {
413 if (uri == null) {
414 throw new NullPointerException();
415 }
416 String scheme = uri.getScheme();
Paul Westbrook86a60192010-09-15 12:55:49 -0700417 if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) {
418 throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + uri);
Steve Howarda2709362010-07-02 17:12:48 -0700419 }
420 mUri = uri;
421 }
422
Vasu Noric0e50752011-01-20 17:57:54 -0800423 Request(String uriString) {
424 mUri = Uri.parse(uriString);
425 }
426
Steve Howarda2709362010-07-02 17:12:48 -0700427 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700428 * Set the local destination for the downloaded file. Must be a file URI to a path on
Steve Howarda2709362010-07-02 17:12:48 -0700429 * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE
430 * permission.
Vasu Nori5be894e2010-11-02 21:55:30 -0700431 * <p>
432 * The downloaded file is not scanned by MediaScanner.
433 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
434 * <p>
Steve Howard4f564cd2010-09-22 15:57:25 -0700435 * By default, downloads are saved to a generated filename in the shared download cache and
436 * may be deleted by the system at any time to reclaim space.
Steve Howarda2709362010-07-02 17:12:48 -0700437 *
438 * @return this object
439 */
440 public Request setDestinationUri(Uri uri) {
441 mDestinationUri = uri;
442 return this;
443 }
444
445 /**
Vasu Norif83e6e42010-12-13 16:28:31 -0800446 * Set the local destination for the downloaded file to the system cache dir (/cache).
447 * This is only available to System apps with the permission
448 * {@link android.Manifest.permission#ACCESS_CACHE_FILESYSTEM}.
449 * <p>
450 * The downloaded file is not scanned by MediaScanner.
451 * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}.
452 * <p>
453 * Files downloaded to /cache may be deleted by the system at any time to reclaim space.
454 *
455 * @return this object
456 * @hide
457 */
458 public Request setDestinationToSystemCache() {
459 mUseSystemCache = true;
460 return this;
461 }
462
463 /**
Jeff Sharkey45d01ea2013-03-25 17:30:16 -0700464 * Set the local destination for the downloaded file to a path within
465 * the application's external files directory (as returned by
466 * {@link Context#getExternalFilesDir(String)}.
Vasu Nori5be894e2010-11-02 21:55:30 -0700467 * <p>
Jeff Sharkey45d01ea2013-03-25 17:30:16 -0700468 * The downloaded file is not scanned by MediaScanner. But it can be
469 * made scannable by calling {@link #allowScanningByMediaScanner()}.
Steve Howard4f564cd2010-09-22 15:57:25 -0700470 *
Jeff Sharkey45d01ea2013-03-25 17:30:16 -0700471 * @param context the {@link Context} to use in determining the external
472 * files directory
473 * @param dirType the directory type to pass to
474 * {@link Context#getExternalFilesDir(String)}
475 * @param subPath the path within the external directory, including the
476 * destination filename
Steve Howard4f564cd2010-09-22 15:57:25 -0700477 * @return this object
Jeff Sharkey45d01ea2013-03-25 17:30:16 -0700478 * @throws IllegalStateException If the external storage directory
479 * cannot be found or created.
Steve Howard4f564cd2010-09-22 15:57:25 -0700480 */
481 public Request setDestinationInExternalFilesDir(Context context, String dirType,
482 String subPath) {
Jeff Sharkey45d01ea2013-03-25 17:30:16 -0700483 final File file = context.getExternalFilesDir(dirType);
484 if (file == null) {
485 throw new IllegalStateException("Failed to get external storage files directory");
486 } else if (file.exists()) {
487 if (!file.isDirectory()) {
488 throw new IllegalStateException(file.getAbsolutePath() +
489 " already exists and is not a directory");
490 }
491 } else {
492 if (!file.mkdirs()) {
493 throw new IllegalStateException("Unable to create directory: "+
494 file.getAbsolutePath());
495 }
496 }
497 setDestinationFromBase(file, subPath);
Steve Howard4f564cd2010-09-22 15:57:25 -0700498 return this;
499 }
500
501 /**
Jeff Sharkey45d01ea2013-03-25 17:30:16 -0700502 * Set the local destination for the downloaded file to a path within
503 * the public external storage directory (as returned by
504 * {@link Environment#getExternalStoragePublicDirectory(String)}).
505 * <p>
506 * The downloaded file is not scanned by MediaScanner. But it can be
507 * made scannable by calling {@link #allowScanningByMediaScanner()}.
Steve Howard4f564cd2010-09-22 15:57:25 -0700508 *
Jeff Sharkey45d01ea2013-03-25 17:30:16 -0700509 * @param dirType the directory type to pass to {@link Environment#getExternalStoragePublicDirectory(String)}
510 * @param subPath the path within the external directory, including the
511 * destination filename
Steve Howard4f564cd2010-09-22 15:57:25 -0700512 * @return this object
Jeff Sharkey45d01ea2013-03-25 17:30:16 -0700513 * @throws IllegalStateException If the external storage directory
514 * cannot be found or created.
Steve Howard4f564cd2010-09-22 15:57:25 -0700515 */
516 public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
Vasu Nori6916b032010-12-19 20:31:00 -0800517 File file = Environment.getExternalStoragePublicDirectory(dirType);
Jeff Sharkey45d01ea2013-03-25 17:30:16 -0700518 if (file == null) {
519 throw new IllegalStateException("Failed to get external storage public directory");
520 } else if (file.exists()) {
Vasu Nori6916b032010-12-19 20:31:00 -0800521 if (!file.isDirectory()) {
522 throw new IllegalStateException(file.getAbsolutePath() +
523 " already exists and is not a directory");
524 }
525 } else {
Roger Chen1740ada2012-12-17 13:31:17 +0800526 if (!file.mkdirs()) {
Vasu Nori6916b032010-12-19 20:31:00 -0800527 throw new IllegalStateException("Unable to create directory: "+
528 file.getAbsolutePath());
529 }
530 }
531 setDestinationFromBase(file, subPath);
Steve Howard4f564cd2010-09-22 15:57:25 -0700532 return this;
533 }
534
535 private void setDestinationFromBase(File base, String subPath) {
536 if (subPath == null) {
537 throw new NullPointerException("subPath cannot be null");
538 }
539 mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath);
540 }
541
542 /**
Vasu Nori5be894e2010-11-02 21:55:30 -0700543 * If the file to be downloaded is to be scanned by MediaScanner, this method
544 * should be called before {@link DownloadManager#enqueue(Request)} is called.
545 */
546 public void allowScanningByMediaScanner() {
547 mScannable = true;
548 }
549
550 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700551 * Add an HTTP header to be included with the download request. The header will be added to
552 * the end of the list.
Steve Howarda2709362010-07-02 17:12:48 -0700553 * @param header HTTP header name
554 * @param value header value
555 * @return this object
Steve Howard4f564cd2010-09-22 15:57:25 -0700556 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">HTTP/1.1
557 * Message Headers</a>
Steve Howarda2709362010-07-02 17:12:48 -0700558 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700559 public Request addRequestHeader(String header, String value) {
560 if (header == null) {
561 throw new NullPointerException("header cannot be null");
562 }
563 if (header.contains(":")) {
564 throw new IllegalArgumentException("header may not contain ':'");
565 }
566 if (value == null) {
567 value = "";
568 }
569 mRequestHeaders.add(Pair.create(header, value));
Steve Howarda2709362010-07-02 17:12:48 -0700570 return this;
571 }
572
573 /**
Steve Howard610c4352010-09-30 18:30:04 -0700574 * Set the title of this download, to be displayed in notifications (if enabled). If no
575 * title is given, a default one will be assigned based on the download filename, once the
576 * download starts.
Steve Howarda2709362010-07-02 17:12:48 -0700577 * @return this object
578 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700579 public Request setTitle(CharSequence title) {
Steve Howarda2709362010-07-02 17:12:48 -0700580 mTitle = title;
581 return this;
582 }
583
584 /**
585 * Set a description of this download, to be displayed in notifications (if enabled)
586 * @return this object
587 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700588 public Request setDescription(CharSequence description) {
Steve Howarda2709362010-07-02 17:12:48 -0700589 mDescription = description;
590 return this;
591 }
592
593 /**
Steve Howard4f564cd2010-09-22 15:57:25 -0700594 * Set the MIME content type of this download. This will override the content type declared
Steve Howarda2709362010-07-02 17:12:48 -0700595 * in the server's response.
Steve Howard4f564cd2010-09-22 15:57:25 -0700596 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1
597 * Media Types</a>
Steve Howarda2709362010-07-02 17:12:48 -0700598 * @return this object
599 */
Steve Howard4f564cd2010-09-22 15:57:25 -0700600 public Request setMimeType(String mimeType) {
601 mMimeType = mimeType;
Steve Howarda2709362010-07-02 17:12:48 -0700602 return this;
603 }
604
605 /**
Steve Howard8e15afe2010-07-28 17:12:40 -0700606 * Control whether a system notification is posted by the download manager while this
607 * download is running. If enabled, the download manager posts notifications about downloads
608 * through the system {@link android.app.NotificationManager}. By default, a notification is
609 * shown.
Steve Howarda2709362010-07-02 17:12:48 -0700610 *
Steve Howard8e15afe2010-07-28 17:12:40 -0700611 * If set to false, this requires the permission
612 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
613 *
614 * @param show whether the download manager should show a notification for this download.
Steve Howarda2709362010-07-02 17:12:48 -0700615 * @return this object
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700616 * @deprecated use {@link #setNotificationVisibility(int)}
Steve Howarda2709362010-07-02 17:12:48 -0700617 */
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700618 @Deprecated
Steve Howard8e15afe2010-07-28 17:12:40 -0700619 public Request setShowRunningNotification(boolean show) {
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700620 return (show) ? setNotificationVisibility(VISIBILITY_VISIBLE) :
621 setNotificationVisibility(VISIBILITY_HIDDEN);
622 }
623
624 /**
625 * Control whether a system notification is posted by the download manager while this
626 * download is running or when it is completed.
627 * If enabled, the download manager posts notifications about downloads
628 * through the system {@link android.app.NotificationManager}.
629 * By default, a notification is shown only when the download is in progress.
630 *<p>
631 * It can take the following values: {@link #VISIBILITY_HIDDEN},
632 * {@link #VISIBILITY_VISIBLE},
633 * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}.
634 *<p>
635 * If set to {@link #VISIBILITY_HIDDEN}, this requires the permission
636 * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.
637 *
638 * @param visibility the visibility setting value
639 * @return this object
640 */
641 public Request setNotificationVisibility(int visibility) {
642 mNotificationVisibility = visibility;
Steve Howarda2709362010-07-02 17:12:48 -0700643 return this;
644 }
645
Steve Howardb8e07a52010-07-21 14:53:21 -0700646 /**
Jeff Sharkey792e0912012-04-16 11:52:18 -0700647 * Restrict the types of networks over which this download may proceed.
648 * By default, all network types are allowed. Consider using
649 * {@link #setAllowedOverMetered(boolean)} instead, since it's more
650 * flexible.
651 *
Steve Howardb8e07a52010-07-21 14:53:21 -0700652 * @param flags any combination of the NETWORK_* bit flags.
653 * @return this object
654 */
Steve Howarda2709362010-07-02 17:12:48 -0700655 public Request setAllowedNetworkTypes(int flags) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700656 mAllowedNetworkTypes = flags;
657 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700658 }
659
Steve Howardb8e07a52010-07-21 14:53:21 -0700660 /**
661 * Set whether this download may proceed over a roaming connection. By default, roaming is
662 * allowed.
663 * @param allowed whether to allow a roaming connection to be used
664 * @return this object
665 */
Steve Howarda2709362010-07-02 17:12:48 -0700666 public Request setAllowedOverRoaming(boolean allowed) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700667 mRoamingAllowed = allowed;
668 return this;
Steve Howarda2709362010-07-02 17:12:48 -0700669 }
670
671 /**
Jeff Sharkey15ec7d62012-04-17 12:23:40 -0700672 * Set whether this download may proceed over a metered network
673 * connection. By default, metered networks are allowed.
674 *
675 * @see ConnectivityManager#isActiveNetworkMetered()
676 */
677 public Request setAllowedOverMetered(boolean allow) {
678 mMeteredAllowed = allow;
679 return this;
680 }
681
682 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700683 * Set whether this download should be displayed in the system's Downloads UI. True by
684 * default.
685 * @param isVisible whether to display this download in the Downloads UI
686 * @return this object
687 */
688 public Request setVisibleInDownloadsUi(boolean isVisible) {
689 mIsVisibleInDownloadsUi = isVisible;
690 return this;
691 }
692
693 /**
Steve Howarda2709362010-07-02 17:12:48 -0700694 * @return ContentValues to be passed to DownloadProvider.insert()
695 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700696 ContentValues toContentValues(String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700697 ContentValues values = new ContentValues();
698 assert mUri != null;
Vasu Norief7e33b2010-10-20 13:26:02 -0700699 values.put(Downloads.Impl.COLUMN_URI, mUri.toString());
Steve Howardb8e07a52010-07-21 14:53:21 -0700700 values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true);
Vasu Norief7e33b2010-10-20 13:26:02 -0700701 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE, packageName);
Steve Howarda2709362010-07-02 17:12:48 -0700702
703 if (mDestinationUri != null) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700704 values.put(Downloads.Impl.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI);
705 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, mDestinationUri.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700706 } else {
Vasu Norief7e33b2010-10-20 13:26:02 -0700707 values.put(Downloads.Impl.COLUMN_DESTINATION,
Vasu Norif83e6e42010-12-13 16:28:31 -0800708 (this.mUseSystemCache) ?
709 Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION :
710 Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE);
Steve Howarda2709362010-07-02 17:12:48 -0700711 }
Vasu Nori5be894e2010-11-02 21:55:30 -0700712 // is the file supposed to be media-scannable?
Vasu Nori1cde3fb2010-11-05 11:02:52 -0700713 values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED, (mScannable) ? SCANNABLE_VALUE_YES :
714 SCANNABLE_VALUE_NO);
Steve Howarda2709362010-07-02 17:12:48 -0700715
716 if (!mRequestHeaders.isEmpty()) {
Steve Howardea9147d2010-07-13 19:02:45 -0700717 encodeHttpHeaders(values);
Steve Howarda2709362010-07-02 17:12:48 -0700718 }
719
Vasu Norief7e33b2010-10-20 13:26:02 -0700720 putIfNonNull(values, Downloads.Impl.COLUMN_TITLE, mTitle);
721 putIfNonNull(values, Downloads.Impl.COLUMN_DESCRIPTION, mDescription);
722 putIfNonNull(values, Downloads.Impl.COLUMN_MIME_TYPE, mMimeType);
Steve Howarda2709362010-07-02 17:12:48 -0700723
Vasu Nori4c6e5df2010-10-26 17:00:16 -0700724 values.put(Downloads.Impl.COLUMN_VISIBILITY, mNotificationVisibility);
Steve Howardb8e07a52010-07-21 14:53:21 -0700725 values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes);
726 values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed);
Jeff Sharkey15ec7d62012-04-17 12:23:40 -0700727 values.put(Downloads.Impl.COLUMN_ALLOW_METERED, mMeteredAllowed);
Steve Howard90fb15a2010-09-09 16:13:41 -0700728 values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi);
Steve Howardb8e07a52010-07-21 14:53:21 -0700729
Steve Howarda2709362010-07-02 17:12:48 -0700730 return values;
731 }
732
Steve Howardea9147d2010-07-13 19:02:45 -0700733 private void encodeHttpHeaders(ContentValues values) {
734 int index = 0;
Steve Howard4f564cd2010-09-22 15:57:25 -0700735 for (Pair<String, String> header : mRequestHeaders) {
736 String headerString = header.first + ": " + header.second;
Steve Howardea9147d2010-07-13 19:02:45 -0700737 values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString);
738 index++;
739 }
740 }
741
Steve Howard4f564cd2010-09-22 15:57:25 -0700742 private void putIfNonNull(ContentValues contentValues, String key, Object value) {
Steve Howarda2709362010-07-02 17:12:48 -0700743 if (value != null) {
Steve Howard4f564cd2010-09-22 15:57:25 -0700744 contentValues.put(key, value.toString());
Steve Howarda2709362010-07-02 17:12:48 -0700745 }
746 }
747 }
748
749 /**
750 * This class may be used to filter download manager queries.
751 */
752 public static class Query {
Steve Howardf054e192010-09-01 18:26:26 -0700753 /**
754 * Constant for use with {@link #orderBy}
755 * @hide
756 */
757 public static final int ORDER_ASCENDING = 1;
758
759 /**
760 * Constant for use with {@link #orderBy}
761 * @hide
762 */
763 public static final int ORDER_DESCENDING = 2;
764
Steve Howard64c48b82010-10-07 17:53:52 -0700765 private long[] mIds = null;
Steve Howarda2709362010-07-02 17:12:48 -0700766 private Integer mStatusFlags = null;
Vasu Norief7e33b2010-10-20 13:26:02 -0700767 private String mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION;
Steve Howardf054e192010-09-01 18:26:26 -0700768 private int mOrderDirection = ORDER_DESCENDING;
Steve Howard90fb15a2010-09-09 16:13:41 -0700769 private boolean mOnlyIncludeVisibleInDownloadsUi = false;
Steve Howarda2709362010-07-02 17:12:48 -0700770
771 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700772 * Include only the downloads with the given IDs.
Steve Howarda2709362010-07-02 17:12:48 -0700773 * @return this object
774 */
Steve Howard64c48b82010-10-07 17:53:52 -0700775 public Query setFilterById(long... ids) {
776 mIds = ids;
Steve Howarda2709362010-07-02 17:12:48 -0700777 return this;
778 }
779
780 /**
781 * Include only downloads with status matching any the given status flags.
782 * @param flags any combination of the STATUS_* bit flags
783 * @return this object
784 */
785 public Query setFilterByStatus(int flags) {
786 mStatusFlags = flags;
787 return this;
788 }
789
790 /**
Steve Howard90fb15a2010-09-09 16:13:41 -0700791 * Controls whether this query includes downloads not visible in the system's Downloads UI.
792 * @param value if true, this query will only include downloads that should be displayed in
793 * the system's Downloads UI; if false (the default), this query will include
794 * both visible and invisible downloads.
795 * @return this object
796 * @hide
797 */
798 public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) {
799 mOnlyIncludeVisibleInDownloadsUi = value;
800 return this;
801 }
802
803 /**
Steve Howardf054e192010-09-01 18:26:26 -0700804 * Change the sort order of the returned Cursor.
805 *
806 * @param column one of the COLUMN_* constants; currently, only
807 * {@link #COLUMN_LAST_MODIFIED_TIMESTAMP} and {@link #COLUMN_TOTAL_SIZE_BYTES} are
808 * supported.
809 * @param direction either {@link #ORDER_ASCENDING} or {@link #ORDER_DESCENDING}
810 * @return this object
811 * @hide
812 */
813 public Query orderBy(String column, int direction) {
814 if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) {
815 throw new IllegalArgumentException("Invalid direction: " + direction);
816 }
817
818 if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700819 mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION;
Steve Howardf054e192010-09-01 18:26:26 -0700820 } else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700821 mOrderByColumn = Downloads.Impl.COLUMN_TOTAL_BYTES;
Steve Howardf054e192010-09-01 18:26:26 -0700822 } else {
823 throw new IllegalArgumentException("Cannot order by " + column);
824 }
825 mOrderDirection = direction;
826 return this;
827 }
828
829 /**
Steve Howarda2709362010-07-02 17:12:48 -0700830 * Run this query using the given ContentResolver.
831 * @param projection the projection to pass to ContentResolver.query()
832 * @return the Cursor returned by ContentResolver.query()
833 */
Steve Howardeca77fc2010-09-12 18:49:08 -0700834 Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) {
835 Uri uri = baseUri;
Steve Howard90fb15a2010-09-09 16:13:41 -0700836 List<String> selectionParts = new ArrayList<String>();
Steve Howard64c48b82010-10-07 17:53:52 -0700837 String[] selectionArgs = null;
Steve Howarda2709362010-07-02 17:12:48 -0700838
Steve Howard64c48b82010-10-07 17:53:52 -0700839 if (mIds != null) {
840 selectionParts.add(getWhereClauseForIds(mIds));
841 selectionArgs = getWhereArgsForIds(mIds);
Steve Howarda2709362010-07-02 17:12:48 -0700842 }
843
844 if (mStatusFlags != null) {
845 List<String> parts = new ArrayList<String>();
846 if ((mStatusFlags & STATUS_PENDING) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700847 parts.add(statusClause("=", Downloads.Impl.STATUS_PENDING));
Steve Howarda2709362010-07-02 17:12:48 -0700848 }
849 if ((mStatusFlags & STATUS_RUNNING) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700850 parts.add(statusClause("=", Downloads.Impl.STATUS_RUNNING));
Steve Howarda2709362010-07-02 17:12:48 -0700851 }
852 if ((mStatusFlags & STATUS_PAUSED) != 0) {
Steve Howard3e8c1d32010-09-29 17:03:32 -0700853 parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP));
854 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY));
855 parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK));
856 parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI));
Steve Howarda2709362010-07-02 17:12:48 -0700857 }
858 if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700859 parts.add(statusClause("=", Downloads.Impl.STATUS_SUCCESS));
Steve Howarda2709362010-07-02 17:12:48 -0700860 }
861 if ((mStatusFlags & STATUS_FAILED) != 0) {
862 parts.add("(" + statusClause(">=", 400)
863 + " AND " + statusClause("<", 600) + ")");
864 }
Steve Howard90fb15a2010-09-09 16:13:41 -0700865 selectionParts.add(joinStrings(" OR ", parts));
Steve Howarda2709362010-07-02 17:12:48 -0700866 }
Steve Howardf054e192010-09-01 18:26:26 -0700867
Steve Howard90fb15a2010-09-09 16:13:41 -0700868 if (mOnlyIncludeVisibleInDownloadsUi) {
869 selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'");
870 }
871
Vasu Nori216fa222010-10-12 23:08:13 -0700872 // only return rows which are not marked 'deleted = 1'
873 selectionParts.add(Downloads.Impl.COLUMN_DELETED + " != '1'");
874
Steve Howard90fb15a2010-09-09 16:13:41 -0700875 String selection = joinStrings(" AND ", selectionParts);
Steve Howardf054e192010-09-01 18:26:26 -0700876 String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC");
877 String orderBy = mOrderByColumn + " " + orderDirection;
878
Steve Howard64c48b82010-10-07 17:53:52 -0700879 return resolver.query(uri, projection, selection, selectionArgs, orderBy);
Steve Howarda2709362010-07-02 17:12:48 -0700880 }
881
882 private String joinStrings(String joiner, Iterable<String> parts) {
883 StringBuilder builder = new StringBuilder();
884 boolean first = true;
885 for (String part : parts) {
886 if (!first) {
887 builder.append(joiner);
888 }
889 builder.append(part);
890 first = false;
891 }
892 return builder.toString();
893 }
894
895 private String statusClause(String operator, int value) {
Vasu Norief7e33b2010-10-20 13:26:02 -0700896 return Downloads.Impl.COLUMN_STATUS + operator + "'" + value + "'";
Steve Howarda2709362010-07-02 17:12:48 -0700897 }
898 }
899
900 private ContentResolver mResolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700901 private String mPackageName;
Steve Howardeca77fc2010-09-12 18:49:08 -0700902 private Uri mBaseUri = Downloads.Impl.CONTENT_URI;
Steve Howarda2709362010-07-02 17:12:48 -0700903
904 /**
905 * @hide
906 */
Steve Howardb8e07a52010-07-21 14:53:21 -0700907 public DownloadManager(ContentResolver resolver, String packageName) {
Steve Howarda2709362010-07-02 17:12:48 -0700908 mResolver = resolver;
Steve Howardb8e07a52010-07-21 14:53:21 -0700909 mPackageName = packageName;
Steve Howarda2709362010-07-02 17:12:48 -0700910 }
911
912 /**
Steve Howardeca77fc2010-09-12 18:49:08 -0700913 * Makes this object access the download provider through /all_downloads URIs rather than
914 * /my_downloads URIs, for clients that have permission to do so.
915 * @hide
916 */
917 public void setAccessAllDownloads(boolean accessAllDownloads) {
918 if (accessAllDownloads) {
919 mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI;
920 } else {
921 mBaseUri = Downloads.Impl.CONTENT_URI;
922 }
923 }
924
925 /**
Steve Howarda2709362010-07-02 17:12:48 -0700926 * Enqueue a new download. The download will start automatically once the download manager is
927 * ready to execute it and connectivity is available.
928 *
929 * @param request the parameters specifying this download
930 * @return an ID for the download, unique across the system. This ID is used to make future
931 * calls related to this download.
932 */
933 public long enqueue(Request request) {
Steve Howardb8e07a52010-07-21 14:53:21 -0700934 ContentValues values = request.toContentValues(mPackageName);
Vasu Norief7e33b2010-10-20 13:26:02 -0700935 Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values);
Steve Howarda2709362010-07-02 17:12:48 -0700936 long id = Long.parseLong(downloadUri.getLastPathSegment());
937 return id;
938 }
939
940 /**
Vasu Nori216fa222010-10-12 23:08:13 -0700941 * Marks the specified download as 'to be deleted'. This is done when a completed download
942 * is to be removed but the row was stored without enough info to delete the corresponding
943 * metadata from Mediaprovider database. Actual cleanup of this row is done in DownloadService.
944 *
945 * @param ids the IDs of the downloads to be marked 'deleted'
946 * @return the number of downloads actually updated
947 * @hide
948 */
949 public int markRowDeleted(long... ids) {
950 if (ids == null || ids.length == 0) {
951 // called with nothing to remove!
952 throw new IllegalArgumentException("input param 'ids' can't be null");
953 }
954 ContentValues values = new ContentValues();
955 values.put(Downloads.Impl.COLUMN_DELETED, 1);
Vasu Nori8da7a4e2010-11-17 17:14:49 -0800956 // if only one id is passed in, then include it in the uri itself.
957 // this will eliminate a full database scan in the download service.
958 if (ids.length == 1) {
959 return mResolver.update(ContentUris.withAppendedId(mBaseUri, ids[0]), values,
960 null, null);
961 }
Vasu Nori216fa222010-10-12 23:08:13 -0700962 return mResolver.update(mBaseUri, values, getWhereClauseForIds(ids),
963 getWhereArgsForIds(ids));
964 }
965
966 /**
Steve Howard64c48b82010-10-07 17:53:52 -0700967 * Cancel downloads and remove them from the download manager. Each download will be stopped if
Vasu Nori17ee56c2011-02-28 08:35:39 -0800968 * it was running, and it will no longer be accessible through the download manager.
969 * If there is a downloaded file, partial or complete, it is deleted.
Steve Howarda2709362010-07-02 17:12:48 -0700970 *
Steve Howard64c48b82010-10-07 17:53:52 -0700971 * @param ids the IDs of the downloads to remove
972 * @return the number of downloads actually removed
Steve Howarda2709362010-07-02 17:12:48 -0700973 */
Steve Howard64c48b82010-10-07 17:53:52 -0700974 public int remove(long... ids) {
Vasu Norie16c43b2010-11-06 18:48:08 -0700975 return markRowDeleted(ids);
Steve Howarda2709362010-07-02 17:12:48 -0700976 }
977
978 /**
979 * Query the download manager about downloads that have been requested.
980 * @param query parameters specifying filters for this query
981 * @return a Cursor over the result set of downloads, with columns consisting of all the
982 * COLUMN_* constants.
983 */
984 public Cursor query(Query query) {
Steve Howardeca77fc2010-09-12 18:49:08 -0700985 Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri);
Steve Howardf054e192010-09-01 18:26:26 -0700986 if (underlyingCursor == null) {
987 return null;
988 }
Steve Howardeca77fc2010-09-12 18:49:08 -0700989 return new CursorTranslator(underlyingCursor, mBaseUri);
Steve Howarda2709362010-07-02 17:12:48 -0700990 }
991
992 /**
993 * Open a downloaded file for reading. The download must have completed.
994 * @param id the ID of the download
995 * @return a read-only {@link ParcelFileDescriptor}
996 * @throws FileNotFoundException if the destination file does not already exist
997 */
998 public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException {
999 return mResolver.openFileDescriptor(getDownloadUri(id), "r");
1000 }
1001
1002 /**
Vasu Nori5be894e2010-11-02 21:55:30 -07001003 * Returns {@link Uri} for the given downloaded file id, if the file is
1004 * downloaded successfully. otherwise, null is returned.
1005 *<p>
1006 * If the specified downloaded file is in external storage (for example, /sdcard dir),
Vasu Nori6e2b2a62010-11-16 17:58:22 -08001007 * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds
1008 * to the filepath on sdcard.
Vasu Nori5be894e2010-11-02 21:55:30 -07001009 *
1010 * @param id the id of the downloaded file.
Vasu Nori1cde3fb2010-11-05 11:02:52 -07001011 * @return the {@link Uri} for the given downloaded file id, if download was successful. null
Vasu Nori5be894e2010-11-02 21:55:30 -07001012 * otherwise.
1013 */
1014 public Uri getUriForDownloadedFile(long id) {
1015 // to check if the file is in cache, get its destination from the database
1016 Query query = new Query().setFilterById(id);
1017 Cursor cursor = null;
1018 try {
1019 cursor = query(query);
1020 if (cursor == null) {
1021 return null;
1022 }
Leon Scrogginsd75e64a2010-12-13 15:48:40 -05001023 if (cursor.moveToFirst()) {
Vasu Nori6e2b2a62010-11-16 17:58:22 -08001024 int status = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_STATUS));
Vasu Nori5be894e2010-11-02 21:55:30 -07001025 if (DownloadManager.STATUS_SUCCESSFUL == status) {
1026 int indx = cursor.getColumnIndexOrThrow(
1027 Downloads.Impl.COLUMN_DESTINATION);
1028 int destination = cursor.getInt(indx);
1029 // TODO: if we ever add API to DownloadManager to let the caller specify
Vasu Nori1cde3fb2010-11-05 11:02:52 -07001030 // non-external storage for a downloaded file, then the following code
Vasu Nori5be894e2010-11-02 21:55:30 -07001031 // should also check for that destination.
1032 if (destination == Downloads.Impl.DESTINATION_CACHE_PARTITION ||
Vasu Norif83e6e42010-12-13 16:28:31 -08001033 destination == Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION ||
Vasu Nori5be894e2010-11-02 21:55:30 -07001034 destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_NOROAMING ||
1035 destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE) {
1036 // return private uri
1037 return ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, id);
1038 } else {
1039 // return public uri
Vasu Nori6e2b2a62010-11-16 17:58:22 -08001040 String path = cursor.getString(
1041 cursor.getColumnIndexOrThrow(COLUMN_LOCAL_FILENAME));
1042 return Uri.fromFile(new File(path));
Vasu Nori5be894e2010-11-02 21:55:30 -07001043 }
1044 }
1045 }
1046 } finally {
1047 if (cursor != null) {
1048 cursor.close();
1049 }
1050 }
1051 // downloaded file not found or its status is not 'successfully completed'
1052 return null;
1053 }
1054
1055 /**
Vasu Nori6e2b2a62010-11-16 17:58:22 -08001056 * Returns {@link Uri} for the given downloaded file id, if the file is
1057 * downloaded successfully. otherwise, null is returned.
1058 *<p>
1059 * If the specified downloaded file is in external storage (for example, /sdcard dir),
1060 * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds
1061 * to the filepath on sdcard.
1062 *
1063 * @param id the id of the downloaded file.
1064 * @return the {@link Uri} for the given downloaded file id, if download was successful. null
1065 * otherwise.
1066 */
1067 public String getMimeTypeForDownloadedFile(long id) {
1068 Query query = new Query().setFilterById(id);
1069 Cursor cursor = null;
1070 try {
1071 cursor = query(query);
1072 if (cursor == null) {
1073 return null;
1074 }
1075 while (cursor.moveToFirst()) {
1076 return cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MEDIA_TYPE));
1077 }
1078 } finally {
1079 if (cursor != null) {
1080 cursor.close();
1081 }
1082 }
1083 // downloaded file not found or its status is not 'successfully completed'
1084 return null;
1085 }
1086
1087 /**
Steve Howard64c48b82010-10-07 17:53:52 -07001088 * Restart the given downloads, which must have already completed (successfully or not). This
Steve Howard90fb15a2010-09-09 16:13:41 -07001089 * method will only work when called from within the download manager's process.
Steve Howard64c48b82010-10-07 17:53:52 -07001090 * @param ids the IDs of the downloads
Steve Howard90fb15a2010-09-09 16:13:41 -07001091 * @hide
1092 */
Steve Howard64c48b82010-10-07 17:53:52 -07001093 public void restartDownload(long... ids) {
1094 Cursor cursor = query(new Query().setFilterById(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -07001095 try {
Steve Howard64c48b82010-10-07 17:53:52 -07001096 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
1097 int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS));
1098 if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) {
1099 throw new IllegalArgumentException("Cannot restart incomplete download: "
1100 + cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
1101 }
Steve Howard90fb15a2010-09-09 16:13:41 -07001102 }
1103 } finally {
1104 cursor.close();
1105 }
1106
1107 ContentValues values = new ContentValues();
1108 values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0);
1109 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1);
1110 values.putNull(Downloads.Impl._DATA);
1111 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
Jeff Sharkey54781202013-01-17 17:27:33 -08001112 values.put(Downloads.Impl.COLUMN_FAILED_CONNECTIONS, 0);
Steve Howard64c48b82010-10-07 17:53:52 -07001113 mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids));
Steve Howard90fb15a2010-09-09 16:13:41 -07001114 }
1115
1116 /**
Vasu Nori0abbf802011-01-17 15:08:14 -08001117 * Returns maximum size, in bytes, of downloads that may go over a mobile connection; or null if
1118 * there's no limit
1119 *
1120 * @param context the {@link Context} to use for accessing the {@link ContentResolver}
1121 * @return maximum size, in bytes, of downloads that may go over a mobile connection; or null if
1122 * there's no limit
1123 */
1124 public static Long getMaxBytesOverMobile(Context context) {
1125 try {
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001126 return Settings.Global.getLong(context.getContentResolver(),
1127 Settings.Global.DOWNLOAD_MAX_BYTES_OVER_MOBILE);
Vasu Nori0abbf802011-01-17 15:08:14 -08001128 } catch (SettingNotFoundException exc) {
1129 return null;
1130 }
1131 }
1132
1133 /**
1134 * Returns recommended maximum size, in bytes, of downloads that may go over a mobile
1135 * connection; or null if there's no recommended limit. The user will have the option to bypass
1136 * this limit.
1137 *
1138 * @param context the {@link Context} to use for accessing the {@link ContentResolver}
1139 * @return recommended maximum size, in bytes, of downloads that may go over a mobile
1140 * connection; or null if there's no recommended limit.
1141 */
1142 public static Long getRecommendedMaxBytesOverMobile(Context context) {
1143 try {
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001144 return Settings.Global.getLong(context.getContentResolver(),
1145 Settings.Global.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE);
Vasu Nori0abbf802011-01-17 15:08:14 -08001146 } catch (SettingNotFoundException exc) {
1147 return null;
1148 }
1149 }
Vasu Noric0e50752011-01-20 17:57:54 -08001150
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001151 /** {@hide} */
1152 public static boolean isActiveNetworkExpensive(Context context) {
1153 // TODO: connect to NetworkPolicyManager
1154 return false;
1155 }
1156
1157 /** {@hide} */
1158 public static long getActiveNetworkWarningBytes(Context context) {
1159 // TODO: connect to NetworkPolicyManager
1160 return -1;
1161 }
1162
Vasu Noric0e50752011-01-20 17:57:54 -08001163 /**
1164 * Adds a file to the downloads database system, so it could appear in Downloads App
1165 * (and thus become eligible for management by the Downloads App).
1166 * <p>
1167 * It is helpful to make the file scannable by MediaScanner by setting the param
1168 * isMediaScannerScannable to true. It makes the file visible in media managing
1169 * applications such as Gallery App, which could be a useful purpose of using this API.
1170 *
1171 * @param title the title that would appear for this file in Downloads App.
1172 * @param description the description that would appear for this file in Downloads App.
1173 * @param isMediaScannerScannable true if the file is to be scanned by MediaScanner. Files
1174 * scanned by MediaScanner appear in the applications used to view media (for example,
1175 * Gallery app).
1176 * @param mimeType mimetype of the file.
1177 * @param path absolute pathname to the file. The file should be world-readable, so that it can
1178 * be managed by the Downloads App and any other app that is used to read it (for example,
1179 * Gallery app to display the file, if the file contents represent a video/image).
1180 * @param length length of the downloaded file
Vasu Norif9e85232011-02-10 14:59:54 -08001181 * @param showNotification true if a notification is to be sent, false otherwise
Vasu Noric0e50752011-01-20 17:57:54 -08001182 * @return an ID for the download entry added to the downloads app, unique across the system
1183 * This ID is used to make future calls related to this download.
1184 */
Vasu Nori37281302011-03-07 11:25:01 -08001185 public long addCompletedDownload(String title, String description,
Vasu Norif9e85232011-02-10 14:59:54 -08001186 boolean isMediaScannerScannable, String mimeType, String path, long length,
1187 boolean showNotification) {
Vasu Noric0e50752011-01-20 17:57:54 -08001188 // make sure the input args are non-null/non-zero
1189 validateArgumentIsNonEmpty("title", title);
1190 validateArgumentIsNonEmpty("description", description);
1191 validateArgumentIsNonEmpty("path", path);
1192 validateArgumentIsNonEmpty("mimeType", mimeType);
Jeff Sharkey33f95ed2012-05-02 17:07:54 -07001193 if (length < 0) {
Vasu Noric0e50752011-01-20 17:57:54 -08001194 throw new IllegalArgumentException(" invalid value for param: totalBytes");
1195 }
1196
1197 // if there is already an entry with the given path name in downloads.db, return its id
1198 Request request = new Request(NON_DOWNLOADMANAGER_DOWNLOAD)
1199 .setTitle(title)
1200 .setDescription(description)
1201 .setMimeType(mimeType);
1202 ContentValues values = request.toContentValues(null);
1203 values.put(Downloads.Impl.COLUMN_DESTINATION,
1204 Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD);
1205 values.put(Downloads.Impl._DATA, path);
1206 values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_SUCCESS);
1207 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, length);
1208 values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED,
1209 (isMediaScannerScannable) ? Request.SCANNABLE_VALUE_YES :
1210 Request.SCANNABLE_VALUE_NO);
Vasu Norif9e85232011-02-10 14:59:54 -08001211 values.put(Downloads.Impl.COLUMN_VISIBILITY, (showNotification) ?
1212 Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION : Request.VISIBILITY_HIDDEN);
Vasu Noric0e50752011-01-20 17:57:54 -08001213 Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values);
1214 if (downloadUri == null) {
1215 return -1;
1216 }
1217 return Long.parseLong(downloadUri.getLastPathSegment());
1218 }
1219 private static final String NON_DOWNLOADMANAGER_DOWNLOAD =
1220 "non-dwnldmngr-download-dont-retry2download";
1221
1222 private static void validateArgumentIsNonEmpty(String paramName, String val) {
1223 if (TextUtils.isEmpty(val)) {
1224 throw new IllegalArgumentException(paramName + " can't be null");
1225 }
1226 }
1227
Vasu Nori0abbf802011-01-17 15:08:14 -08001228 /**
Steve Howarda2709362010-07-02 17:12:48 -07001229 * Get the DownloadProvider URI for the download with the given ID.
1230 */
Steve Howardeca77fc2010-09-12 18:49:08 -07001231 Uri getDownloadUri(long id) {
1232 return ContentUris.withAppendedId(mBaseUri, id);
Steve Howarda2709362010-07-02 17:12:48 -07001233 }
1234
1235 /**
Steve Howard64c48b82010-10-07 17:53:52 -07001236 * Get a parameterized SQL WHERE clause to select a bunch of IDs.
1237 */
1238 static String getWhereClauseForIds(long[] ids) {
1239 StringBuilder whereClause = new StringBuilder();
Vasu Norie7be6bd2010-10-10 14:58:08 -07001240 whereClause.append("(");
Steve Howard64c48b82010-10-07 17:53:52 -07001241 for (int i = 0; i < ids.length; i++) {
1242 if (i > 0) {
Vasu Norie7be6bd2010-10-10 14:58:08 -07001243 whereClause.append("OR ");
Steve Howard64c48b82010-10-07 17:53:52 -07001244 }
Vasu Norie7be6bd2010-10-10 14:58:08 -07001245 whereClause.append(Downloads.Impl._ID);
1246 whereClause.append(" = ? ");
Steve Howard64c48b82010-10-07 17:53:52 -07001247 }
1248 whereClause.append(")");
1249 return whereClause.toString();
1250 }
1251
1252 /**
1253 * Get the selection args for a clause returned by {@link #getWhereClauseForIds(long[])}.
1254 */
1255 static String[] getWhereArgsForIds(long[] ids) {
1256 String[] whereArgs = new String[ids.length];
1257 for (int i = 0; i < ids.length; i++) {
1258 whereArgs[i] = Long.toString(ids[i]);
1259 }
1260 return whereArgs;
1261 }
1262
1263 /**
Steve Howarda2709362010-07-02 17:12:48 -07001264 * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and
1265 * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants.
1266 * Some columns correspond directly to underlying values while others are computed from
1267 * underlying data.
1268 */
1269 private static class CursorTranslator extends CursorWrapper {
Steve Howardeca77fc2010-09-12 18:49:08 -07001270 private Uri mBaseUri;
1271
1272 public CursorTranslator(Cursor cursor, Uri baseUri) {
Steve Howarda2709362010-07-02 17:12:48 -07001273 super(cursor);
Steve Howardeca77fc2010-09-12 18:49:08 -07001274 mBaseUri = baseUri;
Steve Howarda2709362010-07-02 17:12:48 -07001275 }
1276
1277 @Override
Steve Howarda2709362010-07-02 17:12:48 -07001278 public int getInt(int columnIndex) {
1279 return (int) getLong(columnIndex);
1280 }
1281
1282 @Override
1283 public long getLong(int columnIndex) {
Vasu Norie16c43b2010-11-06 18:48:08 -07001284 if (getColumnName(columnIndex).equals(COLUMN_REASON)) {
1285 return getReason(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS)));
1286 } else if (getColumnName(columnIndex).equals(COLUMN_STATUS)) {
1287 return translateStatus(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS)));
1288 } else {
1289 return super.getLong(columnIndex);
1290 }
Steve Howarda2709362010-07-02 17:12:48 -07001291 }
1292
1293 @Override
1294 public String getString(int columnIndex) {
Vasu Norie16c43b2010-11-06 18:48:08 -07001295 return (getColumnName(columnIndex).equals(COLUMN_LOCAL_URI)) ? getLocalUri() :
1296 super.getString(columnIndex);
Steve Howardeca77fc2010-09-12 18:49:08 -07001297 }
1298
1299 private String getLocalUri() {
Vasu Norie16c43b2010-11-06 18:48:08 -07001300 long destinationType = getLong(getColumnIndex(Downloads.Impl.COLUMN_DESTINATION));
1301 if (destinationType == Downloads.Impl.DESTINATION_FILE_URI ||
Vasu Noric0e50752011-01-20 17:57:54 -08001302 destinationType == Downloads.Impl.DESTINATION_EXTERNAL ||
1303 destinationType == Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD) {
Vasu Nori98f03072010-11-15 17:22:20 -08001304 String localPath = getString(getColumnIndex(COLUMN_LOCAL_FILENAME));
Steve Howard99047d72010-09-29 17:41:37 -07001305 if (localPath == null) {
1306 return null;
1307 }
1308 return Uri.fromFile(new File(localPath)).toString();
Steve Howardbb0d23b2010-09-22 18:56:29 -07001309 }
1310
Steve Howardeca77fc2010-09-12 18:49:08 -07001311 // return content URI for cache download
Vasu Norie16c43b2010-11-06 18:48:08 -07001312 long downloadId = getLong(getColumnIndex(Downloads.Impl._ID));
Steve Howardeca77fc2010-09-12 18:49:08 -07001313 return ContentUris.withAppendedId(mBaseUri, downloadId).toString();
Steve Howarda2709362010-07-02 17:12:48 -07001314 }
1315
Steve Howard3e8c1d32010-09-29 17:03:32 -07001316 private long getReason(int status) {
1317 switch (translateStatus(status)) {
1318 case STATUS_FAILED:
1319 return getErrorCode(status);
1320
1321 case STATUS_PAUSED:
1322 return getPausedReason(status);
1323
1324 default:
1325 return 0; // arbitrary value when status is not an error
Steve Howarda2709362010-07-02 17:12:48 -07001326 }
Steve Howard3e8c1d32010-09-29 17:03:32 -07001327 }
1328
1329 private long getPausedReason(int status) {
1330 switch (status) {
1331 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1332 return PAUSED_WAITING_TO_RETRY;
1333
1334 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1335 return PAUSED_WAITING_FOR_NETWORK;
1336
1337 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
1338 return PAUSED_QUEUED_FOR_WIFI;
1339
1340 default:
1341 return PAUSED_UNKNOWN;
1342 }
1343 }
1344
1345 private long getErrorCode(int status) {
Steve Howard33bbd122010-08-02 17:51:29 -07001346 if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS)
1347 || (500 <= status && status < 600)) {
Steve Howarda2709362010-07-02 17:12:48 -07001348 // HTTP status code
1349 return status;
1350 }
1351
1352 switch (status) {
Vasu Norief7e33b2010-10-20 13:26:02 -07001353 case Downloads.Impl.STATUS_FILE_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001354 return ERROR_FILE_ERROR;
1355
Vasu Norief7e33b2010-10-20 13:26:02 -07001356 case Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE:
1357 case Downloads.Impl.STATUS_UNHANDLED_REDIRECT:
Steve Howarda2709362010-07-02 17:12:48 -07001358 return ERROR_UNHANDLED_HTTP_CODE;
1359
Vasu Norief7e33b2010-10-20 13:26:02 -07001360 case Downloads.Impl.STATUS_HTTP_DATA_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001361 return ERROR_HTTP_DATA_ERROR;
1362
Vasu Norief7e33b2010-10-20 13:26:02 -07001363 case Downloads.Impl.STATUS_TOO_MANY_REDIRECTS:
Steve Howarda2709362010-07-02 17:12:48 -07001364 return ERROR_TOO_MANY_REDIRECTS;
1365
Vasu Norief7e33b2010-10-20 13:26:02 -07001366 case Downloads.Impl.STATUS_INSUFFICIENT_SPACE_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001367 return ERROR_INSUFFICIENT_SPACE;
1368
Vasu Norief7e33b2010-10-20 13:26:02 -07001369 case Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR:
Steve Howarda2709362010-07-02 17:12:48 -07001370 return ERROR_DEVICE_NOT_FOUND;
1371
Steve Howard33bbd122010-08-02 17:51:29 -07001372 case Downloads.Impl.STATUS_CANNOT_RESUME:
1373 return ERROR_CANNOT_RESUME;
1374
Steve Howarda9e87c92010-09-16 12:02:03 -07001375 case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR:
1376 return ERROR_FILE_ALREADY_EXISTS;
1377
Steve Howarda2709362010-07-02 17:12:48 -07001378 default:
1379 return ERROR_UNKNOWN;
1380 }
1381 }
1382
Steve Howard3e8c1d32010-09-29 17:03:32 -07001383 private int translateStatus(int status) {
Steve Howarda2709362010-07-02 17:12:48 -07001384 switch (status) {
Vasu Norief7e33b2010-10-20 13:26:02 -07001385 case Downloads.Impl.STATUS_PENDING:
Steve Howarda2709362010-07-02 17:12:48 -07001386 return STATUS_PENDING;
1387
Vasu Norief7e33b2010-10-20 13:26:02 -07001388 case Downloads.Impl.STATUS_RUNNING:
Steve Howarda2709362010-07-02 17:12:48 -07001389 return STATUS_RUNNING;
1390
Steve Howard3e8c1d32010-09-29 17:03:32 -07001391 case Downloads.Impl.STATUS_PAUSED_BY_APP:
1392 case Downloads.Impl.STATUS_WAITING_TO_RETRY:
1393 case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
1394 case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
Steve Howarda2709362010-07-02 17:12:48 -07001395 return STATUS_PAUSED;
1396
Vasu Norief7e33b2010-10-20 13:26:02 -07001397 case Downloads.Impl.STATUS_SUCCESS:
Steve Howarda2709362010-07-02 17:12:48 -07001398 return STATUS_SUCCESSFUL;
1399
1400 default:
Vasu Norief7e33b2010-10-20 13:26:02 -07001401 assert Downloads.Impl.isStatusError(status);
Steve Howarda2709362010-07-02 17:12:48 -07001402 return STATUS_FAILED;
1403 }
1404 }
1405 }
1406}