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