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