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 | /** |
| 188 | * Broadcast intent action sent by the download manager when a download completes. |
| 189 | */ |
| 190 | public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE"; |
| 191 | |
| 192 | /** |
| 193 | * Broadcast intent action sent by the download manager when a running download notification is |
| 194 | * clicked. |
| 195 | */ |
| 196 | public final static String ACTION_NOTIFICATION_CLICKED = |
| 197 | "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"; |
| 198 | |
| 199 | /** |
| 200 | * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a |
| 201 | * long) of the download that just completed. |
| 202 | */ |
| 203 | public static final String EXTRA_DOWNLOAD_ID = "extra_download_id"; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 204 | |
| 205 | // this array must contain all public columns |
| 206 | private static final String[] COLUMNS = new String[] { |
| 207 | COLUMN_ID, |
| 208 | COLUMN_TITLE, |
| 209 | COLUMN_DESCRIPTION, |
| 210 | COLUMN_URI, |
| 211 | COLUMN_MEDIA_TYPE, |
| 212 | COLUMN_TOTAL_SIZE_BYTES, |
| 213 | COLUMN_LOCAL_URI, |
| 214 | COLUMN_STATUS, |
| 215 | COLUMN_ERROR_CODE, |
| 216 | COLUMN_BYTES_DOWNLOADED_SO_FAR, |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 217 | COLUMN_LAST_MODIFIED_TIMESTAMP |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | // columns to request from DownloadProvider |
| 221 | private static final String[] UNDERLYING_COLUMNS = new String[] { |
| 222 | Downloads.Impl._ID, |
| 223 | Downloads.COLUMN_TITLE, |
| 224 | Downloads.COLUMN_DESCRIPTION, |
| 225 | Downloads.COLUMN_URI, |
| 226 | Downloads.COLUMN_MIME_TYPE, |
| 227 | Downloads.COLUMN_TOTAL_BYTES, |
| 228 | Downloads._DATA, |
| 229 | Downloads.COLUMN_STATUS, |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 230 | Downloads.COLUMN_CURRENT_BYTES, |
| 231 | Downloads.COLUMN_LAST_MODIFICATION, |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | private static final Set<String> LONG_COLUMNS = new HashSet<String>( |
| 235 | 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] | 236 | COLUMN_BYTES_DOWNLOADED_SO_FAR, COLUMN_LAST_MODIFIED_TIMESTAMP)); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * This class contains all the information necessary to request a new download. The URI is the |
| 240 | * only required parameter. |
| 241 | */ |
| 242 | public static class Request { |
| 243 | /** |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 244 | * Bit flag for {@link #setAllowedNetworkTypes} corresponding to |
| 245 | * {@link ConnectivityManager#TYPE_MOBILE}. |
| 246 | */ |
| 247 | public static final int NETWORK_MOBILE = 1 << 0; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 248 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 249 | /** |
| 250 | * Bit flag for {@link #setAllowedNetworkTypes} corresponding to |
| 251 | * {@link ConnectivityManager#TYPE_WIFI}. |
| 252 | */ |
| 253 | public static final int NETWORK_WIFI = 1 << 1; |
| 254 | |
| 255 | /** |
| 256 | * Bit flag for {@link #setAllowedNetworkTypes} corresponding to |
| 257 | * {@link ConnectivityManager#TYPE_WIMAX}. |
| 258 | */ |
| 259 | public static final int NETWORK_WIMAX = 1 << 2; |
| 260 | |
| 261 | private Uri mUri; |
| 262 | private Uri mDestinationUri; |
| 263 | private Map<String, String> mRequestHeaders = new HashMap<String, String>(); |
| 264 | private String mTitle; |
| 265 | private String mDescription; |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame^] | 266 | private boolean mShowNotification = true; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 267 | private String mMediaType; |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 268 | private boolean mRoamingAllowed = true; |
| 269 | private int mAllowedNetworkTypes = ~0; // default to all network types allowed |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 270 | |
| 271 | /** |
| 272 | * @param uri the HTTP URI to download. |
| 273 | */ |
| 274 | public Request(Uri uri) { |
| 275 | if (uri == null) { |
| 276 | throw new NullPointerException(); |
| 277 | } |
| 278 | String scheme = uri.getScheme(); |
| 279 | if (scheme == null || !scheme.equals("http")) { |
| 280 | throw new IllegalArgumentException("Can only download HTTP URIs: " + uri); |
| 281 | } |
| 282 | mUri = uri; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Set the local destination for the downloaded data. Must be a file URI to a path on |
| 287 | * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE |
| 288 | * permission. |
| 289 | * |
| 290 | * By default, downloads are saved to a generated file in the download cache and may be |
| 291 | * deleted by the download manager at any time. |
| 292 | * |
| 293 | * @return this object |
| 294 | */ |
| 295 | public Request setDestinationUri(Uri uri) { |
| 296 | mDestinationUri = uri; |
| 297 | return this; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Set an HTTP header to be included with the download request. |
| 302 | * @param header HTTP header name |
| 303 | * @param value header value |
| 304 | * @return this object |
| 305 | */ |
| 306 | public Request setRequestHeader(String header, String value) { |
| 307 | mRequestHeaders.put(header, value); |
| 308 | return this; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Set the title of this download, to be displayed in notifications (if enabled) |
| 313 | * @return this object |
| 314 | */ |
| 315 | public Request setTitle(String title) { |
| 316 | mTitle = title; |
| 317 | return this; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Set a description of this download, to be displayed in notifications (if enabled) |
| 322 | * @return this object |
| 323 | */ |
| 324 | public Request setDescription(String description) { |
| 325 | mDescription = description; |
| 326 | return this; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Set the Internet Media Type of this download. This will override the media type declared |
| 331 | * in the server's response. |
| 332 | * @see <a href="http://www.ietf.org/rfc/rfc1590.txt">RFC 1590, defining Media Types</a> |
| 333 | * @return this object |
| 334 | */ |
| 335 | public Request setMediaType(String mediaType) { |
| 336 | mMediaType = mediaType; |
| 337 | return this; |
| 338 | } |
| 339 | |
| 340 | /** |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame^] | 341 | * Control whether a system notification is posted by the download manager while this |
| 342 | * download is running. If enabled, the download manager posts notifications about downloads |
| 343 | * through the system {@link android.app.NotificationManager}. By default, a notification is |
| 344 | * shown. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 345 | * |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame^] | 346 | * If set to false, this requires the permission |
| 347 | * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION. |
| 348 | * |
| 349 | * @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] | 350 | * @return this object |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame^] | 351 | * @hide |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 352 | */ |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame^] | 353 | public Request setShowRunningNotification(boolean show) { |
| 354 | mShowNotification = show; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 355 | return this; |
| 356 | } |
| 357 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 358 | /** |
| 359 | * Restrict the types of networks over which this download may proceed. By default, all |
| 360 | * network types are allowed. |
| 361 | * @param flags any combination of the NETWORK_* bit flags. |
| 362 | * @return this object |
| 363 | */ |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 364 | public Request setAllowedNetworkTypes(int flags) { |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 365 | mAllowedNetworkTypes = flags; |
| 366 | return this; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 369 | /** |
| 370 | * Set whether this download may proceed over a roaming connection. By default, roaming is |
| 371 | * allowed. |
| 372 | * @param allowed whether to allow a roaming connection to be used |
| 373 | * @return this object |
| 374 | */ |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 375 | public Request setAllowedOverRoaming(boolean allowed) { |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 376 | mRoamingAllowed = allowed; |
| 377 | return this; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
| 381 | * @return ContentValues to be passed to DownloadProvider.insert() |
| 382 | */ |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 383 | ContentValues toContentValues(String packageName) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 384 | ContentValues values = new ContentValues(); |
| 385 | assert mUri != null; |
| 386 | values.put(Downloads.COLUMN_URI, mUri.toString()); |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 387 | values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true); |
| 388 | values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE, packageName); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 389 | |
| 390 | if (mDestinationUri != null) { |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 391 | values.put(Downloads.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI); |
| 392 | values.put(Downloads.COLUMN_FILE_NAME_HINT, mDestinationUri.toString()); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 393 | } else { |
| 394 | values.put(Downloads.COLUMN_DESTINATION, |
| 395 | Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE); |
| 396 | } |
| 397 | |
| 398 | if (!mRequestHeaders.isEmpty()) { |
Steve Howard | ea9147d | 2010-07-13 19:02:45 -0700 | [diff] [blame] | 399 | encodeHttpHeaders(values); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | putIfNonNull(values, Downloads.COLUMN_TITLE, mTitle); |
| 403 | putIfNonNull(values, Downloads.COLUMN_DESCRIPTION, mDescription); |
| 404 | putIfNonNull(values, Downloads.COLUMN_MIME_TYPE, mMediaType); |
| 405 | |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame^] | 406 | values.put(Downloads.COLUMN_VISIBILITY, |
| 407 | mShowNotification ? Downloads.VISIBILITY_VISIBLE |
| 408 | : Downloads.VISIBILITY_HIDDEN); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 409 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 410 | values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes); |
| 411 | values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed); |
| 412 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 413 | return values; |
| 414 | } |
| 415 | |
Steve Howard | ea9147d | 2010-07-13 19:02:45 -0700 | [diff] [blame] | 416 | private void encodeHttpHeaders(ContentValues values) { |
| 417 | int index = 0; |
| 418 | for (Map.Entry<String, String> entry : mRequestHeaders.entrySet()) { |
| 419 | String headerString = entry.getKey() + ": " + entry.getValue(); |
| 420 | values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString); |
| 421 | index++; |
| 422 | } |
| 423 | } |
| 424 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 425 | private void putIfNonNull(ContentValues contentValues, String key, String value) { |
| 426 | if (value != null) { |
| 427 | contentValues.put(key, value); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * This class may be used to filter download manager queries. |
| 434 | */ |
| 435 | public static class Query { |
| 436 | private Long mId; |
| 437 | private Integer mStatusFlags = null; |
| 438 | |
| 439 | /** |
| 440 | * Include only the download with the given ID. |
| 441 | * @return this object |
| 442 | */ |
| 443 | public Query setFilterById(long id) { |
| 444 | mId = id; |
| 445 | return this; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Include only downloads with status matching any the given status flags. |
| 450 | * @param flags any combination of the STATUS_* bit flags |
| 451 | * @return this object |
| 452 | */ |
| 453 | public Query setFilterByStatus(int flags) { |
| 454 | mStatusFlags = flags; |
| 455 | return this; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Run this query using the given ContentResolver. |
| 460 | * @param projection the projection to pass to ContentResolver.query() |
| 461 | * @return the Cursor returned by ContentResolver.query() |
| 462 | */ |
| 463 | Cursor runQuery(ContentResolver resolver, String[] projection) { |
| 464 | Uri uri = Downloads.CONTENT_URI; |
| 465 | String selection = null; |
| 466 | |
| 467 | if (mId != null) { |
| 468 | uri = Uri.withAppendedPath(uri, mId.toString()); |
| 469 | } |
| 470 | |
| 471 | if (mStatusFlags != null) { |
| 472 | List<String> parts = new ArrayList<String>(); |
| 473 | if ((mStatusFlags & STATUS_PENDING) != 0) { |
| 474 | parts.add(statusClause("=", Downloads.STATUS_PENDING)); |
| 475 | } |
| 476 | if ((mStatusFlags & STATUS_RUNNING) != 0) { |
| 477 | parts.add(statusClause("=", Downloads.STATUS_RUNNING)); |
| 478 | } |
| 479 | if ((mStatusFlags & STATUS_PAUSED) != 0) { |
| 480 | parts.add(statusClause("=", Downloads.STATUS_PENDING_PAUSED)); |
| 481 | parts.add(statusClause("=", Downloads.STATUS_RUNNING_PAUSED)); |
| 482 | } |
| 483 | if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) { |
| 484 | parts.add(statusClause("=", Downloads.STATUS_SUCCESS)); |
| 485 | } |
| 486 | if ((mStatusFlags & STATUS_FAILED) != 0) { |
| 487 | parts.add("(" + statusClause(">=", 400) |
| 488 | + " AND " + statusClause("<", 600) + ")"); |
| 489 | } |
| 490 | selection = joinStrings(" OR ", parts); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 491 | } |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 492 | String orderBy = Downloads.COLUMN_LAST_MODIFICATION + " DESC"; |
| 493 | return resolver.query(uri, projection, selection, null, orderBy); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | private String joinStrings(String joiner, Iterable<String> parts) { |
| 497 | StringBuilder builder = new StringBuilder(); |
| 498 | boolean first = true; |
| 499 | for (String part : parts) { |
| 500 | if (!first) { |
| 501 | builder.append(joiner); |
| 502 | } |
| 503 | builder.append(part); |
| 504 | first = false; |
| 505 | } |
| 506 | return builder.toString(); |
| 507 | } |
| 508 | |
| 509 | private String statusClause(String operator, int value) { |
| 510 | return Downloads.COLUMN_STATUS + operator + "'" + value + "'"; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private ContentResolver mResolver; |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 515 | private String mPackageName; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 516 | |
| 517 | /** |
| 518 | * @hide |
| 519 | */ |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 520 | public DownloadManager(ContentResolver resolver, String packageName) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 521 | mResolver = resolver; |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 522 | mPackageName = packageName; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Enqueue a new download. The download will start automatically once the download manager is |
| 527 | * ready to execute it and connectivity is available. |
| 528 | * |
| 529 | * @param request the parameters specifying this download |
| 530 | * @return an ID for the download, unique across the system. This ID is used to make future |
| 531 | * calls related to this download. |
| 532 | */ |
| 533 | public long enqueue(Request request) { |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 534 | ContentValues values = request.toContentValues(mPackageName); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 535 | Uri downloadUri = mResolver.insert(Downloads.CONTENT_URI, values); |
| 536 | long id = Long.parseLong(downloadUri.getLastPathSegment()); |
| 537 | return id; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Cancel a download and remove it from the download manager. The download will be stopped if |
| 542 | * it was running, and it will no longer be accessible through the download manager. If a file |
| 543 | * was already downloaded, it will not be deleted. |
| 544 | * |
| 545 | * @param id the ID of the download |
| 546 | */ |
| 547 | public void remove(long id) { |
| 548 | int numDeleted = mResolver.delete(getDownloadUri(id), null, null); |
| 549 | if (numDeleted == 0) { |
| 550 | throw new IllegalArgumentException("Download " + id + " does not exist"); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Query the download manager about downloads that have been requested. |
| 556 | * @param query parameters specifying filters for this query |
| 557 | * @return a Cursor over the result set of downloads, with columns consisting of all the |
| 558 | * COLUMN_* constants. |
| 559 | */ |
| 560 | public Cursor query(Query query) { |
| 561 | Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS); |
| 562 | return new CursorTranslator(underlyingCursor); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Open a downloaded file for reading. The download must have completed. |
| 567 | * @param id the ID of the download |
| 568 | * @return a read-only {@link ParcelFileDescriptor} |
| 569 | * @throws FileNotFoundException if the destination file does not already exist |
| 570 | */ |
| 571 | public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException { |
| 572 | return mResolver.openFileDescriptor(getDownloadUri(id), "r"); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Get the DownloadProvider URI for the download with the given ID. |
| 577 | */ |
| 578 | private Uri getDownloadUri(long id) { |
| 579 | Uri downloadUri = Uri.withAppendedPath(Downloads.CONTENT_URI, Long.toString(id)); |
| 580 | return downloadUri; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and |
| 585 | * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants. |
| 586 | * Some columns correspond directly to underlying values while others are computed from |
| 587 | * underlying data. |
| 588 | */ |
| 589 | private static class CursorTranslator extends CursorWrapper { |
| 590 | public CursorTranslator(Cursor cursor) { |
| 591 | super(cursor); |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public int getColumnIndex(String columnName) { |
| 596 | return Arrays.asList(COLUMNS).indexOf(columnName); |
| 597 | } |
| 598 | |
| 599 | @Override |
| 600 | public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException { |
| 601 | int index = getColumnIndex(columnName); |
| 602 | if (index == -1) { |
| 603 | throw new IllegalArgumentException(); |
| 604 | } |
| 605 | return index; |
| 606 | } |
| 607 | |
| 608 | @Override |
| 609 | public String getColumnName(int columnIndex) { |
| 610 | int numColumns = COLUMNS.length; |
| 611 | if (columnIndex < 0 || columnIndex >= numColumns) { |
| 612 | throw new IllegalArgumentException("Invalid column index " + columnIndex + ", " |
| 613 | + numColumns + " columns exist"); |
| 614 | } |
| 615 | return COLUMNS[columnIndex]; |
| 616 | } |
| 617 | |
| 618 | @Override |
| 619 | public String[] getColumnNames() { |
| 620 | String[] returnColumns = new String[COLUMNS.length]; |
| 621 | System.arraycopy(COLUMNS, 0, returnColumns, 0, COLUMNS.length); |
| 622 | return returnColumns; |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public int getColumnCount() { |
| 627 | return COLUMNS.length; |
| 628 | } |
| 629 | |
| 630 | @Override |
| 631 | public byte[] getBlob(int columnIndex) { |
| 632 | throw new UnsupportedOperationException(); |
| 633 | } |
| 634 | |
| 635 | @Override |
| 636 | public double getDouble(int columnIndex) { |
| 637 | return getLong(columnIndex); |
| 638 | } |
| 639 | |
| 640 | private boolean isLongColumn(String column) { |
| 641 | return LONG_COLUMNS.contains(column); |
| 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public float getFloat(int columnIndex) { |
| 646 | return (float) getDouble(columnIndex); |
| 647 | } |
| 648 | |
| 649 | @Override |
| 650 | public int getInt(int columnIndex) { |
| 651 | return (int) getLong(columnIndex); |
| 652 | } |
| 653 | |
| 654 | @Override |
| 655 | public long getLong(int columnIndex) { |
| 656 | return translateLong(getColumnName(columnIndex)); |
| 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public short getShort(int columnIndex) { |
| 661 | return (short) getLong(columnIndex); |
| 662 | } |
| 663 | |
| 664 | @Override |
| 665 | public String getString(int columnIndex) { |
| 666 | return translateString(getColumnName(columnIndex)); |
| 667 | } |
| 668 | |
| 669 | private String translateString(String column) { |
| 670 | if (isLongColumn(column)) { |
| 671 | return Long.toString(translateLong(column)); |
| 672 | } |
| 673 | if (column.equals(COLUMN_TITLE)) { |
| 674 | return getUnderlyingString(Downloads.COLUMN_TITLE); |
| 675 | } |
| 676 | if (column.equals(COLUMN_DESCRIPTION)) { |
| 677 | return getUnderlyingString(Downloads.COLUMN_DESCRIPTION); |
| 678 | } |
| 679 | if (column.equals(COLUMN_URI)) { |
| 680 | return getUnderlyingString(Downloads.COLUMN_URI); |
| 681 | } |
| 682 | if (column.equals(COLUMN_MEDIA_TYPE)) { |
| 683 | return getUnderlyingString(Downloads.COLUMN_MIME_TYPE); |
| 684 | } |
| 685 | assert column.equals(COLUMN_LOCAL_URI); |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 686 | return Uri.fromFile(new File(getUnderlyingString(Downloads._DATA))).toString(); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | private long translateLong(String column) { |
| 690 | if (!isLongColumn(column)) { |
| 691 | // mimic behavior of underlying cursor -- most likely, throw NumberFormatException |
| 692 | return Long.valueOf(translateString(column)); |
| 693 | } |
| 694 | |
| 695 | if (column.equals(COLUMN_ID)) { |
| 696 | return getUnderlyingLong(Downloads.Impl._ID); |
| 697 | } |
| 698 | if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) { |
| 699 | return getUnderlyingLong(Downloads.COLUMN_TOTAL_BYTES); |
| 700 | } |
| 701 | if (column.equals(COLUMN_STATUS)) { |
| 702 | return translateStatus((int) getUnderlyingLong(Downloads.COLUMN_STATUS)); |
| 703 | } |
| 704 | if (column.equals(COLUMN_ERROR_CODE)) { |
| 705 | return translateErrorCode((int) getUnderlyingLong(Downloads.COLUMN_STATUS)); |
| 706 | } |
| 707 | if (column.equals(COLUMN_BYTES_DOWNLOADED_SO_FAR)) { |
| 708 | return getUnderlyingLong(Downloads.COLUMN_CURRENT_BYTES); |
| 709 | } |
Steve Howard | adcb697 | 2010-07-12 17:09:25 -0700 | [diff] [blame] | 710 | assert column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP); |
| 711 | return getUnderlyingLong(Downloads.COLUMN_LAST_MODIFICATION); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | private long translateErrorCode(int status) { |
| 715 | if (translateStatus(status) != STATUS_FAILED) { |
| 716 | return 0; // arbitrary value when status is not an error |
| 717 | } |
| 718 | if ((400 <= status && status < 490) || (500 <= status && status < 600)) { |
| 719 | // HTTP status code |
| 720 | return status; |
| 721 | } |
| 722 | |
| 723 | switch (status) { |
| 724 | case Downloads.STATUS_FILE_ERROR: |
| 725 | return ERROR_FILE_ERROR; |
| 726 | |
| 727 | case Downloads.STATUS_UNHANDLED_HTTP_CODE: |
| 728 | case Downloads.STATUS_UNHANDLED_REDIRECT: |
| 729 | return ERROR_UNHANDLED_HTTP_CODE; |
| 730 | |
| 731 | case Downloads.STATUS_HTTP_DATA_ERROR: |
| 732 | return ERROR_HTTP_DATA_ERROR; |
| 733 | |
| 734 | case Downloads.STATUS_TOO_MANY_REDIRECTS: |
| 735 | return ERROR_TOO_MANY_REDIRECTS; |
| 736 | |
| 737 | case Downloads.STATUS_INSUFFICIENT_SPACE_ERROR: |
| 738 | return ERROR_INSUFFICIENT_SPACE; |
| 739 | |
| 740 | case Downloads.STATUS_DEVICE_NOT_FOUND_ERROR: |
| 741 | return ERROR_DEVICE_NOT_FOUND; |
| 742 | |
| 743 | default: |
| 744 | return ERROR_UNKNOWN; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | private long getUnderlyingLong(String column) { |
| 749 | return super.getLong(super.getColumnIndex(column)); |
| 750 | } |
| 751 | |
| 752 | private String getUnderlyingString(String column) { |
| 753 | return super.getString(super.getColumnIndex(column)); |
| 754 | } |
| 755 | |
| 756 | private long translateStatus(int status) { |
| 757 | switch (status) { |
| 758 | case Downloads.STATUS_PENDING: |
| 759 | return STATUS_PENDING; |
| 760 | |
| 761 | case Downloads.STATUS_RUNNING: |
| 762 | return STATUS_RUNNING; |
| 763 | |
| 764 | case Downloads.STATUS_PENDING_PAUSED: |
| 765 | case Downloads.STATUS_RUNNING_PAUSED: |
| 766 | return STATUS_PAUSED; |
| 767 | |
| 768 | case Downloads.STATUS_SUCCESS: |
| 769 | return STATUS_SUCCESSFUL; |
| 770 | |
| 771 | default: |
| 772 | assert Downloads.isStatusError(status); |
| 773 | return STATUS_FAILED; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | } |