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