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