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