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