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