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; |
Vasu Nori | 0abbf80 | 2011-01-17 15:08:14 -0800 | [diff] [blame] | 30 | import android.provider.Settings; |
| 31 | import android.provider.Settings.SettingNotFoundException; |
Vasu Nori | c0e5075 | 2011-01-20 17:57:54 -0800 | [diff] [blame] | 32 | import android.text.TextUtils; |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 33 | import android.util.Pair; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 34 | |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 35 | import java.io.File; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 36 | import java.io.FileNotFoundException; |
| 37 | import java.util.ArrayList; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 38 | import java.util.List; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 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 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 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 | */ |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 60 | public final static String COLUMN_ID = Downloads.Impl._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 | */ |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 66 | public final static String COLUMN_TITLE = Downloads.Impl.COLUMN_TITLE; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 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 | */ |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 72 | public final static String COLUMN_DESCRIPTION = Downloads.Impl.COLUMN_DESCRIPTION; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * URI to be downloaded. |
| 76 | */ |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 77 | public final static String COLUMN_URI = Downloads.Impl.COLUMN_URI; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 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 | */ |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 109 | public final static String COLUMN_STATUS = Downloads.Impl.COLUMN_STATUS; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 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 | |
Vasu Nori | 216fa22 | 2010-10-12 23:08:13 -0700 | [diff] [blame] | 141 | /** |
| 142 | * The URI to the corresponding entry in MediaProvider for this downloaded entry. It is |
| 143 | * used to delete the entries from MediaProvider database when it is deleted from the |
| 144 | * downloaded list. |
| 145 | */ |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 146 | public static final String COLUMN_MEDIAPROVIDER_URI = Downloads.Impl.COLUMN_MEDIAPROVIDER_URI; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Value of {@link #COLUMN_STATUS} when the download is waiting to start. |
| 150 | */ |
| 151 | public final static int STATUS_PENDING = 1 << 0; |
| 152 | |
| 153 | /** |
| 154 | * Value of {@link #COLUMN_STATUS} when the download is currently running. |
| 155 | */ |
| 156 | public final static int STATUS_RUNNING = 1 << 1; |
| 157 | |
| 158 | /** |
| 159 | * Value of {@link #COLUMN_STATUS} when the download is waiting to retry or resume. |
| 160 | */ |
| 161 | public final static int STATUS_PAUSED = 1 << 2; |
| 162 | |
| 163 | /** |
| 164 | * Value of {@link #COLUMN_STATUS} when the download has successfully completed. |
| 165 | */ |
| 166 | public final static int STATUS_SUCCESSFUL = 1 << 3; |
| 167 | |
| 168 | /** |
| 169 | * Value of {@link #COLUMN_STATUS} when the download has failed (and will not be retried). |
| 170 | */ |
| 171 | public final static int STATUS_FAILED = 1 << 4; |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * Value of COLUMN_ERROR_CODE when the download has completed with an error that doesn't fit |
| 176 | * under any other error code. |
| 177 | */ |
| 178 | public final static int ERROR_UNKNOWN = 1000; |
| 179 | |
| 180 | /** |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 181 | * 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] | 182 | * other error code. Use the more specific {@link #ERROR_INSUFFICIENT_SPACE} and |
| 183 | * {@link #ERROR_DEVICE_NOT_FOUND} when appropriate. |
| 184 | */ |
| 185 | public final static int ERROR_FILE_ERROR = 1001; |
| 186 | |
| 187 | /** |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 188 | * 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] | 189 | * can't handle. |
| 190 | */ |
| 191 | public final static int ERROR_UNHANDLED_HTTP_CODE = 1002; |
| 192 | |
| 193 | /** |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 194 | * 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] | 195 | * the HTTP level. |
| 196 | */ |
| 197 | public final static int ERROR_HTTP_DATA_ERROR = 1004; |
| 198 | |
| 199 | /** |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 200 | * Value of {@link #COLUMN_REASON} when there were too many redirects. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 201 | */ |
| 202 | public final static int ERROR_TOO_MANY_REDIRECTS = 1005; |
| 203 | |
| 204 | /** |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 205 | * Value of {@link #COLUMN_REASON} when there was insufficient storage space. Typically, |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 206 | * this is because the SD card is full. |
| 207 | */ |
| 208 | public final static int ERROR_INSUFFICIENT_SPACE = 1006; |
| 209 | |
| 210 | /** |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 211 | * 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] | 212 | * this is because the SD card is not mounted. |
| 213 | */ |
| 214 | public final static int ERROR_DEVICE_NOT_FOUND = 1007; |
| 215 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 216 | /** |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 217 | * 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] | 218 | * resume the download. |
| 219 | */ |
| 220 | public final static int ERROR_CANNOT_RESUME = 1008; |
| 221 | |
| 222 | /** |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 223 | * 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] | 224 | * download manager will not overwrite an existing file). |
| 225 | */ |
| 226 | public final static int ERROR_FILE_ALREADY_EXISTS = 1009; |
| 227 | |
| 228 | /** |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 229 | * Value of {@link #COLUMN_REASON} when the download is paused because some network error |
| 230 | * occurred and the download manager is waiting before retrying the request. |
| 231 | */ |
| 232 | public final static int PAUSED_WAITING_TO_RETRY = 1; |
| 233 | |
| 234 | /** |
| 235 | * Value of {@link #COLUMN_REASON} when the download is waiting for network connectivity to |
| 236 | * proceed. |
| 237 | */ |
| 238 | public final static int PAUSED_WAITING_FOR_NETWORK = 2; |
| 239 | |
| 240 | /** |
| 241 | * Value of {@link #COLUMN_REASON} when the download exceeds a size limit for downloads over |
| 242 | * the mobile network and the download manager is waiting for a Wi-Fi connection to proceed. |
| 243 | */ |
| 244 | public final static int PAUSED_QUEUED_FOR_WIFI = 3; |
| 245 | |
| 246 | /** |
| 247 | * Value of {@link #COLUMN_REASON} when the download is paused for some other reason. |
| 248 | */ |
| 249 | public final static int PAUSED_UNKNOWN = 4; |
| 250 | |
| 251 | /** |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 252 | * Broadcast intent action sent by the download manager when a download completes. |
| 253 | */ |
| 254 | public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE"; |
| 255 | |
| 256 | /** |
Steve Howard | 610c435 | 2010-09-30 18:30:04 -0700 | [diff] [blame] | 257 | * Broadcast intent action sent by the download manager when the user clicks on a running |
| 258 | * download, either from a system notification or from the downloads UI. |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 259 | */ |
| 260 | public final static String ACTION_NOTIFICATION_CLICKED = |
| 261 | "android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"; |
| 262 | |
| 263 | /** |
Steve Howard | e78fc18 | 2010-09-24 14:59:36 -0700 | [diff] [blame] | 264 | * Intent action to launch an activity to display all downloads. |
| 265 | */ |
| 266 | public final static String ACTION_VIEW_DOWNLOADS = "android.intent.action.VIEW_DOWNLOADS"; |
| 267 | |
| 268 | /** |
Vasu Nori | e5f9224 | 2011-01-24 16:12:20 -0800 | [diff] [blame] | 269 | * Intent extra included with {@link #ACTION_VIEW_DOWNLOADS} to start DownloadApp in |
| 270 | * sort-by-size mode. |
| 271 | */ |
| 272 | public final static String INTENT_EXTRAS_SORT_BY_SIZE = |
| 273 | "android.app.DownloadManager.extra_sortBySize"; |
| 274 | |
| 275 | /** |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 276 | * Intent extra included with {@link #ACTION_DOWNLOAD_COMPLETE} intents, indicating the ID (as a |
| 277 | * long) of the download that just completed. |
| 278 | */ |
| 279 | public static final String EXTRA_DOWNLOAD_ID = "extra_download_id"; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 280 | |
Vasu Nori | 71b8c23 | 2010-10-27 15:22:19 -0700 | [diff] [blame] | 281 | /** |
| 282 | * When clicks on multiple notifications are received, the following |
| 283 | * provides an array of download ids corresponding to the download notification that was |
| 284 | * clicked. It can be retrieved by the receiver of this |
| 285 | * Intent using {@link android.content.Intent#getLongArrayExtra(String)}. |
| 286 | */ |
| 287 | public static final String EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS = "extra_click_download_ids"; |
| 288 | |
Vasu Nori | e16c43b | 2010-11-06 18:48:08 -0700 | [diff] [blame] | 289 | /** |
| 290 | * columns to request from DownloadProvider. |
| 291 | * @hide |
| 292 | */ |
| 293 | public static final String[] UNDERLYING_COLUMNS = new String[] { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 294 | Downloads.Impl._ID, |
Vasu Nori | e69924f | 2010-11-15 13:10:11 -0800 | [diff] [blame] | 295 | Downloads.Impl._DATA + " AS " + COLUMN_LOCAL_FILENAME, |
Vasu Nori | 216fa22 | 2010-10-12 23:08:13 -0700 | [diff] [blame] | 296 | Downloads.Impl.COLUMN_MEDIAPROVIDER_URI, |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 297 | Downloads.Impl.COLUMN_DESTINATION, |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 298 | Downloads.Impl.COLUMN_TITLE, |
| 299 | Downloads.Impl.COLUMN_DESCRIPTION, |
| 300 | Downloads.Impl.COLUMN_URI, |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 301 | Downloads.Impl.COLUMN_STATUS, |
Steve Howard | a9e87c9 | 2010-09-16 12:02:03 -0700 | [diff] [blame] | 302 | Downloads.Impl.COLUMN_FILE_NAME_HINT, |
Vasu Nori | e16c43b | 2010-11-06 18:48:08 -0700 | [diff] [blame] | 303 | Downloads.Impl.COLUMN_MIME_TYPE + " AS " + COLUMN_MEDIA_TYPE, |
| 304 | Downloads.Impl.COLUMN_TOTAL_BYTES + " AS " + COLUMN_TOTAL_SIZE_BYTES, |
| 305 | Downloads.Impl.COLUMN_LAST_MODIFICATION + " AS " + COLUMN_LAST_MODIFIED_TIMESTAMP, |
| 306 | Downloads.Impl.COLUMN_CURRENT_BYTES + " AS " + COLUMN_BYTES_DOWNLOADED_SO_FAR, |
| 307 | /* add the following 'computed' columns to the cursor. |
| 308 | * they are not 'returned' by the database, but their inclusion |
| 309 | * eliminates need to have lot of methods in CursorTranslator |
| 310 | */ |
| 311 | "'placeholder' AS " + COLUMN_LOCAL_URI, |
| 312 | "'placeholder' AS " + COLUMN_REASON |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 315 | /** |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 316 | * 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] | 317 | * only required parameter. |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 318 | * |
| 319 | * Note that the default download destination is a shared volume where the system might delete |
| 320 | * your file if it needs to reclaim space for system use. If this is a problem, use a location |
| 321 | * on external storage (see {@link #setDestinationUri(Uri)}. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 322 | */ |
| 323 | public static class Request { |
| 324 | /** |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 325 | * Bit flag for {@link #setAllowedNetworkTypes} corresponding to |
| 326 | * {@link ConnectivityManager#TYPE_MOBILE}. |
| 327 | */ |
| 328 | public static final int NETWORK_MOBILE = 1 << 0; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 329 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 330 | /** |
| 331 | * Bit flag for {@link #setAllowedNetworkTypes} corresponding to |
| 332 | * {@link ConnectivityManager#TYPE_WIFI}. |
| 333 | */ |
| 334 | public static final int NETWORK_WIFI = 1 << 1; |
| 335 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 336 | private Uri mUri; |
| 337 | private Uri mDestinationUri; |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 338 | private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>(); |
| 339 | private CharSequence mTitle; |
| 340 | private CharSequence mDescription; |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 341 | private String mMimeType; |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 342 | private boolean mRoamingAllowed = true; |
| 343 | private int mAllowedNetworkTypes = ~0; // default to all network types allowed |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 344 | private boolean mIsVisibleInDownloadsUi = true; |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 345 | private boolean mScannable = false; |
Vasu Nori | f83e6e4 | 2010-12-13 16:28:31 -0800 | [diff] [blame] | 346 | private boolean mUseSystemCache = false; |
Vasu Nori | 1cde3fb | 2010-11-05 11:02:52 -0700 | [diff] [blame] | 347 | /** if a file is designated as a MediaScanner scannable file, the following value is |
| 348 | * stored in the database column {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}. |
| 349 | */ |
| 350 | private static final int SCANNABLE_VALUE_YES = 0; |
| 351 | // value of 1 is stored in the above column by DownloadProvider after it is scanned by |
| 352 | // MediaScanner |
| 353 | /** if a file is designated as a file that should not be scanned by MediaScanner, |
| 354 | * the following value is stored in the database column |
| 355 | * {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}. |
| 356 | */ |
| 357 | private static final int SCANNABLE_VALUE_NO = 2; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 358 | |
| 359 | /** |
Vasu Nori | 4c6e5df | 2010-10-26 17:00:16 -0700 | [diff] [blame] | 360 | * This download is visible but only shows in the notifications |
| 361 | * while it's in progress. |
| 362 | */ |
| 363 | public static final int VISIBILITY_VISIBLE = 0; |
| 364 | |
| 365 | /** |
| 366 | * This download is visible and shows in the notifications while |
| 367 | * in progress and after completion. |
| 368 | */ |
| 369 | public static final int VISIBILITY_VISIBLE_NOTIFY_COMPLETED = 1; |
| 370 | |
| 371 | /** |
| 372 | * This download doesn't show in the UI or in the notifications. |
| 373 | */ |
| 374 | public static final int VISIBILITY_HIDDEN = 2; |
| 375 | |
Vasu Nori | f9e8523 | 2011-02-10 14:59:54 -0800 | [diff] [blame^] | 376 | /** |
| 377 | * This download shows in the notifications after completion ONLY. |
| 378 | * It is usuable only with |
| 379 | * {@link DownloadManager#completedDownload(String, String, boolean, String, |
| 380 | * String, long, boolean)}. |
| 381 | */ |
| 382 | public static final int VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION = 3; |
| 383 | |
Vasu Nori | 4c6e5df | 2010-10-26 17:00:16 -0700 | [diff] [blame] | 384 | /** can take any of the following values: {@link #VISIBILITY_HIDDEN} |
Vasu Nori | f9e8523 | 2011-02-10 14:59:54 -0800 | [diff] [blame^] | 385 | * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}, {@link #VISIBILITY_VISIBLE}, |
| 386 | * {@link #VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION} |
Vasu Nori | 4c6e5df | 2010-10-26 17:00:16 -0700 | [diff] [blame] | 387 | */ |
| 388 | private int mNotificationVisibility = VISIBILITY_VISIBLE; |
| 389 | |
| 390 | /** |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 391 | * @param uri the HTTP URI to download. |
| 392 | */ |
| 393 | public Request(Uri uri) { |
| 394 | if (uri == null) { |
| 395 | throw new NullPointerException(); |
| 396 | } |
| 397 | String scheme = uri.getScheme(); |
Paul Westbrook | 86a6019 | 2010-09-15 12:55:49 -0700 | [diff] [blame] | 398 | if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) { |
| 399 | throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + uri); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 400 | } |
| 401 | mUri = uri; |
| 402 | } |
| 403 | |
Vasu Nori | c0e5075 | 2011-01-20 17:57:54 -0800 | [diff] [blame] | 404 | Request(String uriString) { |
| 405 | mUri = Uri.parse(uriString); |
| 406 | } |
| 407 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 408 | /** |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 409 | * 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] | 410 | * external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE |
| 411 | * permission. |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 412 | * <p> |
| 413 | * The downloaded file is not scanned by MediaScanner. |
| 414 | * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}. |
| 415 | * <p> |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 416 | * By default, downloads are saved to a generated filename in the shared download cache and |
| 417 | * may be deleted by the system at any time to reclaim space. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 418 | * |
| 419 | * @return this object |
| 420 | */ |
| 421 | public Request setDestinationUri(Uri uri) { |
| 422 | mDestinationUri = uri; |
| 423 | return this; |
| 424 | } |
| 425 | |
| 426 | /** |
Vasu Nori | f83e6e4 | 2010-12-13 16:28:31 -0800 | [diff] [blame] | 427 | * Set the local destination for the downloaded file to the system cache dir (/cache). |
| 428 | * This is only available to System apps with the permission |
| 429 | * {@link android.Manifest.permission#ACCESS_CACHE_FILESYSTEM}. |
| 430 | * <p> |
| 431 | * The downloaded file is not scanned by MediaScanner. |
| 432 | * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}. |
| 433 | * <p> |
| 434 | * Files downloaded to /cache may be deleted by the system at any time to reclaim space. |
| 435 | * |
| 436 | * @return this object |
| 437 | * @hide |
| 438 | */ |
| 439 | public Request setDestinationToSystemCache() { |
| 440 | mUseSystemCache = true; |
| 441 | return this; |
| 442 | } |
| 443 | |
| 444 | /** |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 445 | * Set the local destination for the downloaded file to a path within the application's |
| 446 | * external files directory (as returned by {@link Context#getExternalFilesDir(String)}. |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 447 | * <p> |
| 448 | * The downloaded file is not scanned by MediaScanner. |
| 449 | * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}. |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 450 | * |
| 451 | * @param context the {@link Context} to use in determining the external files directory |
| 452 | * @param dirType the directory type to pass to {@link Context#getExternalFilesDir(String)} |
| 453 | * @param subPath the path within the external directory, including the destination filename |
| 454 | * @return this object |
| 455 | */ |
| 456 | public Request setDestinationInExternalFilesDir(Context context, String dirType, |
| 457 | String subPath) { |
| 458 | setDestinationFromBase(context.getExternalFilesDir(dirType), subPath); |
| 459 | return this; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Set the local destination for the downloaded file to a path within the public external |
| 464 | * storage directory (as returned by |
| 465 | * {@link Environment#getExternalStoragePublicDirectory(String)}. |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 466 | *<p> |
| 467 | * The downloaded file is not scanned by MediaScanner. |
| 468 | * But it can be made scannable by calling {@link #allowScanningByMediaScanner()}. |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 469 | * |
| 470 | * @param dirType the directory type to pass to |
| 471 | * {@link Environment#getExternalStoragePublicDirectory(String)} |
| 472 | * @param subPath the path within the external directory, including the destination filename |
| 473 | * @return this object |
| 474 | */ |
| 475 | public Request setDestinationInExternalPublicDir(String dirType, String subPath) { |
Vasu Nori | 6916b03 | 2010-12-19 20:31:00 -0800 | [diff] [blame] | 476 | File file = Environment.getExternalStoragePublicDirectory(dirType); |
| 477 | if (file.exists()) { |
| 478 | if (!file.isDirectory()) { |
| 479 | throw new IllegalStateException(file.getAbsolutePath() + |
| 480 | " already exists and is not a directory"); |
| 481 | } |
| 482 | } else { |
| 483 | if (!file.mkdir()) { |
| 484 | throw new IllegalStateException("Unable to create directory: "+ |
| 485 | file.getAbsolutePath()); |
| 486 | } |
| 487 | } |
| 488 | setDestinationFromBase(file, subPath); |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 489 | return this; |
| 490 | } |
| 491 | |
| 492 | private void setDestinationFromBase(File base, String subPath) { |
| 493 | if (subPath == null) { |
| 494 | throw new NullPointerException("subPath cannot be null"); |
| 495 | } |
| 496 | mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath); |
| 497 | } |
| 498 | |
| 499 | /** |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 500 | * If the file to be downloaded is to be scanned by MediaScanner, this method |
| 501 | * should be called before {@link DownloadManager#enqueue(Request)} is called. |
| 502 | */ |
| 503 | public void allowScanningByMediaScanner() { |
| 504 | mScannable = true; |
| 505 | } |
| 506 | |
| 507 | /** |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 508 | * Add an HTTP header to be included with the download request. The header will be added to |
| 509 | * the end of the list. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 510 | * @param header HTTP header name |
| 511 | * @param value header value |
| 512 | * @return this object |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 513 | * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2">HTTP/1.1 |
| 514 | * Message Headers</a> |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 515 | */ |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 516 | public Request addRequestHeader(String header, String value) { |
| 517 | if (header == null) { |
| 518 | throw new NullPointerException("header cannot be null"); |
| 519 | } |
| 520 | if (header.contains(":")) { |
| 521 | throw new IllegalArgumentException("header may not contain ':'"); |
| 522 | } |
| 523 | if (value == null) { |
| 524 | value = ""; |
| 525 | } |
| 526 | mRequestHeaders.add(Pair.create(header, value)); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 527 | return this; |
| 528 | } |
| 529 | |
| 530 | /** |
Steve Howard | 610c435 | 2010-09-30 18:30:04 -0700 | [diff] [blame] | 531 | * Set the title of this download, to be displayed in notifications (if enabled). If no |
| 532 | * title is given, a default one will be assigned based on the download filename, once the |
| 533 | * download starts. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 534 | * @return this object |
| 535 | */ |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 536 | public Request setTitle(CharSequence title) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 537 | mTitle = title; |
| 538 | return this; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Set a description of this download, to be displayed in notifications (if enabled) |
| 543 | * @return this object |
| 544 | */ |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 545 | public Request setDescription(CharSequence description) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 546 | mDescription = description; |
| 547 | return this; |
| 548 | } |
| 549 | |
| 550 | /** |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 551 | * 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] | 552 | * in the server's response. |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 553 | * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1 |
| 554 | * Media Types</a> |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 555 | * @return this object |
| 556 | */ |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 557 | public Request setMimeType(String mimeType) { |
| 558 | mMimeType = mimeType; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 559 | return this; |
| 560 | } |
| 561 | |
| 562 | /** |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame] | 563 | * Control whether a system notification is posted by the download manager while this |
| 564 | * download is running. If enabled, the download manager posts notifications about downloads |
| 565 | * through the system {@link android.app.NotificationManager}. By default, a notification is |
| 566 | * shown. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 567 | * |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame] | 568 | * If set to false, this requires the permission |
| 569 | * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION. |
| 570 | * |
| 571 | * @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] | 572 | * @return this object |
Vasu Nori | 4c6e5df | 2010-10-26 17:00:16 -0700 | [diff] [blame] | 573 | * @deprecated use {@link #setNotificationVisibility(int)} |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 574 | */ |
Vasu Nori | 4c6e5df | 2010-10-26 17:00:16 -0700 | [diff] [blame] | 575 | @Deprecated |
Steve Howard | 8e15afe | 2010-07-28 17:12:40 -0700 | [diff] [blame] | 576 | public Request setShowRunningNotification(boolean show) { |
Vasu Nori | 4c6e5df | 2010-10-26 17:00:16 -0700 | [diff] [blame] | 577 | return (show) ? setNotificationVisibility(VISIBILITY_VISIBLE) : |
| 578 | setNotificationVisibility(VISIBILITY_HIDDEN); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Control whether a system notification is posted by the download manager while this |
| 583 | * download is running or when it is completed. |
| 584 | * If enabled, the download manager posts notifications about downloads |
| 585 | * through the system {@link android.app.NotificationManager}. |
| 586 | * By default, a notification is shown only when the download is in progress. |
| 587 | *<p> |
| 588 | * It can take the following values: {@link #VISIBILITY_HIDDEN}, |
| 589 | * {@link #VISIBILITY_VISIBLE}, |
| 590 | * {@link #VISIBILITY_VISIBLE_NOTIFY_COMPLETED}. |
| 591 | *<p> |
| 592 | * If set to {@link #VISIBILITY_HIDDEN}, this requires the permission |
| 593 | * android.permission.DOWNLOAD_WITHOUT_NOTIFICATION. |
| 594 | * |
| 595 | * @param visibility the visibility setting value |
| 596 | * @return this object |
| 597 | */ |
| 598 | public Request setNotificationVisibility(int visibility) { |
| 599 | mNotificationVisibility = visibility; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 600 | return this; |
| 601 | } |
| 602 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 603 | /** |
| 604 | * Restrict the types of networks over which this download may proceed. By default, all |
| 605 | * network types are allowed. |
| 606 | * @param flags any combination of the NETWORK_* bit flags. |
| 607 | * @return this object |
| 608 | */ |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 609 | public Request setAllowedNetworkTypes(int flags) { |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 610 | mAllowedNetworkTypes = flags; |
| 611 | return this; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 614 | /** |
| 615 | * Set whether this download may proceed over a roaming connection. By default, roaming is |
| 616 | * allowed. |
| 617 | * @param allowed whether to allow a roaming connection to be used |
| 618 | * @return this object |
| 619 | */ |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 620 | public Request setAllowedOverRoaming(boolean allowed) { |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 621 | mRoamingAllowed = allowed; |
| 622 | return this; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /** |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 626 | * Set whether this download should be displayed in the system's Downloads UI. True by |
| 627 | * default. |
| 628 | * @param isVisible whether to display this download in the Downloads UI |
| 629 | * @return this object |
| 630 | */ |
| 631 | public Request setVisibleInDownloadsUi(boolean isVisible) { |
| 632 | mIsVisibleInDownloadsUi = isVisible; |
| 633 | return this; |
| 634 | } |
| 635 | |
| 636 | /** |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 637 | * @return ContentValues to be passed to DownloadProvider.insert() |
| 638 | */ |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 639 | ContentValues toContentValues(String packageName) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 640 | ContentValues values = new ContentValues(); |
| 641 | assert mUri != null; |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 642 | values.put(Downloads.Impl.COLUMN_URI, mUri.toString()); |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 643 | values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true); |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 644 | values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE, packageName); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 645 | |
| 646 | if (mDestinationUri != null) { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 647 | values.put(Downloads.Impl.COLUMN_DESTINATION, Downloads.Impl.DESTINATION_FILE_URI); |
| 648 | values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, mDestinationUri.toString()); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 649 | } else { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 650 | values.put(Downloads.Impl.COLUMN_DESTINATION, |
Vasu Nori | f83e6e4 | 2010-12-13 16:28:31 -0800 | [diff] [blame] | 651 | (this.mUseSystemCache) ? |
| 652 | Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION : |
| 653 | Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 654 | } |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 655 | // is the file supposed to be media-scannable? |
Vasu Nori | 1cde3fb | 2010-11-05 11:02:52 -0700 | [diff] [blame] | 656 | values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED, (mScannable) ? SCANNABLE_VALUE_YES : |
| 657 | SCANNABLE_VALUE_NO); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 658 | |
| 659 | if (!mRequestHeaders.isEmpty()) { |
Steve Howard | ea9147d | 2010-07-13 19:02:45 -0700 | [diff] [blame] | 660 | encodeHttpHeaders(values); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 663 | putIfNonNull(values, Downloads.Impl.COLUMN_TITLE, mTitle); |
| 664 | putIfNonNull(values, Downloads.Impl.COLUMN_DESCRIPTION, mDescription); |
| 665 | putIfNonNull(values, Downloads.Impl.COLUMN_MIME_TYPE, mMimeType); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 666 | |
Vasu Nori | 4c6e5df | 2010-10-26 17:00:16 -0700 | [diff] [blame] | 667 | values.put(Downloads.Impl.COLUMN_VISIBILITY, mNotificationVisibility); |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 668 | values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes); |
| 669 | values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed); |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 670 | values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi); |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 671 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 672 | return values; |
| 673 | } |
| 674 | |
Steve Howard | ea9147d | 2010-07-13 19:02:45 -0700 | [diff] [blame] | 675 | private void encodeHttpHeaders(ContentValues values) { |
| 676 | int index = 0; |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 677 | for (Pair<String, String> header : mRequestHeaders) { |
| 678 | String headerString = header.first + ": " + header.second; |
Steve Howard | ea9147d | 2010-07-13 19:02:45 -0700 | [diff] [blame] | 679 | values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString); |
| 680 | index++; |
| 681 | } |
| 682 | } |
| 683 | |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 684 | private void putIfNonNull(ContentValues contentValues, String key, Object value) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 685 | if (value != null) { |
Steve Howard | 4f564cd | 2010-09-22 15:57:25 -0700 | [diff] [blame] | 686 | contentValues.put(key, value.toString()); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * This class may be used to filter download manager queries. |
| 693 | */ |
| 694 | public static class Query { |
Steve Howard | f054e19 | 2010-09-01 18:26:26 -0700 | [diff] [blame] | 695 | /** |
| 696 | * Constant for use with {@link #orderBy} |
| 697 | * @hide |
| 698 | */ |
| 699 | public static final int ORDER_ASCENDING = 1; |
| 700 | |
| 701 | /** |
| 702 | * Constant for use with {@link #orderBy} |
| 703 | * @hide |
| 704 | */ |
| 705 | public static final int ORDER_DESCENDING = 2; |
| 706 | |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 707 | private long[] mIds = null; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 708 | private Integer mStatusFlags = null; |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 709 | private String mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION; |
Steve Howard | f054e19 | 2010-09-01 18:26:26 -0700 | [diff] [blame] | 710 | private int mOrderDirection = ORDER_DESCENDING; |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 711 | private boolean mOnlyIncludeVisibleInDownloadsUi = false; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 712 | |
| 713 | /** |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 714 | * Include only the downloads with the given IDs. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 715 | * @return this object |
| 716 | */ |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 717 | public Query setFilterById(long... ids) { |
| 718 | mIds = ids; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 719 | return this; |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * Include only downloads with status matching any the given status flags. |
| 724 | * @param flags any combination of the STATUS_* bit flags |
| 725 | * @return this object |
| 726 | */ |
| 727 | public Query setFilterByStatus(int flags) { |
| 728 | mStatusFlags = flags; |
| 729 | return this; |
| 730 | } |
| 731 | |
| 732 | /** |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 733 | * Controls whether this query includes downloads not visible in the system's Downloads UI. |
| 734 | * @param value if true, this query will only include downloads that should be displayed in |
| 735 | * the system's Downloads UI; if false (the default), this query will include |
| 736 | * both visible and invisible downloads. |
| 737 | * @return this object |
| 738 | * @hide |
| 739 | */ |
| 740 | public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) { |
| 741 | mOnlyIncludeVisibleInDownloadsUi = value; |
| 742 | return this; |
| 743 | } |
| 744 | |
| 745 | /** |
Steve Howard | f054e19 | 2010-09-01 18:26:26 -0700 | [diff] [blame] | 746 | * Change the sort order of the returned Cursor. |
| 747 | * |
| 748 | * @param column one of the COLUMN_* constants; currently, only |
| 749 | * {@link #COLUMN_LAST_MODIFIED_TIMESTAMP} and {@link #COLUMN_TOTAL_SIZE_BYTES} are |
| 750 | * supported. |
| 751 | * @param direction either {@link #ORDER_ASCENDING} or {@link #ORDER_DESCENDING} |
| 752 | * @return this object |
| 753 | * @hide |
| 754 | */ |
| 755 | public Query orderBy(String column, int direction) { |
| 756 | if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) { |
| 757 | throw new IllegalArgumentException("Invalid direction: " + direction); |
| 758 | } |
| 759 | |
| 760 | if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 761 | mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION; |
Steve Howard | f054e19 | 2010-09-01 18:26:26 -0700 | [diff] [blame] | 762 | } else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 763 | mOrderByColumn = Downloads.Impl.COLUMN_TOTAL_BYTES; |
Steve Howard | f054e19 | 2010-09-01 18:26:26 -0700 | [diff] [blame] | 764 | } else { |
| 765 | throw new IllegalArgumentException("Cannot order by " + column); |
| 766 | } |
| 767 | mOrderDirection = direction; |
| 768 | return this; |
| 769 | } |
| 770 | |
| 771 | /** |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 772 | * Run this query using the given ContentResolver. |
| 773 | * @param projection the projection to pass to ContentResolver.query() |
| 774 | * @return the Cursor returned by ContentResolver.query() |
| 775 | */ |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 776 | Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) { |
| 777 | Uri uri = baseUri; |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 778 | List<String> selectionParts = new ArrayList<String>(); |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 779 | String[] selectionArgs = null; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 780 | |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 781 | if (mIds != null) { |
| 782 | selectionParts.add(getWhereClauseForIds(mIds)); |
| 783 | selectionArgs = getWhereArgsForIds(mIds); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | if (mStatusFlags != null) { |
| 787 | List<String> parts = new ArrayList<String>(); |
| 788 | if ((mStatusFlags & STATUS_PENDING) != 0) { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 789 | parts.add(statusClause("=", Downloads.Impl.STATUS_PENDING)); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 790 | } |
| 791 | if ((mStatusFlags & STATUS_RUNNING) != 0) { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 792 | parts.add(statusClause("=", Downloads.Impl.STATUS_RUNNING)); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 793 | } |
| 794 | if ((mStatusFlags & STATUS_PAUSED) != 0) { |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 795 | parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP)); |
| 796 | parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY)); |
| 797 | parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK)); |
| 798 | parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI)); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 799 | } |
| 800 | if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 801 | parts.add(statusClause("=", Downloads.Impl.STATUS_SUCCESS)); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 802 | } |
| 803 | if ((mStatusFlags & STATUS_FAILED) != 0) { |
| 804 | parts.add("(" + statusClause(">=", 400) |
| 805 | + " AND " + statusClause("<", 600) + ")"); |
| 806 | } |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 807 | selectionParts.add(joinStrings(" OR ", parts)); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 808 | } |
Steve Howard | f054e19 | 2010-09-01 18:26:26 -0700 | [diff] [blame] | 809 | |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 810 | if (mOnlyIncludeVisibleInDownloadsUi) { |
| 811 | selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'"); |
| 812 | } |
| 813 | |
Vasu Nori | 216fa22 | 2010-10-12 23:08:13 -0700 | [diff] [blame] | 814 | // only return rows which are not marked 'deleted = 1' |
| 815 | selectionParts.add(Downloads.Impl.COLUMN_DELETED + " != '1'"); |
| 816 | |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 817 | String selection = joinStrings(" AND ", selectionParts); |
Steve Howard | f054e19 | 2010-09-01 18:26:26 -0700 | [diff] [blame] | 818 | String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC"); |
| 819 | String orderBy = mOrderByColumn + " " + orderDirection; |
| 820 | |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 821 | return resolver.query(uri, projection, selection, selectionArgs, orderBy); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | private String joinStrings(String joiner, Iterable<String> parts) { |
| 825 | StringBuilder builder = new StringBuilder(); |
| 826 | boolean first = true; |
| 827 | for (String part : parts) { |
| 828 | if (!first) { |
| 829 | builder.append(joiner); |
| 830 | } |
| 831 | builder.append(part); |
| 832 | first = false; |
| 833 | } |
| 834 | return builder.toString(); |
| 835 | } |
| 836 | |
| 837 | private String statusClause(String operator, int value) { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 838 | return Downloads.Impl.COLUMN_STATUS + operator + "'" + value + "'"; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
| 842 | private ContentResolver mResolver; |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 843 | private String mPackageName; |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 844 | private Uri mBaseUri = Downloads.Impl.CONTENT_URI; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 845 | |
| 846 | /** |
| 847 | * @hide |
| 848 | */ |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 849 | public DownloadManager(ContentResolver resolver, String packageName) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 850 | mResolver = resolver; |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 851 | mPackageName = packageName; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /** |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 855 | * Makes this object access the download provider through /all_downloads URIs rather than |
| 856 | * /my_downloads URIs, for clients that have permission to do so. |
| 857 | * @hide |
| 858 | */ |
| 859 | public void setAccessAllDownloads(boolean accessAllDownloads) { |
| 860 | if (accessAllDownloads) { |
| 861 | mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI; |
| 862 | } else { |
| 863 | mBaseUri = Downloads.Impl.CONTENT_URI; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /** |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 868 | * Enqueue a new download. The download will start automatically once the download manager is |
| 869 | * ready to execute it and connectivity is available. |
| 870 | * |
| 871 | * @param request the parameters specifying this download |
| 872 | * @return an ID for the download, unique across the system. This ID is used to make future |
| 873 | * calls related to this download. |
| 874 | */ |
| 875 | public long enqueue(Request request) { |
Steve Howard | b8e07a5 | 2010-07-21 14:53:21 -0700 | [diff] [blame] | 876 | ContentValues values = request.toContentValues(mPackageName); |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 877 | Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 878 | long id = Long.parseLong(downloadUri.getLastPathSegment()); |
| 879 | return id; |
| 880 | } |
| 881 | |
| 882 | /** |
Vasu Nori | 216fa22 | 2010-10-12 23:08:13 -0700 | [diff] [blame] | 883 | * Marks the specified download as 'to be deleted'. This is done when a completed download |
| 884 | * is to be removed but the row was stored without enough info to delete the corresponding |
| 885 | * metadata from Mediaprovider database. Actual cleanup of this row is done in DownloadService. |
| 886 | * |
| 887 | * @param ids the IDs of the downloads to be marked 'deleted' |
| 888 | * @return the number of downloads actually updated |
| 889 | * @hide |
| 890 | */ |
| 891 | public int markRowDeleted(long... ids) { |
| 892 | if (ids == null || ids.length == 0) { |
| 893 | // called with nothing to remove! |
| 894 | throw new IllegalArgumentException("input param 'ids' can't be null"); |
| 895 | } |
| 896 | ContentValues values = new ContentValues(); |
| 897 | values.put(Downloads.Impl.COLUMN_DELETED, 1); |
Vasu Nori | 8da7a4e | 2010-11-17 17:14:49 -0800 | [diff] [blame] | 898 | // if only one id is passed in, then include it in the uri itself. |
| 899 | // this will eliminate a full database scan in the download service. |
| 900 | if (ids.length == 1) { |
| 901 | return mResolver.update(ContentUris.withAppendedId(mBaseUri, ids[0]), values, |
| 902 | null, null); |
| 903 | } |
Vasu Nori | 216fa22 | 2010-10-12 23:08:13 -0700 | [diff] [blame] | 904 | return mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), |
| 905 | getWhereArgsForIds(ids)); |
| 906 | } |
| 907 | |
| 908 | /** |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 909 | * 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] | 910 | * 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] | 911 | * was already downloaded to external storage, it will not be deleted. |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 912 | * |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 913 | * @param ids the IDs of the downloads to remove |
| 914 | * @return the number of downloads actually removed |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 915 | */ |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 916 | public int remove(long... ids) { |
Vasu Nori | e16c43b | 2010-11-06 18:48:08 -0700 | [diff] [blame] | 917 | return markRowDeleted(ids); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /** |
| 921 | * Query the download manager about downloads that have been requested. |
| 922 | * @param query parameters specifying filters for this query |
| 923 | * @return a Cursor over the result set of downloads, with columns consisting of all the |
| 924 | * COLUMN_* constants. |
| 925 | */ |
| 926 | public Cursor query(Query query) { |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 927 | Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri); |
Steve Howard | f054e19 | 2010-09-01 18:26:26 -0700 | [diff] [blame] | 928 | if (underlyingCursor == null) { |
| 929 | return null; |
| 930 | } |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 931 | return new CursorTranslator(underlyingCursor, mBaseUri); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Open a downloaded file for reading. The download must have completed. |
| 936 | * @param id the ID of the download |
| 937 | * @return a read-only {@link ParcelFileDescriptor} |
| 938 | * @throws FileNotFoundException if the destination file does not already exist |
| 939 | */ |
| 940 | public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException { |
| 941 | return mResolver.openFileDescriptor(getDownloadUri(id), "r"); |
| 942 | } |
| 943 | |
| 944 | /** |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 945 | * Returns {@link Uri} for the given downloaded file id, if the file is |
| 946 | * downloaded successfully. otherwise, null is returned. |
| 947 | *<p> |
| 948 | * If the specified downloaded file is in external storage (for example, /sdcard dir), |
Vasu Nori | 6e2b2a6 | 2010-11-16 17:58:22 -0800 | [diff] [blame] | 949 | * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds |
| 950 | * to the filepath on sdcard. |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 951 | * |
| 952 | * @param id the id of the downloaded file. |
Vasu Nori | 1cde3fb | 2010-11-05 11:02:52 -0700 | [diff] [blame] | 953 | * @return the {@link Uri} for the given downloaded file id, if download was successful. null |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 954 | * otherwise. |
| 955 | */ |
| 956 | public Uri getUriForDownloadedFile(long id) { |
| 957 | // to check if the file is in cache, get its destination from the database |
| 958 | Query query = new Query().setFilterById(id); |
| 959 | Cursor cursor = null; |
| 960 | try { |
| 961 | cursor = query(query); |
| 962 | if (cursor == null) { |
| 963 | return null; |
| 964 | } |
Leon Scroggins | d75e64a | 2010-12-13 15:48:40 -0500 | [diff] [blame] | 965 | if (cursor.moveToFirst()) { |
Vasu Nori | 6e2b2a6 | 2010-11-16 17:58:22 -0800 | [diff] [blame] | 966 | int status = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_STATUS)); |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 967 | if (DownloadManager.STATUS_SUCCESSFUL == status) { |
| 968 | int indx = cursor.getColumnIndexOrThrow( |
| 969 | Downloads.Impl.COLUMN_DESTINATION); |
| 970 | int destination = cursor.getInt(indx); |
| 971 | // TODO: if we ever add API to DownloadManager to let the caller specify |
Vasu Nori | 1cde3fb | 2010-11-05 11:02:52 -0700 | [diff] [blame] | 972 | // non-external storage for a downloaded file, then the following code |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 973 | // should also check for that destination. |
| 974 | if (destination == Downloads.Impl.DESTINATION_CACHE_PARTITION || |
Vasu Nori | f83e6e4 | 2010-12-13 16:28:31 -0800 | [diff] [blame] | 975 | destination == Downloads.Impl.DESTINATION_SYSTEMCACHE_PARTITION || |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 976 | destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_NOROAMING || |
| 977 | destination == Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE) { |
| 978 | // return private uri |
| 979 | return ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, id); |
| 980 | } else { |
| 981 | // return public uri |
Vasu Nori | 6e2b2a6 | 2010-11-16 17:58:22 -0800 | [diff] [blame] | 982 | String path = cursor.getString( |
| 983 | cursor.getColumnIndexOrThrow(COLUMN_LOCAL_FILENAME)); |
| 984 | return Uri.fromFile(new File(path)); |
Vasu Nori | 5be894e | 2010-11-02 21:55:30 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | } |
| 988 | } finally { |
| 989 | if (cursor != null) { |
| 990 | cursor.close(); |
| 991 | } |
| 992 | } |
| 993 | // downloaded file not found or its status is not 'successfully completed' |
| 994 | return null; |
| 995 | } |
| 996 | |
| 997 | /** |
Vasu Nori | 6e2b2a6 | 2010-11-16 17:58:22 -0800 | [diff] [blame] | 998 | * Returns {@link Uri} for the given downloaded file id, if the file is |
| 999 | * downloaded successfully. otherwise, null is returned. |
| 1000 | *<p> |
| 1001 | * If the specified downloaded file is in external storage (for example, /sdcard dir), |
| 1002 | * then it is assumed to be safe for anyone to read and the returned {@link Uri} corresponds |
| 1003 | * to the filepath on sdcard. |
| 1004 | * |
| 1005 | * @param id the id of the downloaded file. |
| 1006 | * @return the {@link Uri} for the given downloaded file id, if download was successful. null |
| 1007 | * otherwise. |
| 1008 | */ |
| 1009 | public String getMimeTypeForDownloadedFile(long id) { |
| 1010 | Query query = new Query().setFilterById(id); |
| 1011 | Cursor cursor = null; |
| 1012 | try { |
| 1013 | cursor = query(query); |
| 1014 | if (cursor == null) { |
| 1015 | return null; |
| 1016 | } |
| 1017 | while (cursor.moveToFirst()) { |
| 1018 | return cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MEDIA_TYPE)); |
| 1019 | } |
| 1020 | } finally { |
| 1021 | if (cursor != null) { |
| 1022 | cursor.close(); |
| 1023 | } |
| 1024 | } |
| 1025 | // downloaded file not found or its status is not 'successfully completed' |
| 1026 | return null; |
| 1027 | } |
| 1028 | |
| 1029 | /** |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 1030 | * 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] | 1031 | * 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] | 1032 | * @param ids the IDs of the downloads |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 1033 | * @hide |
| 1034 | */ |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 1035 | public void restartDownload(long... ids) { |
| 1036 | Cursor cursor = query(new Query().setFilterById(ids)); |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 1037 | try { |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 1038 | for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { |
| 1039 | int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS)); |
| 1040 | if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) { |
| 1041 | throw new IllegalArgumentException("Cannot restart incomplete download: " |
| 1042 | + cursor.getLong(cursor.getColumnIndex(COLUMN_ID))); |
| 1043 | } |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } finally { |
| 1046 | cursor.close(); |
| 1047 | } |
| 1048 | |
| 1049 | ContentValues values = new ContentValues(); |
| 1050 | values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0); |
| 1051 | values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1); |
| 1052 | values.putNull(Downloads.Impl._DATA); |
| 1053 | values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING); |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 1054 | mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids)); |
Steve Howard | 90fb15a | 2010-09-09 16:13:41 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | /** |
Vasu Nori | 0abbf80 | 2011-01-17 15:08:14 -0800 | [diff] [blame] | 1058 | * Returns maximum size, in bytes, of downloads that may go over a mobile connection; or null if |
| 1059 | * there's no limit |
| 1060 | * |
| 1061 | * @param context the {@link Context} to use for accessing the {@link ContentResolver} |
| 1062 | * @return maximum size, in bytes, of downloads that may go over a mobile connection; or null if |
| 1063 | * there's no limit |
| 1064 | */ |
| 1065 | public static Long getMaxBytesOverMobile(Context context) { |
| 1066 | try { |
| 1067 | return Settings.Secure.getLong(context.getContentResolver(), |
| 1068 | Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE); |
| 1069 | } catch (SettingNotFoundException exc) { |
| 1070 | return null; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | /** |
| 1075 | * Returns recommended maximum size, in bytes, of downloads that may go over a mobile |
| 1076 | * connection; or null if there's no recommended limit. The user will have the option to bypass |
| 1077 | * this limit. |
| 1078 | * |
| 1079 | * @param context the {@link Context} to use for accessing the {@link ContentResolver} |
| 1080 | * @return recommended maximum size, in bytes, of downloads that may go over a mobile |
| 1081 | * connection; or null if there's no recommended limit. |
| 1082 | */ |
| 1083 | public static Long getRecommendedMaxBytesOverMobile(Context context) { |
| 1084 | try { |
| 1085 | return Settings.Secure.getLong(context.getContentResolver(), |
| 1086 | Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE); |
| 1087 | } catch (SettingNotFoundException exc) { |
| 1088 | return null; |
| 1089 | } |
| 1090 | } |
Vasu Nori | c0e5075 | 2011-01-20 17:57:54 -0800 | [diff] [blame] | 1091 | |
| 1092 | /** |
| 1093 | * Adds a file to the downloads database system, so it could appear in Downloads App |
| 1094 | * (and thus become eligible for management by the Downloads App). |
| 1095 | * <p> |
| 1096 | * It is helpful to make the file scannable by MediaScanner by setting the param |
| 1097 | * isMediaScannerScannable to true. It makes the file visible in media managing |
| 1098 | * applications such as Gallery App, which could be a useful purpose of using this API. |
| 1099 | * |
| 1100 | * @param title the title that would appear for this file in Downloads App. |
| 1101 | * @param description the description that would appear for this file in Downloads App. |
| 1102 | * @param isMediaScannerScannable true if the file is to be scanned by MediaScanner. Files |
| 1103 | * scanned by MediaScanner appear in the applications used to view media (for example, |
| 1104 | * Gallery app). |
| 1105 | * @param mimeType mimetype of the file. |
| 1106 | * @param path absolute pathname to the file. The file should be world-readable, so that it can |
| 1107 | * be managed by the Downloads App and any other app that is used to read it (for example, |
| 1108 | * Gallery app to display the file, if the file contents represent a video/image). |
| 1109 | * @param length length of the downloaded file |
Vasu Nori | f9e8523 | 2011-02-10 14:59:54 -0800 | [diff] [blame^] | 1110 | * @param showNotification true if a notification is to be sent, false otherwise |
Vasu Nori | c0e5075 | 2011-01-20 17:57:54 -0800 | [diff] [blame] | 1111 | * @return an ID for the download entry added to the downloads app, unique across the system |
| 1112 | * This ID is used to make future calls related to this download. |
| 1113 | */ |
| 1114 | public long completedDownload(String title, String description, |
Vasu Nori | f9e8523 | 2011-02-10 14:59:54 -0800 | [diff] [blame^] | 1115 | boolean isMediaScannerScannable, String mimeType, String path, long length, |
| 1116 | boolean showNotification) { |
Vasu Nori | c0e5075 | 2011-01-20 17:57:54 -0800 | [diff] [blame] | 1117 | // make sure the input args are non-null/non-zero |
| 1118 | validateArgumentIsNonEmpty("title", title); |
| 1119 | validateArgumentIsNonEmpty("description", description); |
| 1120 | validateArgumentIsNonEmpty("path", path); |
| 1121 | validateArgumentIsNonEmpty("mimeType", mimeType); |
| 1122 | if (length <= 0) { |
| 1123 | throw new IllegalArgumentException(" invalid value for param: totalBytes"); |
| 1124 | } |
| 1125 | |
| 1126 | // if there is already an entry with the given path name in downloads.db, return its id |
| 1127 | Request request = new Request(NON_DOWNLOADMANAGER_DOWNLOAD) |
| 1128 | .setTitle(title) |
| 1129 | .setDescription(description) |
| 1130 | .setMimeType(mimeType); |
| 1131 | ContentValues values = request.toContentValues(null); |
| 1132 | values.put(Downloads.Impl.COLUMN_DESTINATION, |
| 1133 | Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD); |
| 1134 | values.put(Downloads.Impl._DATA, path); |
| 1135 | values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_SUCCESS); |
| 1136 | values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, length); |
| 1137 | values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED, |
| 1138 | (isMediaScannerScannable) ? Request.SCANNABLE_VALUE_YES : |
| 1139 | Request.SCANNABLE_VALUE_NO); |
Vasu Nori | f9e8523 | 2011-02-10 14:59:54 -0800 | [diff] [blame^] | 1140 | values.put(Downloads.Impl.COLUMN_VISIBILITY, (showNotification) ? |
| 1141 | Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION : Request.VISIBILITY_HIDDEN); |
Vasu Nori | c0e5075 | 2011-01-20 17:57:54 -0800 | [diff] [blame] | 1142 | Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values); |
| 1143 | if (downloadUri == null) { |
| 1144 | return -1; |
| 1145 | } |
| 1146 | return Long.parseLong(downloadUri.getLastPathSegment()); |
| 1147 | } |
| 1148 | private static final String NON_DOWNLOADMANAGER_DOWNLOAD = |
| 1149 | "non-dwnldmngr-download-dont-retry2download"; |
| 1150 | |
| 1151 | private static void validateArgumentIsNonEmpty(String paramName, String val) { |
| 1152 | if (TextUtils.isEmpty(val)) { |
| 1153 | throw new IllegalArgumentException(paramName + " can't be null"); |
| 1154 | } |
| 1155 | } |
| 1156 | |
Vasu Nori | 0abbf80 | 2011-01-17 15:08:14 -0800 | [diff] [blame] | 1157 | /** |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1158 | * Get the DownloadProvider URI for the download with the given ID. |
| 1159 | */ |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 1160 | Uri getDownloadUri(long id) { |
| 1161 | return ContentUris.withAppendedId(mBaseUri, id); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /** |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 1165 | * Get a parameterized SQL WHERE clause to select a bunch of IDs. |
| 1166 | */ |
| 1167 | static String getWhereClauseForIds(long[] ids) { |
| 1168 | StringBuilder whereClause = new StringBuilder(); |
Vasu Nori | e7be6bd | 2010-10-10 14:58:08 -0700 | [diff] [blame] | 1169 | whereClause.append("("); |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 1170 | for (int i = 0; i < ids.length; i++) { |
| 1171 | if (i > 0) { |
Vasu Nori | e7be6bd | 2010-10-10 14:58:08 -0700 | [diff] [blame] | 1172 | whereClause.append("OR "); |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 1173 | } |
Vasu Nori | e7be6bd | 2010-10-10 14:58:08 -0700 | [diff] [blame] | 1174 | whereClause.append(Downloads.Impl._ID); |
| 1175 | whereClause.append(" = ? "); |
Steve Howard | 64c48b8 | 2010-10-07 17:53:52 -0700 | [diff] [blame] | 1176 | } |
| 1177 | whereClause.append(")"); |
| 1178 | return whereClause.toString(); |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * Get the selection args for a clause returned by {@link #getWhereClauseForIds(long[])}. |
| 1183 | */ |
| 1184 | static String[] getWhereArgsForIds(long[] ids) { |
| 1185 | String[] whereArgs = new String[ids.length]; |
| 1186 | for (int i = 0; i < ids.length; i++) { |
| 1187 | whereArgs[i] = Long.toString(ids[i]); |
| 1188 | } |
| 1189 | return whereArgs; |
| 1190 | } |
| 1191 | |
| 1192 | /** |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1193 | * This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and |
| 1194 | * presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants. |
| 1195 | * Some columns correspond directly to underlying values while others are computed from |
| 1196 | * underlying data. |
| 1197 | */ |
| 1198 | private static class CursorTranslator extends CursorWrapper { |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 1199 | private Uri mBaseUri; |
| 1200 | |
| 1201 | public CursorTranslator(Cursor cursor, Uri baseUri) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1202 | super(cursor); |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 1203 | mBaseUri = baseUri; |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | @Override |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1207 | public int getInt(int columnIndex) { |
| 1208 | return (int) getLong(columnIndex); |
| 1209 | } |
| 1210 | |
| 1211 | @Override |
| 1212 | public long getLong(int columnIndex) { |
Vasu Nori | e16c43b | 2010-11-06 18:48:08 -0700 | [diff] [blame] | 1213 | if (getColumnName(columnIndex).equals(COLUMN_REASON)) { |
| 1214 | return getReason(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS))); |
| 1215 | } else if (getColumnName(columnIndex).equals(COLUMN_STATUS)) { |
| 1216 | return translateStatus(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS))); |
| 1217 | } else { |
| 1218 | return super.getLong(columnIndex); |
| 1219 | } |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | @Override |
| 1223 | public String getString(int columnIndex) { |
Vasu Nori | e16c43b | 2010-11-06 18:48:08 -0700 | [diff] [blame] | 1224 | return (getColumnName(columnIndex).equals(COLUMN_LOCAL_URI)) ? getLocalUri() : |
| 1225 | super.getString(columnIndex); |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | private String getLocalUri() { |
Vasu Nori | e16c43b | 2010-11-06 18:48:08 -0700 | [diff] [blame] | 1229 | long destinationType = getLong(getColumnIndex(Downloads.Impl.COLUMN_DESTINATION)); |
| 1230 | if (destinationType == Downloads.Impl.DESTINATION_FILE_URI || |
Vasu Nori | c0e5075 | 2011-01-20 17:57:54 -0800 | [diff] [blame] | 1231 | destinationType == Downloads.Impl.DESTINATION_EXTERNAL || |
| 1232 | destinationType == Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD) { |
Vasu Nori | 98f0307 | 2010-11-15 17:22:20 -0800 | [diff] [blame] | 1233 | String localPath = getString(getColumnIndex(COLUMN_LOCAL_FILENAME)); |
Steve Howard | 99047d7 | 2010-09-29 17:41:37 -0700 | [diff] [blame] | 1234 | if (localPath == null) { |
| 1235 | return null; |
| 1236 | } |
| 1237 | return Uri.fromFile(new File(localPath)).toString(); |
Steve Howard | bb0d23b | 2010-09-22 18:56:29 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 1240 | // return content URI for cache download |
Vasu Nori | e16c43b | 2010-11-06 18:48:08 -0700 | [diff] [blame] | 1241 | long downloadId = getLong(getColumnIndex(Downloads.Impl._ID)); |
Steve Howard | eca77fc | 2010-09-12 18:49:08 -0700 | [diff] [blame] | 1242 | return ContentUris.withAppendedId(mBaseUri, downloadId).toString(); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 1245 | private long getReason(int status) { |
| 1246 | switch (translateStatus(status)) { |
| 1247 | case STATUS_FAILED: |
| 1248 | return getErrorCode(status); |
| 1249 | |
| 1250 | case STATUS_PAUSED: |
| 1251 | return getPausedReason(status); |
| 1252 | |
| 1253 | default: |
| 1254 | return 0; // arbitrary value when status is not an error |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1255 | } |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | private long getPausedReason(int status) { |
| 1259 | switch (status) { |
| 1260 | case Downloads.Impl.STATUS_WAITING_TO_RETRY: |
| 1261 | return PAUSED_WAITING_TO_RETRY; |
| 1262 | |
| 1263 | case Downloads.Impl.STATUS_WAITING_FOR_NETWORK: |
| 1264 | return PAUSED_WAITING_FOR_NETWORK; |
| 1265 | |
| 1266 | case Downloads.Impl.STATUS_QUEUED_FOR_WIFI: |
| 1267 | return PAUSED_QUEUED_FOR_WIFI; |
| 1268 | |
| 1269 | default: |
| 1270 | return PAUSED_UNKNOWN; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | private long getErrorCode(int status) { |
Steve Howard | 33bbd12 | 2010-08-02 17:51:29 -0700 | [diff] [blame] | 1275 | if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS) |
| 1276 | || (500 <= status && status < 600)) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1277 | // HTTP status code |
| 1278 | return status; |
| 1279 | } |
| 1280 | |
| 1281 | switch (status) { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1282 | case Downloads.Impl.STATUS_FILE_ERROR: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1283 | return ERROR_FILE_ERROR; |
| 1284 | |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1285 | case Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE: |
| 1286 | case Downloads.Impl.STATUS_UNHANDLED_REDIRECT: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1287 | return ERROR_UNHANDLED_HTTP_CODE; |
| 1288 | |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1289 | case Downloads.Impl.STATUS_HTTP_DATA_ERROR: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1290 | return ERROR_HTTP_DATA_ERROR; |
| 1291 | |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1292 | case Downloads.Impl.STATUS_TOO_MANY_REDIRECTS: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1293 | return ERROR_TOO_MANY_REDIRECTS; |
| 1294 | |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1295 | case Downloads.Impl.STATUS_INSUFFICIENT_SPACE_ERROR: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1296 | return ERROR_INSUFFICIENT_SPACE; |
| 1297 | |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1298 | case Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1299 | return ERROR_DEVICE_NOT_FOUND; |
| 1300 | |
Steve Howard | 33bbd12 | 2010-08-02 17:51:29 -0700 | [diff] [blame] | 1301 | case Downloads.Impl.STATUS_CANNOT_RESUME: |
| 1302 | return ERROR_CANNOT_RESUME; |
| 1303 | |
Steve Howard | a9e87c9 | 2010-09-16 12:02:03 -0700 | [diff] [blame] | 1304 | case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR: |
| 1305 | return ERROR_FILE_ALREADY_EXISTS; |
| 1306 | |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1307 | default: |
| 1308 | return ERROR_UNKNOWN; |
| 1309 | } |
| 1310 | } |
| 1311 | |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 1312 | private int translateStatus(int status) { |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1313 | switch (status) { |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1314 | case Downloads.Impl.STATUS_PENDING: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1315 | return STATUS_PENDING; |
| 1316 | |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1317 | case Downloads.Impl.STATUS_RUNNING: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1318 | return STATUS_RUNNING; |
| 1319 | |
Steve Howard | 3e8c1d3 | 2010-09-29 17:03:32 -0700 | [diff] [blame] | 1320 | case Downloads.Impl.STATUS_PAUSED_BY_APP: |
| 1321 | case Downloads.Impl.STATUS_WAITING_TO_RETRY: |
| 1322 | case Downloads.Impl.STATUS_WAITING_FOR_NETWORK: |
| 1323 | case Downloads.Impl.STATUS_QUEUED_FOR_WIFI: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1324 | return STATUS_PAUSED; |
| 1325 | |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1326 | case Downloads.Impl.STATUS_SUCCESS: |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1327 | return STATUS_SUCCESSFUL; |
| 1328 | |
| 1329 | default: |
Vasu Nori | ef7e33b | 2010-10-20 13:26:02 -0700 | [diff] [blame] | 1330 | assert Downloads.Impl.isStatusError(status); |
Steve Howard | a270936 | 2010-07-02 17:12:48 -0700 | [diff] [blame] | 1331 | return STATUS_FAILED; |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | } |