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