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