Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.ContentValues; |
| 21 | import android.database.Cursor; |
| 22 | import android.database.CursorWrapper; |
| 23 | import android.os.ParcelFileDescriptor; |
| 24 | import android.provider.Downloads; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 25 | |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 26 | import java.io.File; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 27 | import java.io.FileNotFoundException; |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Arrays; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.HashSet; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | import java.util.Set; |
| 35 | |
| 36 | /** |
| 37 | * The download manager is a system service that handles long-running HTTP downloads. Clients may |
| 38 | * request that a URI be downloaded to a particular destination file. The download manager will |
| 39 | * conduct the download in the background, taking care of HTTP interactions and retrying downloads |
| 40 | * after failures or across connectivity changes and system reboots. |
| 41 | * |
| 42 | * Instances of this class should be obtained through |
| 43 | * {@link android.content.Context#getSystemService(String)} by passing |
| 44 | * {@link android.content.Context#DOWNLOAD_SERVICE}. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 45 | */ |
| 46 | public class DownloadManager { |
| 47 | /** |
| 48 | * An identifier for a particular download, unique across the system. Clients use this ID to |
| 49 | * make subsequent calls related to the download. |
| 50 | */ |
| 51 | public final static String COLUMN_ID = "id"; |
| 52 | |
| 53 | /** |
| 54 | * The client-supplied title for this download. This will be displayed in system notifications, |
| 55 | * if enabled. |
| 56 | */ |
| 57 | public final static String COLUMN_TITLE = "title"; |
| 58 | |
| 59 | /** |
| 60 | * The client-supplied description of this download. This will be displayed in system |
| 61 | * notifications, if enabled. |
| 62 | */ |
| 63 | public final static String COLUMN_DESCRIPTION = "description"; |
| 64 | |
| 65 | /** |
| 66 | * URI to be downloaded. |
| 67 | */ |
| 68 | public final static String COLUMN_URI = "uri"; |
| 69 | |
| 70 | /** |
| 71 | * Internet Media Type of the downloaded file. This will be filled in based on the server's |
| 72 | * response once the download has started. |
| 73 | * |
| 74 | * @see <a href="http://www.ietf.org/rfc/rfc1590.txt">RFC 1590, defining Media Types</a> |
| 75 | */ |
| 76 | public final static String COLUMN_MEDIA_TYPE = "media_type"; |
| 77 | |
| 78 | /** |
| 79 | * Total size of the download in bytes. This will be filled in once the download starts. |
| 80 | */ |
| 81 | public final static String COLUMN_TOTAL_SIZE_BYTES = "total_size"; |
| 82 | |
| 83 | /** |
| 84 | * Uri where downloaded file will be stored. If a destination is supplied by client, that URI |
| 85 | * will be used here. Otherwise, the value will be filled in with a generated URI once the |
| 86 | * download has started. |
| 87 | */ |
| 88 | public final static String COLUMN_LOCAL_URI = "local_uri"; |
| 89 | |
| 90 | /** |
| 91 | * Current status of the download, as one of the STATUS_* constants. |
| 92 | */ |
| 93 | public final static String COLUMN_STATUS = "status"; |
| 94 | |
| 95 | /** |
| 96 | * Indicates the type of error that occurred, when {@link #COLUMN_STATUS} is |
| 97 | * {@link #STATUS_FAILED}. If an HTTP error occurred, this will hold the HTTP status code as |
| 98 | * defined in RFC 2616. Otherwise, it will hold one of the ERROR_* constants. |
| 99 | * |
| 100 | * If {@link #COLUMN_STATUS} is not {@link #STATUS_FAILED}, this column's value is undefined. |
| 101 | * |
| 102 | * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1">RFC 2616 |
| 103 | * status codes</a> |
| 104 | */ |
| 105 | public final static String COLUMN_ERROR_CODE = "error_code"; |
| 106 | |
| 107 | /** |
| 108 | * Number of bytes download so far. |
| 109 | */ |
| 110 | public final static String COLUMN_BYTES_DOWNLOADED_SO_FAR = "bytes_so_far"; |
| 111 | |
| 112 | /** |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 113 | * Timestamp when the download was last modified, in {@link System#currentTimeMillis |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 114 | * System.currentTimeMillis()} (wall clock time in UTC). |
| 115 | */ |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 116 | public final static String COLUMN_LAST_MODIFIED_TIMESTAMP = "last_modified_timestamp"; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 117 | |
| 118 | |
| 119 | /** |
| 120 | * Value of {@link #COLUMN_STATUS} when the download is waiting to start. |
| 121 | */ |
| 122 | public final static int STATUS_PENDING = 1 << 0; |
| 123 | |
| 124 | /** |
| 125 | * Value of {@link #COLUMN_STATUS} when the download is currently running. |
| 126 | */ |
| 127 | public final static int STATUS_RUNNING = 1 << 1; |
| 128 | |
| 129 | /** |
| 130 | * Value of {@link #COLUMN_STATUS} when the download is waiting to retry or resume. |
| 131 | */ |
| 132 | public final static int STATUS_PAUSED = 1 << 2; |
| 133 | |
| 134 | /** |
| 135 | * Value of {@link #COLUMN_STATUS} when the download has successfully completed. |
| 136 | */ |
| 137 | public final static int STATUS_SUCCESSFUL = 1 << 3; |
| 138 | |
| 139 | /** |
| 140 | * Value of {@link #COLUMN_STATUS} when the download has failed (and will not be retried). |
| 141 | */ |
| 142 | public final static int STATUS_FAILED = 1 << 4; |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit |
| 147 | * under any other error code. |
| 148 | */ |
| 149 | public final static int ERROR_UNKNOWN = 1000; |
| 150 | |
| 151 | /** |
| 152 | * Value of {@link #COLUMN_ERROR_CODE} when a storage issue arises which doesn't fit under any |
| 153 | * other error code. Use the more specific {@link #ERROR_INSUFFICIENT_SPACE} and |
| 154 | * {@link #ERROR_DEVICE_NOT_FOUND} when appropriate. |
| 155 | */ |
| 156 | public final static int ERROR_FILE_ERROR = 1001; |
| 157 | |
| 158 | /** |
| 159 | * Value of {@link #COLUMN_ERROR_CODE} when an HTTP code was received that download manager |
| 160 | * can't handle. |
| 161 | */ |
| 162 | public final static int ERROR_UNHANDLED_HTTP_CODE = 1002; |
| 163 | |
| 164 | /** |
| 165 | * Value of {@link #COLUMN_ERROR_CODE} when an error receiving or processing data occurred at |
| 166 | * the HTTP level. |
| 167 | */ |
| 168 | public final static int ERROR_HTTP_DATA_ERROR = 1004; |
| 169 | |
| 170 | /** |
| 171 | * Value of {@link #COLUMN_ERROR_CODE} when there were too many redirects. |
| 172 | */ |
| 173 | public final static int ERROR_TOO_MANY_REDIRECTS = 1005; |
| 174 | |
| 175 | /** |
| 176 | * Value of {@link #COLUMN_ERROR_CODE} when there was insufficient storage space. Typically, |
| 177 | * this is because the SD card is full. |
| 178 | */ |
| 179 | public final static int ERROR_INSUFFICIENT_SPACE = 1006; |
| 180 | |
| 181 | /** |
| 182 | * Value of {@link #COLUMN_ERROR_CODE} when no external storage device was found. Typically, |
| 183 | * this is because the SD card is not mounted. |
| 184 | */ |
| 185 | public final static int ERROR_DEVICE_NOT_FOUND = 1007; |
| 186 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 187 | /** |
Steve Howard | 33bbd12 | 2010-08-02 17:51:29 -0700 | [diff] [blame] | 188 | * Value of {@link #COLUMN_ERROR_CODE} when some possibly transient error occurred but we can't |
| 189 | * resume the download. |
| 190 | */ |
| 191 | public final static int ERROR_CANNOT_RESUME = 1008; |
| 192 | |
| 193 | /** |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 194 | * Broadcast intent action sent by the download manager when a download completes. |
| 195 | */ |
| 196 | public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE"; |
| 197 | |
| 198 | /** |
| 199 | * Broadcast intent action sent by the download manager when a running download notification is |
| 200 | * clicked. |
| 201 | */ |
| 202 | public final static String ACTION_NOTIFICATION_CLICKED = |
| 203 | "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"; |
| 204 | |
| 205 | /** |
| 206 | * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a |
| 207 | * long) of the download that just completed. |
| 208 | */ |
| 209 | public static final String EXTRA_DOWNLOAD_ID = "extra_download_id"; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 210 | |
| 211 | // this array must contain all public columns |
| 212 | private static final String[] COLUMNS = new String[] { |
| 213 | COLUMN_ID, |
| 214 | COLUMN_TITLE, |
| 215 | COLUMN_DESCRIPTION, |
| 216 | COLUMN_URI, |
| 217 | COLUMN_MEDIA_TYPE, |
| 218 | COLUMN_TOTAL_SIZE_BYTES, |
| 219 | COLUMN_LOCAL_URI, |
| 220 | COLUMN_STATUS, |
| 221 | COLUMN_ERROR_CODE, |
| 222 | COLUMN_BYTES_DOWNLOADED_SO_FAR, |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 223 | COLUMN_LAST_MODIFIED_TIMESTAMP |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | // columns to request from DownloadProvider |
| 227 | private static final String[] UNDERLYING_COLUMNS = new String[] { |
| 228 | Downloads.Impl._ID, |
| 229 | Downloads.COLUMN_TITLE, |
| 230 | Downloads.COLUMN_DESCRIPTION, |
| 231 | Downloads.COLUMN_URI, |
| 232 | Downloads.COLUMN_MIME_TYPE, |
| 233 | Downloads.COLUMN_TOTAL_BYTES, |
| 234 | Downloads._DATA, |
| 235 | Downloads.COLUMN_STATUS, |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 236 | Downloads.COLUMN_CURRENT_BYTES, |
| 237 | Downloads.COLUMN_LAST_MODIFICATION, |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | private static final Set<String> LONG_COLUMNS = new HashSet<String>( |
| 241 | Arrays.asList(COLUMN_ID, COLUMN_TOTAL_SIZE_BYTES, COLUMN_STATUS, COLUMN_ERROR_CODE, |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 242 | COLUMN_BYTES_DOWNLOADED_SO_FAR, COLUMN_LAST_MODIFIED_TIMESTAMP)); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 243 | |
| 244 | /** |
| 245 | * This class contains all the information necessary to request a new download. The URI is the |
| 246 | * only required parameter. |
| 247 | */ |
| 248 | public static class Request { |
| 249 | /** |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 250 | * Bit flag for {@link #setAllowedNetworkTypes} corresponding to |
| 251 | * {@link ConnectivityManager#TYPE_MOBILE}. |
| 252 | */ |
| 253 | public static final int NETWORK_MOBILE = 1 << 0; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 254 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Bit flag for {@link #setAllowedNetworkTypes} corresponding to |
| 257 | * {@link ConnectivityManager#TYPE_WIFI}. |
| 258 | */ |
| 259 | public static final int NETWORK_WIFI = 1 << 1; |
| 260 | |
| 261 | /** |
| 262 | * Bit flag for {@link #setAllowedNetworkTypes} corresponding to |
| 263 | * {@link ConnectivityManager#TYPE_WIMAX}. |
| 264 | */ |
| 265 | public static final int NETWORK_WIMAX = 1 << 2; |
| 266 | |
| 267 | private Uri mUri; |
| 268 | private Uri mDestinationUri; |
| 269 | private Map<String, String> mRequestHeaders = new HashMap<String, String>(); |
| 270 | private String mTitle; |
| 271 | private String mDescription; |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame] | 272 | private boolean mShowNotification = true; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 273 | private String mMediaType; |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 274 | private boolean mRoamingAllowed = true; |
| 275 | private int mAllowedNetworkTypes = ~0; // default to all network types allowed |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * @param uri the HTTP URI to download. |
| 279 | */ |
| 280 | public Request(Uri uri) { |
| 281 | if (uri == null) { |
| 282 | throw new NullPointerException(); |
| 283 | } |
| 284 | String scheme = uri.getScheme(); |
| 285 | if (scheme == null || !scheme.equals("http")) { |
| 286 | throw new IllegalArgumentException("Can only download HTTP URIs: " + uri); |
| 287 | } |
| 288 | mUri = uri; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Set the local destination for the downloaded data. Must be a file URI to a path on |
| 293 | * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE |
| 294 | * permission. |
| 295 | * |
| 296 | * By default, downloads are saved to a generated file in the download cache and may be |
| 297 | * deleted by the download manager at any time. |
| 298 | * |
| 299 | * @return this object |
| 300 | */ |
| 301 | public Request setDestinationUri(Uri uri) { |
| 302 | mDestinationUri = uri; |
| 303 | return this; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Set an HTTP header to be included with the download request. |
| 308 | * @param header HTTP header name |
| 309 | * @param value header value |
| 310 | * @return this object |
| 311 | */ |
| 312 | public Request setRequestHeader(String header, String value) { |
| 313 | mRequestHeaders.put(header, value); |
| 314 | return this; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Set the title of this download, to be displayed in notifications (if enabled) |
| 319 | * @return this object |
| 320 | */ |
| 321 | public Request setTitle(String title) { |
| 322 | mTitle = title; |
| 323 | return this; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Set a description of this download, to be displayed in notifications (if enabled) |
| 328 | * @return this object |
| 329 | */ |
| 330 | public Request setDescription(String description) { |
| 331 | mDescription = description; |
| 332 | return this; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Set the Internet Media Type of this download. This will override the media type declared |
| 337 | * in the server's response. |
| 338 | * @see <a href="http://www.ietf.org/rfc/rfc1590.txt">RFC 1590, defining Media Types</a> |
| 339 | * @return this object |
| 340 | */ |
| 341 | public Request setMediaType(String mediaType) { |
| 342 | mMediaType = mediaType; |
| 343 | return this; |
| 344 | } |
| 345 | |
| 346 | /** |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame] | 347 | * Control whether a system notification is posted by the download manager while this |
| 348 | * download is running. If enabled, the download manager posts notifications about downloads |
| 349 | * through the system {@link android.app.NotificationManager}. By default, a notification is |
| 350 | * shown. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 351 | * |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame] | 352 | * If set to false, this requires the permission |
| 353 | * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION. |
| 354 | * |
| 355 | * @param show whether the download manager should show a notification for this download. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 356 | * @return this object |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame] | 357 | * @hide |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 358 | */ |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame] | 359 | public Request setShowRunningNotification(boolean show) { |
| 360 | mShowNotification = show; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 361 | return this; |
| 362 | } |
| 363 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 364 | /** |
| 365 | * Restrict the types of networks over which this download may proceed. By default, all |
| 366 | * network types are allowed. |
| 367 | * @param flags any combination of the NETWORK_* bit flags. |
| 368 | * @return this object |
| 369 | */ |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 370 | public Request setAllowedNetworkTypes(int flags) { |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 371 | mAllowedNetworkTypes = flags; |
| 372 | return this; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 375 | /** |
| 376 | * Set whether this download may proceed over a roaming connection. By default, roaming is |
| 377 | * allowed. |
| 378 | * @param allowed whether to allow a roaming connection to be used |
| 379 | * @return this object |
| 380 | */ |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 381 | public Request setAllowedOverRoaming(boolean allowed) { |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 382 | mRoamingAllowed = allowed; |
| 383 | return this; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
| 387 | * @return ContentValues to be passed to DownloadProvider.insert() |
| 388 | */ |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 389 | ContentValues toContentValues(String packageName) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 390 | ContentValues values = new ContentValues(); |
| 391 | assert mUri != null; |
| 392 | values.put(Downloads.COLUMN_URI, mUri.toString()); |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 393 | values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true); |
| 394 | values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE, packageName); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 395 | |
| 396 | if (mDestinationUri != null) { |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 397 | values.put(Downloads.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI); |
| 398 | values.put(Downloads.COLUMN_FILE_NAME_HINT, mDestinationUri.toString()); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 399 | } else { |
| 400 | values.put(Downloads.COLUMN_DESTINATION, |
| 401 | Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE); |
| 402 | } |
| 403 | |
| 404 | if (!mRequestHeaders.isEmpty()) { |
Steve Howard | ea9147d | 2010-07-13 19:02:45 -0700 | [diff] [blame] | 405 | encodeHttpHeaders(values); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | putIfNonNull(values, Downloads.COLUMN_TITLE, mTitle); |
| 409 | putIfNonNull(values, Downloads.COLUMN_DESCRIPTION, mDescription); |
| 410 | putIfNonNull(values, Downloads.COLUMN_MIME_TYPE, mMediaType); |
| 411 | |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame] | 412 | values.put(Downloads.COLUMN_VISIBILITY, |
| 413 | mShowNotification ? Downloads.VISIBILITY_VISIBLE |
| 414 | : Downloads.VISIBILITY_HIDDEN); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 415 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 416 | values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes); |
| 417 | values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed); |
| 418 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 419 | return values; |
| 420 | } |
| 421 | |
Steve Howard | ea9147d | 2010-07-13 19:02:45 -0700 | [diff] [blame] | 422 | private void encodeHttpHeaders(ContentValues values) { |
| 423 | int index = 0; |
| 424 | for (Map.Entry<String, String> entry : mRequestHeaders.entrySet()) { |
| 425 | String headerString = entry.getKey() + ": " + entry.getValue(); |
| 426 | values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString); |
| 427 | index++; |
| 428 | } |
| 429 | } |
| 430 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 431 | private void putIfNonNull(ContentValues contentValues, String key, String value) { |
| 432 | if (value != null) { |
| 433 | contentValues.put(key, value); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * This class may be used to filter download manager queries. |
| 440 | */ |
| 441 | public static class Query { |
| 442 | private Long mId; |
| 443 | private Integer mStatusFlags = null; |
| 444 | |
| 445 | /** |
| 446 | * Include only the download with the given ID. |
| 447 | * @return this object |
| 448 | */ |
| 449 | public Query setFilterById(long id) { |
| 450 | mId = id; |
| 451 | return this; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Include only downloads with status matching any the given status flags. |
| 456 | * @param flags any combination of the STATUS_* bit flags |
| 457 | * @return this object |
| 458 | */ |
| 459 | public Query setFilterByStatus(int flags) { |
| 460 | mStatusFlags = flags; |
| 461 | return this; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Run this query using the given ContentResolver. |
| 466 | * @param projection the projection to pass to ContentResolver.query() |
| 467 | * @return the Cursor returned by ContentResolver.query() |
| 468 | */ |
| 469 | Cursor runQuery(ContentResolver resolver, String[] projection) { |
| 470 | Uri uri = Downloads.CONTENT_URI; |
| 471 | String selection = null; |
| 472 | |
| 473 | if (mId != null) { |
| 474 | uri = Uri.withAppendedPath(uri, mId.toString()); |
| 475 | } |
| 476 | |
| 477 | if (mStatusFlags != null) { |
| 478 | List<String> parts = new ArrayList<String>(); |
| 479 | if ((mStatusFlags & STATUS_PENDING) != 0) { |
| 480 | parts.add(statusClause("=", Downloads.STATUS_PENDING)); |
| 481 | } |
| 482 | if ((mStatusFlags & STATUS_RUNNING) != 0) { |
| 483 | parts.add(statusClause("=", Downloads.STATUS_RUNNING)); |
| 484 | } |
| 485 | if ((mStatusFlags & STATUS_PAUSED) != 0) { |
| 486 | parts.add(statusClause("=", Downloads.STATUS_PENDING_PAUSED)); |
| 487 | parts.add(statusClause("=", Downloads.STATUS_RUNNING_PAUSED)); |
| 488 | } |
| 489 | if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) { |
| 490 | parts.add(statusClause("=", Downloads.STATUS_SUCCESS)); |
| 491 | } |
| 492 | if ((mStatusFlags & STATUS_FAILED) != 0) { |
| 493 | parts.add("(" + statusClause(">=", 400) |
| 494 | + " AND " + statusClause("<", 600) + ")"); |
| 495 | } |
| 496 | selection = joinStrings(" OR ", parts); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 497 | } |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 498 | String orderBy = Downloads.COLUMN_LAST_MODIFICATION + " DESC"; |
| 499 | return resolver.query(uri, projection, selection, null, orderBy); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | private String joinStrings(String joiner, Iterable<String> parts) { |
| 503 | StringBuilder builder = new StringBuilder(); |
| 504 | boolean first = true; |
| 505 | for (String part : parts) { |
| 506 | if (!first) { |
| 507 | builder.append(joiner); |
| 508 | } |
| 509 | builder.append(part); |
| 510 | first = false; |
| 511 | } |
| 512 | return builder.toString(); |
| 513 | } |
| 514 | |
| 515 | private String statusClause(String operator, int value) { |
| 516 | return Downloads.COLUMN_STATUS + operator + "'" + value + "'"; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | private ContentResolver mResolver; |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 521 | private String mPackageName; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 522 | |
| 523 | /** |
| 524 | * @hide |
| 525 | */ |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 526 | public DownloadManager(ContentResolver resolver, String packageName) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 527 | mResolver = resolver; |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 528 | mPackageName = packageName; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Enqueue a new download. The download will start automatically once the download manager is |
| 533 | * ready to execute it and connectivity is available. |
| 534 | * |
| 535 | * @param request the parameters specifying this download |
| 536 | * @return an ID for the download, unique across the system. This ID is used to make future |
| 537 | * calls related to this download. |
| 538 | */ |
| 539 | public long enqueue(Request request) { |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 540 | ContentValues values = request.toContentValues(mPackageName); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 541 | Uri downloadUri = mResolver.insert(Downloads.CONTENT_URI, values); |
| 542 | long id = Long.parseLong(downloadUri.getLastPathSegment()); |
| 543 | return id; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Cancel a download and remove it from the download manager. The download will be stopped if |
| 548 | * it was running, and it will no longer be accessible through the download manager. If a file |
| 549 | * was already downloaded, it will not be deleted. |
| 550 | * |
| 551 | * @param id the ID of the download |
| 552 | */ |
| 553 | public void remove(long id) { |
| 554 | int numDeleted = mResolver.delete(getDownloadUri(id), null, null); |
| 555 | if (numDeleted == 0) { |
| 556 | throw new IllegalArgumentException("Download " + id + " does not exist"); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Query the download manager about downloads that have been requested. |
| 562 | * @param query parameters specifying filters for this query |
| 563 | * @return a Cursor over the result set of downloads, with columns consisting of all the |
| 564 | * COLUMN_* constants. |
| 565 | */ |
| 566 | public Cursor query(Query query) { |
| 567 | Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS); |
| 568 | return new CursorTranslator(underlyingCursor); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Open a downloaded file for reading. The download must have completed. |
| 573 | * @param id the ID of the download |
| 574 | * @return a read-only {@link ParcelFileDescriptor} |
| 575 | * @throws FileNotFoundException if the destination file does not already exist |
| 576 | */ |
| 577 | public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException { |
| 578 | return mResolver.openFileDescriptor(getDownloadUri(id), "r"); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Get the DownloadProvider URI for the download with the given ID. |
| 583 | */ |
| 584 | private Uri getDownloadUri(long id) { |
| 585 | Uri downloadUri = Uri.withAppendedPath(Downloads.CONTENT_URI, Long.toString(id)); |
| 586 | return downloadUri; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and |
| 591 | * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants. |
| 592 | * Some columns correspond directly to underlying values while others are computed from |
| 593 | * underlying data. |
| 594 | */ |
| 595 | private static class CursorTranslator extends CursorWrapper { |
| 596 | public CursorTranslator(Cursor cursor) { |
| 597 | super(cursor); |
| 598 | } |
| 599 | |
| 600 | @Override |
| 601 | public int getColumnIndex(String columnName) { |
| 602 | return Arrays.asList(COLUMNS).indexOf(columnName); |
| 603 | } |
| 604 | |
| 605 | @Override |
| 606 | public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException { |
| 607 | int index = getColumnIndex(columnName); |
| 608 | if (index == -1) { |
| 609 | throw new IllegalArgumentException(); |
| 610 | } |
| 611 | return index; |
| 612 | } |
| 613 | |
| 614 | @Override |
| 615 | public String getColumnName(int columnIndex) { |
| 616 | int numColumns = COLUMNS.length; |
| 617 | if (columnIndex < 0 || columnIndex >= numColumns) { |
| 618 | throw new IllegalArgumentException("Invalid column index " + columnIndex + ", " |
| 619 | + numColumns + " columns exist"); |
| 620 | } |
| 621 | return COLUMNS[columnIndex]; |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public String[] getColumnNames() { |
| 626 | String[] returnColumns = new String[COLUMNS.length]; |
| 627 | System.arraycopy(COLUMNS, 0, returnColumns, 0, COLUMNS.length); |
| 628 | return returnColumns; |
| 629 | } |
| 630 | |
| 631 | @Override |
| 632 | public int getColumnCount() { |
| 633 | return COLUMNS.length; |
| 634 | } |
| 635 | |
| 636 | @Override |
| 637 | public byte[] getBlob(int columnIndex) { |
| 638 | throw new UnsupportedOperationException(); |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public double getDouble(int columnIndex) { |
| 643 | return getLong(columnIndex); |
| 644 | } |
| 645 | |
| 646 | private boolean isLongColumn(String column) { |
| 647 | return LONG_COLUMNS.contains(column); |
| 648 | } |
| 649 | |
| 650 | @Override |
| 651 | public float getFloat(int columnIndex) { |
| 652 | return (float) getDouble(columnIndex); |
| 653 | } |
| 654 | |
| 655 | @Override |
| 656 | public int getInt(int columnIndex) { |
| 657 | return (int) getLong(columnIndex); |
| 658 | } |
| 659 | |
| 660 | @Override |
| 661 | public long getLong(int columnIndex) { |
| 662 | return translateLong(getColumnName(columnIndex)); |
| 663 | } |
| 664 | |
| 665 | @Override |
| 666 | public short getShort(int columnIndex) { |
| 667 | return (short) getLong(columnIndex); |
| 668 | } |
| 669 | |
| 670 | @Override |
| 671 | public String getString(int columnIndex) { |
| 672 | return translateString(getColumnName(columnIndex)); |
| 673 | } |
| 674 | |
| 675 | private String translateString(String column) { |
| 676 | if (isLongColumn(column)) { |
| 677 | return Long.toString(translateLong(column)); |
| 678 | } |
| 679 | if (column.equals(COLUMN_TITLE)) { |
| 680 | return getUnderlyingString(Downloads.COLUMN_TITLE); |
| 681 | } |
| 682 | if (column.equals(COLUMN_DESCRIPTION)) { |
| 683 | return getUnderlyingString(Downloads.COLUMN_DESCRIPTION); |
| 684 | } |
| 685 | if (column.equals(COLUMN_URI)) { |
| 686 | return getUnderlyingString(Downloads.COLUMN_URI); |
| 687 | } |
| 688 | if (column.equals(COLUMN_MEDIA_TYPE)) { |
| 689 | return getUnderlyingString(Downloads.COLUMN_MIME_TYPE); |
| 690 | } |
| 691 | assert column.equals(COLUMN_LOCAL_URI); |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 692 | return Uri.fromFile(new File(getUnderlyingString(Downloads._DATA))).toString(); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | private long translateLong(String column) { |
| 696 | if (!isLongColumn(column)) { |
| 697 | // mimic behavior of underlying cursor -- most likely, throw NumberFormatException |
| 698 | return Long.valueOf(translateString(column)); |
| 699 | } |
| 700 | |
| 701 | if (column.equals(COLUMN_ID)) { |
| 702 | return getUnderlyingLong(Downloads.Impl._ID); |
| 703 | } |
| 704 | if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) { |
| 705 | return getUnderlyingLong(Downloads.COLUMN_TOTAL_BYTES); |
| 706 | } |
| 707 | if (column.equals(COLUMN_STATUS)) { |
| 708 | return translateStatus((int) getUnderlyingLong(Downloads.COLUMN_STATUS)); |
| 709 | } |
| 710 | if (column.equals(COLUMN_ERROR_CODE)) { |
| 711 | return translateErrorCode((int) getUnderlyingLong(Downloads.COLUMN_STATUS)); |
| 712 | } |
| 713 | if (column.equals(COLUMN_BYTES_DOWNLOADED_SO_FAR)) { |
| 714 | return getUnderlyingLong(Downloads.COLUMN_CURRENT_BYTES); |
| 715 | } |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 716 | assert column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP); |
| 717 | return getUnderlyingLong(Downloads.COLUMN_LAST_MODIFICATION); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | private long translateErrorCode(int status) { |
| 721 | if (translateStatus(status) != STATUS_FAILED) { |
| 722 | return 0; // arbitrary value when status is not an error |
| 723 | } |
Steve Howard | 33bbd12 | 2010-08-02 17:51:29 -0700 | [diff] [blame] | 724 | if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS) |
| 725 | || (500 <= status && status < 600)) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 726 | // HTTP status code |
| 727 | return status; |
| 728 | } |
| 729 | |
| 730 | switch (status) { |
| 731 | case Downloads.STATUS_FILE_ERROR: |
| 732 | return ERROR_FILE_ERROR; |
| 733 | |
| 734 | case Downloads.STATUS_UNHANDLED_HTTP_CODE: |
| 735 | case Downloads.STATUS_UNHANDLED_REDIRECT: |
| 736 | return ERROR_UNHANDLED_HTTP_CODE; |
| 737 | |
| 738 | case Downloads.STATUS_HTTP_DATA_ERROR: |
| 739 | return ERROR_HTTP_DATA_ERROR; |
| 740 | |
| 741 | case Downloads.STATUS_TOO_MANY_REDIRECTS: |
| 742 | return ERROR_TOO_MANY_REDIRECTS; |
| 743 | |
| 744 | case Downloads.STATUS_INSUFFICIENT_SPACE_ERROR: |
| 745 | return ERROR_INSUFFICIENT_SPACE; |
| 746 | |
| 747 | case Downloads.STATUS_DEVICE_NOT_FOUND_ERROR: |
| 748 | return ERROR_DEVICE_NOT_FOUND; |
| 749 | |
Steve Howard | 33bbd12 | 2010-08-02 17:51:29 -0700 | [diff] [blame] | 750 | case Downloads.Impl.STATUS_CANNOT_RESUME: |
| 751 | return ERROR_CANNOT_RESUME; |
| 752 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 753 | default: |
| 754 | return ERROR_UNKNOWN; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | private long getUnderlyingLong(String column) { |
| 759 | return super.getLong(super.getColumnIndex(column)); |
| 760 | } |
| 761 | |
| 762 | private String getUnderlyingString(String column) { |
| 763 | return super.getString(super.getColumnIndex(column)); |
| 764 | } |
| 765 | |
| 766 | private long translateStatus(int status) { |
| 767 | switch (status) { |
| 768 | case Downloads.STATUS_PENDING: |
| 769 | return STATUS_PENDING; |
| 770 | |
| 771 | case Downloads.STATUS_RUNNING: |
| 772 | return STATUS_RUNNING; |
| 773 | |
| 774 | case Downloads.STATUS_PENDING_PAUSED: |
| 775 | case Downloads.STATUS_RUNNING_PAUSED: |
| 776 | return STATUS_PAUSED; |
| 777 | |
| 778 | case Downloads.STATUS_SUCCESS: |
| 779 | return STATUS_SUCCESSFUL; |
| 780 | |
| 781 | default: |
| 782 | assert Downloads.isStatusError(status); |
| 783 | return STATUS_FAILED; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | } |