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