1 /*
2  * Copyright (C) 2008 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 
17 package android.provider;
18 
19 import android.app.DownloadManager;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.content.Context;
22 import android.net.NetworkPolicyManager;
23 import android.net.Uri;
24 
25 /**
26  * The Download Manager
27  *
28  * @pending
29  */
30 public final class Downloads {
Downloads()31     private Downloads() {}
32 
33     /**
34      * Implementation details
35      *
36      * Exposes constants used to interact with the download manager's
37      * content provider.
38      * The constants URI ... STATUS are the names of columns in the downloads table.
39      *
40      * @hide
41      */
42     public static final class Impl implements BaseColumns {
Impl()43         private Impl() {}
44 
45         public static final String AUTHORITY = "downloads";
46 
47         /**
48          * The permission to access the download manager
49          */
50         public static final String PERMISSION_ACCESS = "android.permission.ACCESS_DOWNLOAD_MANAGER";
51 
52         /**
53          * The permission to access the download manager's advanced functions
54          */
55         public static final String PERMISSION_ACCESS_ADVANCED =
56                 "android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED";
57 
58         /**
59          * The permission to access the all the downloads in the manager.
60          */
61         public static final String PERMISSION_ACCESS_ALL =
62                 "android.permission.ACCESS_ALL_DOWNLOADS";
63 
64         /**
65          * The permission to directly access the download manager's cache
66          * directory
67          */
68         public static final String PERMISSION_CACHE = "android.permission.ACCESS_CACHE_FILESYSTEM";
69 
70         /**
71          * The permission to send broadcasts on download completion
72          */
73         public static final String PERMISSION_SEND_INTENTS =
74                 "android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS";
75 
76         /**
77          * The permission to download files to the cache partition that won't be automatically
78          * purged when space is needed.
79          */
80         public static final String PERMISSION_CACHE_NON_PURGEABLE =
81                 "android.permission.DOWNLOAD_CACHE_NON_PURGEABLE";
82 
83         /**
84          * The permission to download files without any system notification being shown.
85          */
86         public static final String PERMISSION_NO_NOTIFICATION =
87                 "android.permission.DOWNLOAD_WITHOUT_NOTIFICATION";
88 
89         /**
90          * The content:// URI to access downloads owned by the caller's UID.
91          */
92         @UnsupportedAppUsage
93         public static final Uri CONTENT_URI =
94                 Uri.parse("content://downloads/my_downloads");
95 
96         /**
97          * The content URI for accessing all downloads across all UIDs (requires the
98          * ACCESS_ALL_DOWNLOADS permission).
99          */
100         @UnsupportedAppUsage
101         public static final Uri ALL_DOWNLOADS_CONTENT_URI =
102                 Uri.parse("content://downloads/all_downloads");
103 
104         /** URI segment to access a publicly accessible downloaded file */
105         public static final String PUBLICLY_ACCESSIBLE_DOWNLOADS_URI_SEGMENT = "public_downloads";
106 
107         /**
108          * The content URI for accessing publicly accessible downloads (i.e., it requires no
109          * permissions to access this downloaded file)
110          */
111         @UnsupportedAppUsage
112         public static final Uri PUBLICLY_ACCESSIBLE_DOWNLOADS_URI =
113                 Uri.parse("content://downloads/" + PUBLICLY_ACCESSIBLE_DOWNLOADS_URI_SEGMENT);
114 
115         /**
116          * Broadcast Action: this is sent by the download manager to the app
117          * that had initiated a download when that download completes. The
118          * download's content: uri is specified in the intent's data.
119          */
120         public static final String ACTION_DOWNLOAD_COMPLETED =
121                 DownloadManager.ACTION_DOWNLOAD_COMPLETED;
122 
123         /**
124          * Broadcast Action: this is sent by the download manager to the app
125          * that had initiated a download when the user selects the notification
126          * associated with that download. The download's content: uri is specified
127          * in the intent's data if the click is associated with a single download,
128          * or Downloads.CONTENT_URI if the notification is associated with
129          * multiple downloads.
130          * Note: this is not currently sent for downloads that have completed
131          * successfully.
132          */
133         public static final String ACTION_NOTIFICATION_CLICKED =
134                 DownloadManager.ACTION_NOTIFICATION_CLICKED;
135 
136         /**
137          * The name of the column containing the URI of the data being downloaded.
138          * <P>Type: TEXT</P>
139          * <P>Owner can Init/Read</P>
140          */
141         @UnsupportedAppUsage
142         public static final String COLUMN_URI = "uri";
143 
144         /**
145          * The name of the column containing application-specific data.
146          * <P>Type: TEXT</P>
147          * <P>Owner can Init/Read/Write</P>
148          */
149         public static final String COLUMN_APP_DATA = "entity";
150 
151         /**
152          * The name of the column containing the flags that indicates whether
153          * the initiating application is capable of verifying the integrity of
154          * the downloaded file. When this flag is set, the download manager
155          * performs downloads and reports success even in some situations where
156          * it can't guarantee that the download has completed (e.g. when doing
157          * a byte-range request without an ETag, or when it can't determine
158          * whether a download fully completed).
159          * <P>Type: BOOLEAN</P>
160          * <P>Owner can Init</P>
161          */
162         public static final String COLUMN_NO_INTEGRITY = "no_integrity";
163 
164         /**
165          * The name of the column containing the filename that the initiating
166          * application recommends. When possible, the download manager will attempt
167          * to use this filename, or a variation, as the actual name for the file.
168          * <P>Type: TEXT</P>
169          * <P>Owner can Init</P>
170          */
171         @UnsupportedAppUsage
172         public static final String COLUMN_FILE_NAME_HINT = "hint";
173 
174         /**
175          * The name of the column containing the filename where the downloaded data
176          * was actually stored.
177          * <P>Type: TEXT</P>
178          * <P>Owner can Read</P>
179          */
180         public static final String _DATA = "_data";
181 
182         /**
183          * The name of the column containing the MIME type of the downloaded data.
184          * <P>Type: TEXT</P>
185          * <P>Owner can Init/Read</P>
186          */
187         @UnsupportedAppUsage
188         public static final String COLUMN_MIME_TYPE = "mimetype";
189 
190         /**
191          * The name of the column containing the flag that controls the destination
192          * of the download. See the DESTINATION_* constants for a list of legal values.
193          * <P>Type: INTEGER</P>
194          * <P>Owner can Init</P>
195          */
196         @UnsupportedAppUsage
197         public static final String COLUMN_DESTINATION = "destination";
198 
199         /**
200          * The name of the column containing the flags that controls whether the
201          * download is displayed by the UI. See the VISIBILITY_* constants for
202          * a list of legal values.
203          * <P>Type: INTEGER</P>
204          * <P>Owner can Init/Read/Write</P>
205          */
206         @UnsupportedAppUsage
207         public static final String COLUMN_VISIBILITY = "visibility";
208 
209         /**
210          * The name of the column containing the current control state  of the download.
211          * Applications can write to this to control (pause/resume) the download.
212          * the CONTROL_* constants for a list of legal values.
213          * <P>Type: INTEGER</P>
214          * <P>Owner can Read</P>
215          */
216         public static final String COLUMN_CONTROL = "control";
217 
218         /**
219          * The name of the column containing the current status of the download.
220          * Applications can read this to follow the progress of each download. See
221          * the STATUS_* constants for a list of legal values.
222          * <P>Type: INTEGER</P>
223          * <P>Owner can Read</P>
224          */
225         public static final String COLUMN_STATUS = "status";
226 
227         /**
228          * The name of the column containing the date at which some interesting
229          * status changed in the download. Stored as a System.currentTimeMillis()
230          * value.
231          * <P>Type: BIGINT</P>
232          * <P>Owner can Read</P>
233          */
234         public static final String COLUMN_LAST_MODIFICATION = "lastmod";
235 
236         /**
237          * The name of the column containing the package name of the application
238          * that initiating the download. The download manager will send
239          * notifications to a component in this package when the download completes.
240          * <P>Type: TEXT</P>
241          * <P>Owner can Init/Read</P>
242          */
243         @UnsupportedAppUsage
244         public static final String COLUMN_NOTIFICATION_PACKAGE = "notificationpackage";
245 
246         /**
247          * The name of the column containing the component name of the class that
248          * will receive notifications associated with the download. The
249          * package/class combination is passed to
250          * Intent.setClassName(String,String).
251          * <P>Type: TEXT</P>
252          * <P>Owner can Init/Read</P>
253          */
254         @UnsupportedAppUsage
255         public static final String COLUMN_NOTIFICATION_CLASS = "notificationclass";
256 
257         /**
258          * If extras are specified when requesting a download they will be provided in the intent that
259          * is sent to the specified class and package when a download has finished.
260          * <P>Type: TEXT</P>
261          * <P>Owner can Init</P>
262          */
263         @UnsupportedAppUsage
264         public static final String COLUMN_NOTIFICATION_EXTRAS = "notificationextras";
265 
266         /**
267          * The name of the column contain the values of the cookie to be used for
268          * the download. This is used directly as the value for the Cookie: HTTP
269          * header that gets sent with the request.
270          * <P>Type: TEXT</P>
271          * <P>Owner can Init</P>
272          */
273         @UnsupportedAppUsage
274         public static final String COLUMN_COOKIE_DATA = "cookiedata";
275 
276         /**
277          * The name of the column containing the user agent that the initiating
278          * application wants the download manager to use for this download.
279          * <P>Type: TEXT</P>
280          * <P>Owner can Init</P>
281          */
282         public static final String COLUMN_USER_AGENT = "useragent";
283 
284         /**
285          * The name of the column containing the referer (sic) that the initiating
286          * application wants the download manager to use for this download.
287          * <P>Type: TEXT</P>
288          * <P>Owner can Init</P>
289          */
290         @UnsupportedAppUsage
291         public static final String COLUMN_REFERER = "referer";
292 
293         /**
294          * The name of the column containing the total size of the file being
295          * downloaded.
296          * <P>Type: INTEGER</P>
297          * <P>Owner can Read</P>
298          */
299         public static final String COLUMN_TOTAL_BYTES = "total_bytes";
300 
301         /**
302          * The name of the column containing the size of the part of the file that
303          * has been downloaded so far.
304          * <P>Type: INTEGER</P>
305          * <P>Owner can Read</P>
306          */
307         public static final String COLUMN_CURRENT_BYTES = "current_bytes";
308 
309         /**
310          * The name of the column where the initiating application can provide the
311          * UID of another application that is allowed to access this download. If
312          * multiple applications share the same UID, all those applications will be
313          * allowed to access this download. This column can be updated after the
314          * download is initiated. This requires the permission
315          * android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED.
316          * <P>Type: INTEGER</P>
317          * <P>Owner can Init</P>
318          */
319         public static final String COLUMN_OTHER_UID = "otheruid";
320 
321         /**
322          * The name of the column where the initiating application can provided the
323          * title of this download. The title will be displayed ito the user in the
324          * list of downloads.
325          * <P>Type: TEXT</P>
326          * <P>Owner can Init/Read/Write</P>
327          */
328         @UnsupportedAppUsage
329         public static final String COLUMN_TITLE = "title";
330 
331         /**
332          * The name of the column where the initiating application can provide the
333          * description of this download. The description will be displayed to the
334          * user in the list of downloads.
335          * <P>Type: TEXT</P>
336          * <P>Owner can Init/Read/Write</P>
337          */
338         @UnsupportedAppUsage
339         public static final String COLUMN_DESCRIPTION = "description";
340 
341         /**
342          * The name of the column indicating whether the download was requesting through the public
343          * API.  This controls some differences in behavior.
344          * <P>Type: BOOLEAN</P>
345          * <P>Owner can Init/Read</P>
346          */
347         @UnsupportedAppUsage
348         public static final String COLUMN_IS_PUBLIC_API = "is_public_api";
349 
350         /**
351          * The name of the column holding a bitmask of allowed network types.  This is only used for
352          * public API downloads.
353          * <P>Type: INTEGER</P>
354          * <P>Owner can Init/Read</P>
355          */
356         @UnsupportedAppUsage
357         public static final String COLUMN_ALLOWED_NETWORK_TYPES = "allowed_network_types";
358 
359         /**
360          * The name of the column indicating whether roaming connections can be used.  This is only
361          * used for public API downloads.
362          * <P>Type: BOOLEAN</P>
363          * <P>Owner can Init/Read</P>
364          */
365         @UnsupportedAppUsage
366         public static final String COLUMN_ALLOW_ROAMING = "allow_roaming";
367 
368         /**
369          * The name of the column indicating whether metered connections can be used.  This is only
370          * used for public API downloads.
371          * <P>Type: BOOLEAN</P>
372          * <P>Owner can Init/Read</P>
373          */
374         public static final String COLUMN_ALLOW_METERED = "allow_metered";
375 
376         /**
377          * Whether or not this download should be displayed in the system's Downloads UI.  Defaults
378          * to true.
379          * <P>Type: INTEGER</P>
380          * <P>Owner can Init/Read</P>
381          */
382         @UnsupportedAppUsage
383         public static final String COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI = "is_visible_in_downloads_ui";
384 
385         /**
386          * If true, the user has confirmed that this download can proceed over the mobile network
387          * even though it exceeds the recommended maximum size.
388          * <P>Type: BOOLEAN</P>
389          */
390         public static final String COLUMN_BYPASS_RECOMMENDED_SIZE_LIMIT =
391             "bypass_recommended_size_limit";
392 
393         /**
394          * Set to true if this download is deleted. It is completely removed from the database
395          * when MediaProvider database also deletes the metadata asociated with this downloaded file.
396          * <P>Type: BOOLEAN</P>
397          * <P>Owner can Read</P>
398          */
399         @UnsupportedAppUsage
400         public static final String COLUMN_DELETED = "deleted";
401 
402         /**
403          * The URI to the corresponding entry in MediaProvider for this downloaded entry. It is
404          * used to delete the entries from MediaProvider database when it is deleted from the
405          * downloaded list.
406          * <P>Type: TEXT</P>
407          * <P>Owner can Read</P>
408          */
409         public static final String COLUMN_MEDIAPROVIDER_URI = "mediaprovider_uri";
410 
411         /**
412          * Similar to {@link #COLUMN_MEDIAPROVIDER_URI}, except this cannot be updated/queried
413          * by apps and will be the source of truth when updating/deleting download entries in
414          * MediaProvider database.
415          *
416          * <P>Type: TEXT</P>
417          */
418         public static final String COLUMN_MEDIASTORE_URI = "mediastore_uri";
419 
420         /**
421          * The column that is used to remember whether the media scanner was invoked.
422          * It can take the values: {@link #MEDIA_NOT_SCANNED}, {@link #MEDIA_SCANNED} or
423          * {@link #MEDIA_NOT_SCANNABLE} or {@code null}. If it's value is {@code null}, it will be
424          * treated as {@link #MEDIA_NOT_SCANNED}.
425          *
426          * <P>Type: TEXT</P>
427          */
428         @UnsupportedAppUsage
429         public static final String COLUMN_MEDIA_SCANNED = "scanned";
430 
431         /** Possible values for column {@link #COLUMN_MEDIA_SCANNED} */
432         public static final int MEDIA_NOT_SCANNED = 0;
433         public static final int MEDIA_SCANNED = 1;
434         public static final int MEDIA_NOT_SCANNABLE = 2;
435 
436         /**
437          * The column with errorMsg for a failed downloaded.
438          * Used only for debugging purposes.
439          * <P>Type: TEXT</P>
440          */
441         public static final String COLUMN_ERROR_MSG = "errorMsg";
442 
443         /**
444          *  This column stores the source of the last update to this row.
445          *  This column is only for internal use.
446          *  Valid values are indicated by LAST_UPDATESRC_* constants.
447          * <P>Type: INT</P>
448          */
449         public static final String COLUMN_LAST_UPDATESRC = "lastUpdateSrc";
450 
451         /** The column that is used to count retries */
452         public static final String COLUMN_FAILED_CONNECTIONS = "numfailed";
453 
454         public static final String COLUMN_ALLOW_WRITE = "allow_write";
455 
456         public static final int FLAG_REQUIRES_CHARGING = 1 << 0;
457         public static final int FLAG_REQUIRES_DEVICE_IDLE = 1 << 1;
458 
459         public static final String COLUMN_FLAGS = "flags";
460 
461         /**
462          * default value for {@link #COLUMN_LAST_UPDATESRC}.
463          * This value is used when this column's value is not relevant.
464          */
465         public static final int LAST_UPDATESRC_NOT_RELEVANT = 0;
466 
467         /**
468          * One of the values taken by {@link #COLUMN_LAST_UPDATESRC}.
469          * This value is used when the update is NOT to be relayed to the DownloadService
470          * (and thus spare DownloadService from scanning the database when this change occurs)
471          */
472         public static final int LAST_UPDATESRC_DONT_NOTIFY_DOWNLOADSVC = 1;
473 
474         /*
475          * Lists the destinations that an application can specify for a download.
476          */
477 
478         /**
479          * This download will be saved to the external storage. This is the
480          * default behavior, and should be used for any file that the user
481          * can freely access, copy, delete. Even with that destination,
482          * unencrypted DRM files are saved in secure internal storage.
483          * Downloads to the external destination only write files for which
484          * there is a registered handler. The resulting files are accessible
485          * by filename to all applications.
486          */
487         public static final int DESTINATION_EXTERNAL = 0;
488 
489         /**
490          * This download will be saved to the download manager's private
491          * partition. This is the behavior used by applications that want to
492          * download private files that are used and deleted soon after they
493          * get downloaded. All file types are allowed, and only the initiating
494          * application can access the file (indirectly through a content
495          * provider). This requires the
496          * android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED permission.
497          */
498         public static final int DESTINATION_CACHE_PARTITION = 1;
499 
500         /**
501          * This download will be saved to the download manager's private
502          * partition and will be purged as necessary to make space. This is
503          * for private files (similar to CACHE_PARTITION) that aren't deleted
504          * immediately after they are used, and are kept around by the download
505          * manager as long as space is available.
506          */
507         @UnsupportedAppUsage
508         public static final int DESTINATION_CACHE_PARTITION_PURGEABLE = 2;
509 
510         /**
511          * This download will be saved to the download manager's private
512          * partition, as with DESTINATION_CACHE_PARTITION, but the download
513          * will not proceed if the user is on a roaming data connection.
514          */
515         public static final int DESTINATION_CACHE_PARTITION_NOROAMING = 3;
516 
517         /**
518          * This download will be saved to the location given by the file URI in
519          * {@link #COLUMN_FILE_NAME_HINT}.
520          */
521         @UnsupportedAppUsage
522         public static final int DESTINATION_FILE_URI = 4;
523 
524         /**
525          * This download will be saved to the system cache ("/cache")
526          * partition. This option is only used by system apps and so it requires
527          * android.permission.ACCESS_CACHE_FILESYSTEM permission.
528          */
529         @Deprecated
530         public static final int DESTINATION_SYSTEMCACHE_PARTITION = 5;
531 
532         /**
533          * This download was completed by the caller (i.e., NOT downloadmanager)
534          * and caller wants to have this download displayed in Downloads App.
535          */
536         public static final int DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD = 6;
537 
538         /**
539          * This download is allowed to run.
540          */
541         public static final int CONTROL_RUN = 0;
542 
543         /**
544          * This download must pause at the first opportunity.
545          */
546         public static final int CONTROL_PAUSED = 1;
547 
548         /*
549          * Lists the states that the download manager can set on a download
550          * to notify applications of the download progress.
551          * The codes follow the HTTP families:<br>
552          * 1xx: informational<br>
553          * 2xx: success<br>
554          * 3xx: redirects (not used by the download manager)<br>
555          * 4xx: client errors<br>
556          * 5xx: server errors
557          */
558 
559         /**
560          * Returns whether the status is informational (i.e. 1xx).
561          */
isStatusInformational(int status)562         public static boolean isStatusInformational(int status) {
563             return (status >= 100 && status < 200);
564         }
565 
566         /**
567          * Returns whether the status is a success (i.e. 2xx).
568          */
569         @UnsupportedAppUsage
isStatusSuccess(int status)570         public static boolean isStatusSuccess(int status) {
571             return (status >= 200 && status < 300);
572         }
573 
574         /**
575          * Returns whether the status is an error (i.e. 4xx or 5xx).
576          */
577         @UnsupportedAppUsage
isStatusError(int status)578         public static boolean isStatusError(int status) {
579             return (status >= 400 && status < 600);
580         }
581 
582         /**
583          * Returns whether the status is a client error (i.e. 4xx).
584          */
isStatusClientError(int status)585         public static boolean isStatusClientError(int status) {
586             return (status >= 400 && status < 500);
587         }
588 
589         /**
590          * Returns whether the status is a server error (i.e. 5xx).
591          */
isStatusServerError(int status)592         public static boolean isStatusServerError(int status) {
593             return (status >= 500 && status < 600);
594         }
595 
596         /**
597          * this method determines if a notification should be displayed for a
598          * given {@link #COLUMN_VISIBILITY} value
599          * @param visibility the value of {@link #COLUMN_VISIBILITY}.
600          * @return true if the notification should be displayed. false otherwise.
601          */
602         @UnsupportedAppUsage
isNotificationToBeDisplayed(int visibility)603         public static boolean isNotificationToBeDisplayed(int visibility) {
604             return visibility == DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED ||
605                     visibility == DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION;
606         }
607 
608         /**
609          * Returns whether the download has completed (either with success or
610          * error).
611          */
612         @UnsupportedAppUsage
isStatusCompleted(int status)613         public static boolean isStatusCompleted(int status) {
614             return (status >= 200 && status < 300) || (status >= 400 && status < 600);
615         }
616 
617         /**
618          * This download hasn't stated yet
619          */
620         public static final int STATUS_PENDING = 190;
621 
622         /**
623          * This download has started
624          */
625         public static final int STATUS_RUNNING = 192;
626 
627         /**
628          * This download has been paused by the owning app.
629          */
630         public static final int STATUS_PAUSED_BY_APP = 193;
631 
632         /**
633          * This download encountered some network error and is waiting before retrying the request.
634          */
635         public static final int STATUS_WAITING_TO_RETRY = 194;
636 
637         /**
638          * This download is waiting for network connectivity to proceed.
639          */
640         public static final int STATUS_WAITING_FOR_NETWORK = 195;
641 
642         /**
643          * This download exceeded a size limit for mobile networks and is waiting for a Wi-Fi
644          * connection to proceed.
645          */
646         public static final int STATUS_QUEUED_FOR_WIFI = 196;
647 
648         /**
649          * This download couldn't be completed due to insufficient storage
650          * space.  Typically, this is because the SD card is full.
651          */
652         public static final int STATUS_INSUFFICIENT_SPACE_ERROR = 198;
653 
654         /**
655          * This download couldn't be completed because no external storage
656          * device was found.  Typically, this is because the SD card is not
657          * mounted.
658          */
659         public static final int STATUS_DEVICE_NOT_FOUND_ERROR = 199;
660 
661         /**
662          * This download has successfully completed.
663          * Warning: there might be other status values that indicate success
664          * in the future.
665          * Use isStatusSuccess() to capture the entire category.
666          */
667         public static final int STATUS_SUCCESS = 200;
668 
669         /**
670          * This request couldn't be parsed. This is also used when processing
671          * requests with unknown/unsupported URI schemes.
672          */
673         public static final int STATUS_BAD_REQUEST = 400;
674 
675         /**
676          * This download can't be performed because the content type cannot be
677          * handled.
678          */
679         public static final int STATUS_NOT_ACCEPTABLE = 406;
680 
681         /**
682          * This download cannot be performed because the length cannot be
683          * determined accurately. This is the code for the HTTP error "Length
684          * Required", which is typically used when making requests that require
685          * a content length but don't have one, and it is also used in the
686          * client when a response is received whose length cannot be determined
687          * accurately (therefore making it impossible to know when a download
688          * completes).
689          */
690         public static final int STATUS_LENGTH_REQUIRED = 411;
691 
692         /**
693          * This download was interrupted and cannot be resumed.
694          * This is the code for the HTTP error "Precondition Failed", and it is
695          * also used in situations where the client doesn't have an ETag at all.
696          */
697         public static final int STATUS_PRECONDITION_FAILED = 412;
698 
699         /**
700          * The lowest-valued error status that is not an actual HTTP status code.
701          */
702         public static final int MIN_ARTIFICIAL_ERROR_STATUS = 488;
703 
704         /**
705          * The requested destination file already exists.
706          */
707         public static final int STATUS_FILE_ALREADY_EXISTS_ERROR = 488;
708 
709         /**
710          * Some possibly transient error occurred, but we can't resume the download.
711          */
712         public static final int STATUS_CANNOT_RESUME = 489;
713 
714         /**
715          * This download was canceled
716          */
717         public static final int STATUS_CANCELED = 490;
718 
719         /**
720          * This download has completed with an error.
721          * Warning: there will be other status values that indicate errors in
722          * the future. Use isStatusError() to capture the entire category.
723          */
724         public static final int STATUS_UNKNOWN_ERROR = 491;
725 
726         /**
727          * This download couldn't be completed because of a storage issue.
728          * Typically, that's because the filesystem is missing or full.
729          * Use the more specific {@link #STATUS_INSUFFICIENT_SPACE_ERROR}
730          * and {@link #STATUS_DEVICE_NOT_FOUND_ERROR} when appropriate.
731          */
732         public static final int STATUS_FILE_ERROR = 492;
733 
734         /**
735          * This download couldn't be completed because of an HTTP
736          * redirect response that the download manager couldn't
737          * handle.
738          */
739         public static final int STATUS_UNHANDLED_REDIRECT = 493;
740 
741         /**
742          * This download couldn't be completed because of an
743          * unspecified unhandled HTTP code.
744          */
745         public static final int STATUS_UNHANDLED_HTTP_CODE = 494;
746 
747         /**
748          * This download couldn't be completed because of an
749          * error receiving or processing data at the HTTP level.
750          */
751         public static final int STATUS_HTTP_DATA_ERROR = 495;
752 
753         /**
754          * This download couldn't be completed because of an
755          * HttpException while setting up the request.
756          */
757         public static final int STATUS_HTTP_EXCEPTION = 496;
758 
759         /**
760          * This download couldn't be completed because there were
761          * too many redirects.
762          */
763         public static final int STATUS_TOO_MANY_REDIRECTS = 497;
764 
765         /**
766          * This download has failed because requesting application has been
767          * blocked by {@link NetworkPolicyManager}.
768          *
769          * @hide
770          * @deprecated since behavior now uses
771          *             {@link #STATUS_WAITING_FOR_NETWORK}
772          */
773         @Deprecated
774         public static final int STATUS_BLOCKED = 498;
775 
776         /** {@hide} */
statusToString(int status)777         public static String statusToString(int status) {
778             switch (status) {
779                 case STATUS_PENDING: return "PENDING";
780                 case STATUS_RUNNING: return "RUNNING";
781                 case STATUS_PAUSED_BY_APP: return "PAUSED_BY_APP";
782                 case STATUS_WAITING_TO_RETRY: return "WAITING_TO_RETRY";
783                 case STATUS_WAITING_FOR_NETWORK: return "WAITING_FOR_NETWORK";
784                 case STATUS_QUEUED_FOR_WIFI: return "QUEUED_FOR_WIFI";
785                 case STATUS_INSUFFICIENT_SPACE_ERROR: return "INSUFFICIENT_SPACE_ERROR";
786                 case STATUS_DEVICE_NOT_FOUND_ERROR: return "DEVICE_NOT_FOUND_ERROR";
787                 case STATUS_SUCCESS: return "SUCCESS";
788                 case STATUS_BAD_REQUEST: return "BAD_REQUEST";
789                 case STATUS_NOT_ACCEPTABLE: return "NOT_ACCEPTABLE";
790                 case STATUS_LENGTH_REQUIRED: return "LENGTH_REQUIRED";
791                 case STATUS_PRECONDITION_FAILED: return "PRECONDITION_FAILED";
792                 case STATUS_FILE_ALREADY_EXISTS_ERROR: return "FILE_ALREADY_EXISTS_ERROR";
793                 case STATUS_CANNOT_RESUME: return "CANNOT_RESUME";
794                 case STATUS_CANCELED: return "CANCELED";
795                 case STATUS_UNKNOWN_ERROR: return "UNKNOWN_ERROR";
796                 case STATUS_FILE_ERROR: return "FILE_ERROR";
797                 case STATUS_UNHANDLED_REDIRECT: return "UNHANDLED_REDIRECT";
798                 case STATUS_UNHANDLED_HTTP_CODE: return "UNHANDLED_HTTP_CODE";
799                 case STATUS_HTTP_DATA_ERROR: return "HTTP_DATA_ERROR";
800                 case STATUS_HTTP_EXCEPTION: return "HTTP_EXCEPTION";
801                 case STATUS_TOO_MANY_REDIRECTS: return "TOO_MANY_REDIRECTS";
802                 case STATUS_BLOCKED: return "BLOCKED";
803                 default: return Integer.toString(status);
804             }
805         }
806 
807         /**
808          * This download is visible but only shows in the notifications
809          * while it's in progress.
810          */
811         public static final int VISIBILITY_VISIBLE = DownloadManager.Request.VISIBILITY_VISIBLE;
812 
813         /**
814          * This download is visible and shows in the notifications while
815          * in progress and after completion.
816          */
817         public static final int VISIBILITY_VISIBLE_NOTIFY_COMPLETED =
818                 DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED;
819 
820         /**
821          * This download doesn't show in the UI or in the notifications.
822          */
823         public static final int VISIBILITY_HIDDEN = DownloadManager.Request.VISIBILITY_HIDDEN;
824 
825         /**
826          * Constants related to HTTP request headers associated with each download.
827          */
828         public static class RequestHeaders {
829             public static final String HEADERS_DB_TABLE = "request_headers";
830             public static final String COLUMN_DOWNLOAD_ID = "download_id";
831             public static final String COLUMN_HEADER = "header";
832             public static final String COLUMN_VALUE = "value";
833 
834             /**
835              * Path segment to add to a download URI to retrieve request headers
836              */
837             public static final String URI_SEGMENT = "headers";
838 
839             /**
840              * Prefix for ContentValues keys that contain HTTP header lines, to be passed to
841              * DownloadProvider.insert().
842              */
843             @UnsupportedAppUsage
844             public static final String INSERT_KEY_PREFIX = "http_header_";
845         }
846     }
847 
848     /** @hide */
849     public static final String CALL_MEDIASTORE_DOWNLOADS_DELETED = "mediastore_downloads_deleted";
850     /** @hide */
851     public static final String CALL_CREATE_EXTERNAL_PUBLIC_DIR = "create_external_public_dir";
852     /** @hide */
853     public static final String CALL_REVOKE_MEDIASTORE_URI_PERMS = "revoke_mediastore_uri_perms";
854 
855     /** @hide */
856     public static final String EXTRA_IDS = "ids";
857     /** @hide */
858     public static final String EXTRA_MIME_TYPES = "mime_types";
859     /** @hide */
860     public static final String DIR_TYPE = "dir_type";
861 
862     /**
863      * Query where clause for general querying.
864      */
865     private static final String QUERY_WHERE_CLAUSE = Impl.COLUMN_NOTIFICATION_PACKAGE + "=? AND "
866             + Impl.COLUMN_NOTIFICATION_CLASS + "=?";
867 
868     /**
869      * Delete all the downloads for a package/class pair.
870      */
removeAllDownloadsByPackage( Context context, String notification_package, String notification_class)871     public static final void removeAllDownloadsByPackage(
872             Context context, String notification_package, String notification_class) {
873         context.getContentResolver().delete(Impl.CONTENT_URI, QUERY_WHERE_CLAUSE,
874                 new String[] { notification_package, notification_class });
875     }
876 }
877