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