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