Download Provider

Contents


High-level requirements

Requirements for Android

Req# Description Detailed Requirements Status
DLMGR_A_1 The Download Manager MUST satisfy the basic download needs of OTA Updates.   YES done in 1.0
DLMGR_A_2 The Download Manager MUST satisfy the basic download needs of Market.   YES done in 1.0
DLMGR_A_3 The Download Manager MUST satisfy the basic download needs of Gmail.   YES done in 1.0
DLMGR_A_4 The Download Manager MUST satisfy the basic download needs of the Browser.   YES done in 1.0
DLMGR_A_5 Downloads MUST happen and continue in the background, independently of the application in front.   YES done in 1.0
DLMGR_A_6 Downloads MUST be reliable even on unreliable network connections, within the capabilities of the underlying protocol and of the server.   YES done in 1.0
DLMGR_A_7 User-accessible files MUST be stored on the external storage card, and only files that are explicitly supported by an installed application must be downloaded.   YES done in 1.0
DLMGR_A_8 OMA downloads SHOULD be supported.   NO deferred beyond Cupcake (not enough time)
DLMGR_A_9 The Download Manager SHOULD only download when using high-speed (3G/wifi) links, or only when not roaming (if that can be detected).   NO deferred beyond Cupcake (not enough time)
DLMGR_A_10 Downloads SHOULD be user-controllable (if allowed by the initiating application), including pause, resume, and restart of failed downloads. 4001 NO deferred beyond Cupcake (not enough time, need precise specifications)

Requirements for Cupcake

Req# Description Detailed Requirements Status
DLMGR_C_1 The Download Manager SHOULD resume downloads where the socket got cleanly closed while the download was incomplete (common behavior on proxies and Google servers) 2005 YES done in Cupcake
DLMGR_C_2 The Download Manager SHOULD retry/resume downloads instead of aborting when the server returns a HTTP code 503 2006 YES done in Cupcake
DLMGR_C_3 The Download Manager SHOULD randomize the retry delay 2007 YES done in Cupcake
DLMGR_C_4 The Download Manager SHOULD use the retry-after header (delta-seconds) for 503 responses 2008 YES done in Cupcake
DLMGR_C_5 The Download Manager MAY hide columns that aren't strictly necessary 1010 YES done in Cupcake
DLMGR_C_6 The Download Manager SHOULD allow the initiating app to pause an ongoing download 1011 YES done in Cupcake
DLMGR_C_7 The Download Manager SHOULD not display multiple notification icons for completed downloads. 4002 NO deferred beyond Cupcake (no precise specifications, need framework support)
DLMGR_C_8 The Download Manager SHOULD delete old files from /cache 3006 NO deferred beyond Cupcake (no precise specifications)
DLMGR_C_9 The Download Manager SHOULD handle redirects 2009 YES done in Cupcake

Requirements for Donut

Req# Description Detailed Requirements Status

Technology Evaluation

ALSO See also future directions for possible additional technical changes.

Possible scope for Cupcake

Because there is no testing environment in place in the 1.0 code, and because the schedule between 1.0 and Cupcake doesn't leave enough time to develop a testing environment solid enough to be able to test the database upgrades from 1.0 database scheme to a new scheme, any work done in Cupcake will have to work within the existing 1.0 database scheme.

Reducing the number of classes

Each class in the system has a measurable RAM cost (because of the associated Class objects), and therefore reducing the number of classes when possible or relevant can reduce the memory requirements. That being said, classes that extend system classes and are necessary for the operation of the download manager can't be removed.

Class Comments
com.android.providers.downloads.Constants Only contains constants, can be merged into another class.
com.android.providers.downloads.DownloadFileInfo Should be merged with DownloadInfo.
com.android.providers.downloads.DownloadInfo Once we only store information in RAM about downloads that are explicitly active, can be merged with DownloadThread.
com.android.providers.downloads.DownloadNotification Looks like it could be merged with DownloadProvider or DownloadService.
com.android.providers.downloads.DownloadNotification.NotificationItem Can probably be eliminated by using queries intelligently.
com.android.providers.downloads.DownloadProvider Extends ContentProvider, can't be eliminated.
com.android.providers.downloads.DownloadProvider.DatabaseHelper Can probably be eliminated by re-implementing by hand the logic of SQLiteOpenHelper.
com.android.providers.downloads.DownloadProvider.ReadOnlyCursorWrapper Can be eliminated once Cursor is read-only system-wide.
com.android.providers.downloads.DownloadReceiver Extends BroadcastReceiver, can't be eliminated.
com.android.providers.downloads.DownloadService Extends Service, unlikely that this can be eliminated. TBD.
com.android.providers.downloads.DownloadService.DownloadManagerContentObserver Extends ContentObserver, can be eliminated if the download manager can be re-architected to not depend on ContentObserver any more.
com.android.providers.downloads.DownloadService.MediaScannerConnection Can probably be merged into another class.
com.android.providers.downloads.DownloadService.UpdateThread Can probably be made to implement Runnable instead and merged into another class, can be eliminated if the download manager can be re-architected to not depend on ContentObserver any more.
com.android.providers.downloads.DownloadThread Can probably be made to implement Runnable instead. Unclear whether this can be eliminated as we will probably need one object that represents an ongoing download (unless the entire state can be stored on the stack with primitive types, which is unlikely).
com.android.providers.downloads.Helpers Can't be instantiated, can be merged into another class.
com.android.providers.downloads.Helpers.Lexer Keeps state about an ongoing lex, can probably be merged into another class by making the lexer synchronized, since the operation is short-lived.

Reducing the list of visible columns

Security in the download provider is primarily enforced with two separate mechanisms:

The first mechanism is expected to be fairly robust (the implementation is quite simple, based on projection maps, which are highly structured), but the second one relies on arbitrary strings (URIs and SQL fragments) passed by applications and is therefore at a higher risk of being compromised. Therefore, sensitive information stored in unrestricted columns (for which the first mechanism doesn't apply) is at a greater risk than other information.

Here's the list of columns that can currently be read/queried, with comments:

Column Notes
_ID Needs to be visible so that the app can uniquely identify downloads. No security concern: those numbers are sequential and aren't hard to guess.
_DATA Probably should not be visible to applications. WARNING Security concern: This holds filenames, including those of private files. While file permissions are supposed to kick in and protect the files, hiding private filenames deeper in would probably be a reasonable idea.
MIMETYPE Needs to be visible so that app can display the icon matching the mime type. Intended to be visible by 3rd-party download UIs. TODO Security TBD before we implement support for 3rd-party UIs.
VISIBILITY Needs to be visible in case an app has both visible and invisible downloads. No obvious security concern.
DESTINATION Needs to be visible in case an app has multiple destinations and wants to distinguish between them. Also used internally by the download manager. No obvious security concern.
STATUS Needs to be visible (1004). No obvious security concern.
LAST_MODIFICATION Needs to be visible, e.g. so that apps can sort downloads by date of last activity, or discard old downloads. No obvious security concern.
NOTIFICATION_PACKAGE Allows individual apps running under shared UIDs to identify their own downloads. No security concern: can be queried through package manager.
NOTIFICATION_CLASS See NOTIFICATION_PACKAGE.
TOTAL_BYTES Needs to be visible so that the app can display a progress bar. No obvious security concern. Intended to be visible by 3rd-party download UIs.
CURRENT_BYTES See TOTAL_BYTES.
TITLE Intended to be visible by 3rd-party download UIs. TODO Security and Privacy TBD before we implement support for 3rd-party UIs.
DESCRIPTION See TITLE.

Hiding the URI column

The URI column is visible to the initiating application, which is a mild security risk. It should be hidden, but the OTA update mechanism relies on it to check duplicate downloads and to display the download that's currently ongoing in the settings app. If another string column was exposed to the initiating applications, the OTA update mechanism could use that one, and URI could then be hidden. For Cupcake, without changing the database schema, the ENTITY column could be re-used as it's currently unused.

Handling redirects

There are two important aspects to handle redirects:

If the URI column gets hidden, it could be used to store the intermediate URIs. After 1.0 the only available integer columns were METHOD and CONTROL. CONTROL was re-exposed to applications and can't be used. METHOD is slated to be re-used for 503 retry-after delays. It could be split into two halves, one for retry-after and one for the redirect count. It would make more sense to count the redirect loop with FAILED_CONNECTIONS, but since there's already quite some code using it it'd take a bit more effort. Ideally handling of redirects would be delayed until a future release, with a cleanup of the database schema (going along with the cleanup of the handling of filenames).

Because of the pattern used to read/write DownloadInfo and DownloadProvider, it's impractical to store multiple small integers into a large one. Therefore, since there are no integer columns left in the database, redirects will have to wait beyond Cupcake.

ContentProvider for download UI

In order to allow a UI that can "see" all the relevant downloads, there'll need to be a separate URI (or set of URIs) in the content provider: trying to use the exact same URIs for regular download control and for UI purposes (distinguishing them based on the permissions of the caller) will break down if a same app (or actually a same UID) tries to do both. It'll also break down if the system process tries to do regular download activities, since it has all permissions.

Beyond that, there's little technical challenge: there are already mechanisms in place to restrict the list of columns that can be inserted, queried and updated (inserting of course makes no sense through the UI channel), they just need to be duplicated for the case of the UI. The download provider also knows how to check the permissions of its caller, there isn't anything new here.

Getting rid of OTHER_UID

Right now OTHER_UID is used by checkin/update to allow the settings app to display the name of an ongoing OTA update, and by Market to allow the system to install the new apks. It is however a dangerous feature, at least because it touches a part of the code that is critical to the download manager security (separation of applications).

Getting rid of OTHER_UID would be beneficial for the download manager, but the existing functionality has to be worked around. At this point, the idea that I consider the most likely would be to have checkin and market implement =ContentProvider= wrappers around their downloads, and expose those content providers to whichever app they want, with whichever security mechanism they wish to have.

Only using SDK APIs.

It'd be good if the download manager could be built against the SDK as much as possible.

Here's the list of APIs as of Nov 5 2008 that aren't in the current public API but that are used by the download manager:

com.google.android.collect.Lists
android.drm.mobile1.DrmRawContent
android.media.IMediaScannerService
android.net.http.AndroidHttpClient
android.os.FileUtils
android.provider.DrmStore

Schedule

Detailed Requirements

Req# History Status Feature Description Notes
1xxx N/A   API    
1001 1.0 YES   Download Manager API The download manager provides an API that allows applications to initiate downloads.  
1002 1.0 YES   Cookies The download manager API allows applications to pass cookies to the download manager.  
1003 1.0 YES   Security The download manager provides a mechanism that prevents arbitrary applications from accessing meta-data about current and past downloads. In 1.0, known holes between apps
1004 1.0 YES   Status to Initiator The download manager allows the application initiating a download to query the status of that download.  
1005 1.0 YES   Cancel by Initiator The download manager allows the application initiating a download to cancel that download.  
1006 1.0 YES   Notify Initiator The download manager notifies the application initiating a download when the download completes (with success or failure)  
1007 1.0 YES   Fire and Forget The download manager does not rely on the initiating application when saving the file to a shared filesystem area  
1008 1.0 YES   Short User-Readable Description The download manager allows the initiating application to optionally provide a user-readable short description of the download  
1009 1.0 YES   Long User-Readable Description The download manager allows the initiating application to optionally provide a user-readable full description of the download  
1010 Cupcake YES Cupcake P3 YES Restrict column access The download provider doesn't allow access to columns that aren't strictly necessary.  
1011 Cupcake YES Cupcake P2 YES Pause downloads The download provider allows the application initiating a download to pause that download.  
2xxx N/A   HTTP    
2001 1.0 YES   HTTP Support The download manager supports all the features of HTTP 1.1 (RFC 2616) that are relevant to file downloads. Codes 3xx weren't considered relevant when those requirements were written
2002 1.0 YES   Resume Downloads The download manager resumes downloads that get interrupted.  
2003 1.0 YES   Flaky Downloads The download manager has mechanism that prevent excessive retries of downloads that consistently fail or get interrupted.  
2004 1.0 YES   Resume after Reboot The download manager resumes downloads across device reboots.  
2005 Cupcake YES Cupcake P2 YES Resume after socket closed The download manager resumes incomplete downloads after the socket gets cleanly closed This is necessary in order to reliably download through GFEs, though it pushes us further away from being able to download from servers that don't implement pipelining.
2006 Cupcake YES Cupcake P2 YES Resume after 503 The download manager resumes or retries downloads when the server returns an HTTP code 503  
2007 Cupcake YES Cupcake P2 YES Random retry delay The download manager uses partial randomness when retrying downloads on an exponential backoff pattern.  
2008 Cupcake YES Cupcake P2 YES Retry-after delta-seconds The download manager uses the retry-after header in a 503 response to decide when to retry the request Handling of absolute dates will be covered by a separate requirement.
2009 Cupcake YES Cupcake P2 YES Redirects The download manager handles common redirects.  
25xx N/A   HTTP/Conditions    
3xxx N/A   Storage    
3001 1.0 YES   File Storage The download manager stores the results of downloads in persistent storage.  
3002 1.0 YES   Max File Size The download manager is able to handle large files (order of magnitude: 50MB)  
3003 1.0 YES   Destination File Permissions The download manager restricts access to the internal storage to applications with the appropriate permissions  
3004 1.0 YES   Initiator File Access The download manager allows the initiating application to access the destination file  
3005 1.0 YES   File Types The download manager does not save files that can't be displayed by any currently installed application  
3006 Cupcake NO Cupcake P3 NO Old Files in /cache The download manager deletes old files in /cache  
35xx N/A   Storage/Filename    
4xxx N/A   UI    
4001 1.0 NO   Download Manager UI The download manager provides a UI that lets user get information about current downloads and control them. Didn't get spec on time to be able to even consider it.
4002 Cupcake NO Cupcake P2 NO Single Notification Icon The download manager displays a single icon in the notification bar regardless of the number of ongoing and completed downloads. No spec in Cupcake timeframe.
5xxx N/A   MIME    
52xx N/A   MIME/DRM    
5201 1.0 YES   DRM The download manager respects the DRM information it receives with the responses  
54xx N/A   MIME/OMA    
60xx N/A   Misc    
65xx N/A   Misc/Browser    

System Architecture

+----------------------+    +--------------------------------------+    +-------------+
|                      |    |                                      |    |             |
|   Download Manager   |    |  Browser / Gmail / Market / Updater  |    |  Viewer App |
|                      |    |                                      |    |             |
+----------------------+    +--------------------------------------+    +-------------+
    ^    |    ^                                             ^                ^
    |    |    |                                             |                |
    |    |    |                                             |                |
    |    |    |      +---------------------------+          |                |
    |    |    |      |                           |          |                |
    |    |    |      |                           |          |                |
    |    |    +-------- - - - - - - - - - - - - ------------+                |
    |    |           |                           |                           |
    |    |           |                           |                           |
    |    +------------- - - - - - - - - - - - - -----------------------------+
    |                |                           |
    |                |                           |
    +--------------->|     Android framework     |
                     |                           |
                     |                           |
                     +---------------------------+

          Application                        Download Manager                         Viewer App
               |                                     |                                     |
               |         initiate download           |                                     |
               |------------------------------------>|                                     |
               |<------------------------------------|                                     |
               |        content provider URI         |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |           query download            |                                     |
               |------------------------------------>|                                     |
               |<------------------------------------|                                     |
               |              Cursor                 |                                     |
               |                                     |                                     |
               |       register ContentObserver      |                                     |
               |------------------------------------>|                                     |
               |                                     |                                     |
               |     ContentObserver notification    |                                     |
               |<------------------------------------|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |    intent "notification clicked"    |                                     |
               |<------------------------------------|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |      intent "download complete"     |                                     |
               |<------------------------------------|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |            intent "view"            |
               |                                     |------------------------------------>|
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |           update download           |                                     |
               |------------------------------------>|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |         open downloaded file        |                                     |
               |------------------------------------>|                                     |
               |<------------------------------------|                                     |
               |         ParcelFileDescriptor        |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |           delete download           |                                     |
               |------------------------------------>|                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               |                                     |                                     |
               v                                     v                                     v

Internal Product Dependencies

Interface Documentation

WARNING Since none of those APIs are public, they are all subject to change. If you're working in the Android source tree, do NOT use the explicit values, ONLY use the symbolic constants, unless you REALLY know what you're doing and are willing to deal with the consequences; you've been warned.

The various constants that are meant to be used by applications are all defined in the android.provider.Downloads class. Whenever possible, the constants should be used instead of the explicit values.

Permissions

Constant name Permission name Access restrictions Description
Downloads.PERMISSION_ACCESS "android.permission.ACCESS_DOWNLOAD_MANAGER" Signature or System Applications that want to access the Download Manager MUST have this permission.
Downloads.PERMISSION_ACCESS_ADVANCED "android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED" Signature or System This permission protects some legacy APIs that new applications SHOULD NOT use.
Downloads.PERMISSION_CACHE "android.permission.ACCESS_CACHE_FILESYSTEM" Signature This permission allows an app to access the /cache filesystem, and is only needed by the Update code. Other applications SHOULD NOT use this permission
Downloads.PERMISSION_SEND_INTENTS "android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" Signature The download manager holds this permission, and the receivers through which applications get intents of completed downloads SHOULD require this permission from the sender

Content Provider

The primary interface that applications use to communicate with the download manager is exposed as a ContentProvider.

URIs

The URIs for the Content Provider are:

Constant name URI Description
Downloads.CONTENT_URI Uri.parse("content://downloads/download") The URI of the whole Content Provider, used to insert new rows, or to query all rows.
N/A ContentUris.withAppendedId(CONTENT_URI, <id>) The URI of an individual download. <id> is the value of the Download._ID column

Columns

The following columns are available:

Constant name Column name SQL Type Access Description Notes
Downloads._ID "_id" Integer Read   Inherited from BaseColumns._ID.
Downloads.URI "uri" Text Init   Used to be readable.
Downloads.APP_DATA "entity" Text Init/Read/Modify   Actual column name will change in the future.
Downloads.NO_INTEGRITY "no_integrity" Boolean Init   Used to be readable.
Downloads.FILENAME_HINT "hint" Text Init   Used to be readable.
Downloads._DATA "_data" Text Read   Used to be Downloads.FILENAME and "filename".
Downloads.MIMETYPE "mimetype" Text Init/Read    
Downloads.DESTINATION "destination" Integer Init   See Destination codes for details of legal values. Used to be readable.
Downloads.VISIBILITY "visibility" Integer Init/Read/Modify   See Visibility codes for details of legal values.
Downloads.CONTROL "control" Integer Init/Read/Modify   See Control codes for details of legal values.
Downloads.STATUS "status" Integer Read   See Status codes for details of possible values.
Downloads.LAST_MODIFICATION "lastmod" Bigint Read    
Downloads.NOTIFICATION_PACKAGE "notificationpackage" Text Init/Read    
Downloads.NOTIFICATION_CLASS "notificationclass" Text Init/Read    
Downloads.NOTIFICATION_EXTRAS "notificationextras" Text Init    
Downloads.COOKIE_DATA "cookiedata" Text Init    
Downloads.USER_AGENT "useragent" Text Init    
Downloads.REFERER "referer" Text Init    
Downloads.TOTAL_BYTES "total_bytes" Integer Read   Might gain Init access in the future.
Downloads.CURRENT_BYTES "current_bytes" Integer Read    
Downloads.OTHER_UID "otheruid" Integer Init   Requires the android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED permission. Used to be readable and writable. Might disappear entirely if possible.
Downloads.TITLE "title" String Init/Read/Modify    
Downloads.DESCRIPTION "description" String Init/Read/Modify    

Destination Values

Constants name Constant value Description Notes
Downloads.DESTINATION_EXTERNAL 0 Saves the file to the SD card. Default value. Fails is SD card is not present.
Downloads.DESTINATION_CACHE_PARTITION 1 Saves the file to the internal cache partition. Requires the "android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED" permission
Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE 2 Saves the file to the internal cache partition. The download can get deleted at any time by the download manager when it needs space.

Visibility Values

Constants name Constant value Description Notes
Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED 0 The download is visible in download UIs, and it shows up in the notification area during download and after completion. Default value for external downloads.
Downloads.VISIBILITY_VISIBLE 1 The download is visible in download UIs, and it shows up in the notification area during download but not after completion.  
Downloads.VISIBILITY_HIDDEN 2 The download is hidden from download UIs and doesn't show up in the notification area. Default value for internal downloads.

Control Values

Constants name Constant value Description Notes
Downloads.CONTROL_RUN 0 The download is allowed to run. Default value.
Downloads.CONTROL_PAUSED 0 The download is paused.  

Status Values

Constants name Constant value Description Notes
Downloads.STATUS_PENDING 190 Download hasn't started.  
Downloads.STATUS_PENDING_PAUSED 191 Download hasn't started and can't start immediately (network unavailable, paused by user). Not currently used.
Downloads.STATUS_RUNNING 192 Download has started and is running  
Downloads.STATUS_RUNNING_PAUSED 193 Download has started, but can't run at the moment (network unavailable, paused by user).  
Downloads.STATUS_SUCCESS 200 Download completed successfully.  
Downloads.STATUS_BAD_REQUEST 400 Couldn't initiate the request, or server response 400.  
Downloads.STATUS_NOT_ACCEPTABLE 406 No handler to view the file (external downloads), or server response 406. External downloads are meant to be user-visible, and are aborted if there's no application to handle the relevant MIME type.
Downloads.STATUS_LENGTH_REQUIRED 411 The download manager can't know the length of the download. Because of the unreliability of cell networks, the download manager only performs downloads when it can verify that it has received all the data for a download, except if the initiating app sets the Downloads.NO_INTEGRITY flag.
Downloads.STATUS_PRECONDITION_FAILED 412 The download manager can't resume an interrupted download, because it didn't receive enough information from the server to be able to resume.  
Downloads.STATUS_CANCELED 490 The download was canceled by a cause outside the Download Manager. Formerly known as Downloads.STATUS_CANCELLED. Might be impossible to observe in 1.0.
Downloads.STATUS_UNKNOWN_ERROR 491 The download was aborted because of an unknown error. Formerly known as Downloads.STATUS_ERROR. Typically the result of a runtime exception that is not explicitly handled.
Downloads.STATUS_FILE_ERROR 492 The download was aborted because the data couldn't be saved. Most commonly happens when the filesystem is full.
Downloads.STATUS_UNHANDLED_REDIRECT 493 The download was aborted because the server returned a redirect code 3xx that the download manager doesn't handle. The download manager currently handles 301, 302 and 307.
Downloads.STATUS_TOO_MANY_REDIRECTS 494 The download was aborted because the download manager received too many redirects while trying to find the actual file.  
Downloads.STATUS_UNHANDLED_HTTP_CODE 495 The download was aborted because the server returned a status code that the download manager doesn't handle. Standard codes 3xx, 4xx and 5xx don't trigger this.
Downloads.STATUS_HTTP_DATA_ERROR 496 The download was aborted because of an unrecoverable error trying to get data over the network. Typically this happens when the download manager received several I/O Exceptions in a row while the network is available and without being able to download any data.
  4xx standard HTTP/1.1 4xx codes returned by the server are used as-is. Don't rely on non-standard values being passed through, especially the higher values that would collide with download manager codes.
  5xx standard HTTP/1.1 5xx codes returned by the server are used as-is. Don't rely on non-standard values being passed through.

Status Helper Functions

Function signature Description
public static boolean Downloads.isStatusInformational(int status) Returns whether the status code matches a download that hasn't completed yet.
public static boolean Downloads.isStatusSuspended(int status) Returns whether the download hasn't completed yet but isn't currently making progress.
public static boolean Downloads.isStatusSuccess(int status) Returns whether the download successfully completed.
public static boolean Downloads.isStatusError(int status) Returns whether the download failed.
public static boolean Downloads.isStatusClientError(int status) Returns whether the download failed because of a client error.
public static boolean Downloads.isStatusServerError(int status) Returns whether the download failed because of a server error.
public static boolean Downloads.isStatusCompleted(int status) Returns whether the download completed (with no distinction between success and failure).

Intents

The download manager sends an intent broadcast Downloads.DOWNLOAD_COMPLETED_ACTION when a download completes.

The download manager sends an intent broadcast Downloads.NOTIFICATION_CLICKED_ACTION when the user clicks a download notification that doesn't match a download that can be opened (e.g. because the notification is for several downloads at a time, or because it's for an incomplete download, or because it's for a private download).

The download manager starts an activity with Intent.ACTION_VIEW when the user clicks a download notification that matches a download that can be opened.

Differences between 1.0 and Cupcake

WARNING These are the differences for apps built from source in the Android source tree, i.e. they don't cover any of the cases of binary incompatibility.

Writing code that works on both the 1.0 and Cupcake versions of the download manager.

If you're not 100% sure that you need to be reading this chapter, don't read it.

Basic rule: don't use features that only exist in one of the two implementations (POST, downloads to /data...).

Also, don't use columns in 1.0 that are protected or hidden in Cupcake.

Unfortunately, that's not always entirely possible.

Areas of concern:

Functional Specification

All the details about what the product does.

TODO

Release notes for Cupcake

Product Architecture

How the tasks are split between the different modules that implement the product/feature.

TODO To be completed

Class
com.android.providers.downloads.Constants
com.android.providers.downloads.DownloadFileInfo
com.android.providers.downloads.DownloadInfo
com.android.providers.downloads.DownloadNotification
com.android.providers.downloads.DownloadNotification.NotificationItem
com.android.providers.downloads.DownloadProvider extends ContentProvider
com.android.providers.downloads.DownloadProvider.DatabaseHelper extends SQLiteOpenHelper
com.android.providers.downloads.DownloadProvider.ReadOnlyCursorWrapper extends CursorWrapper implements CrossProcessCursor
com.android.providers.downloads.DownloadReceiver extends BroadcastReceiver
com.android.providers.downloads.DownloadService extends Service
com.android.providers.downloads.DownloadService.DownloadManagerContentObserver extends ContentObserver
com.android.providers.downloads.DownloadService.MediaScannerConnection implements ServiceConnection
com.android.providers.downloads.DownloadService.UpdateThread extends Thread
com.android.providers.downloads.DownloadThread extends Thread
com.android.providers.downloads.Helpers
com.android.providers.downloads.Helpers.Lexer

The download manager is built primarily around a ContentProvider and a Service. The ContentProvider part is the front end, i.e. applications communicate with the download manager through the provider. The Service part is the back end, which contains the actual download logic, running as a background process.

As a first approach, the provider is essentially a canonical provider backed by a SQLite3 database. The biggest difference between the download provider and a "plain" provider is that the download provider aggressively validates its inputs, for security reasons.

The service is a background process that performs the actual downloads as requested by the applications. The service doesn't offer any bindable interface, the service object exists strictly so that the system knows how to prioritize the download manager's process against other processes when memory is tight.

Communication between the provider and the service is done through public Android APIs, so that the two components are deeply decoupled (they could in fact run in different processes). The download manager starts the service whenever a change is made that can start or restart a download. The service observes and queries the provider for changes, and updates the provider as the download progresses.

There are a few secondary classes that provide auxiliary functions.

A Receiver listens to several broadcasts. Is receives some system broadcasts when the system boots (so that the download manager can resume downloads that were interrupted when the system was turned off) or when the connectivity changes (so that the download manager can restart downloads that were interrupted when connectivity was lost). It also receives intents when the user selects a download notification.

Finally, some helper classes provide support functions.

Most significantly, DownloadThread is responsible for performing the actual downloads as part of the DownloadService's functionality, while UpdateThread is responsible for updating the DownloadInfo whenever the DownloadProvider data changes.

DownloadInfo and DownloadFileInfo hold pure data structures, with little or no actual logic.

Lexer takes care of validating the snippets of SQL data that are received from applications, to avoid cases of SQL injection.

The service keeps a copy of the provider data in RAM, so that it can determine what changed in the provider when it receives a change notification through the ContentObserver. That data is kept in an array of DownloadInfo structures.

Each DownloadThread performs the operations for a single download (or, more precisely, for a single HTTP transaction). Each DownloadThread is backed by a DownloadInfo object. which is in fact on of the objects kept by the DownloadService. While a download is running, the DownloadService can influence the download by writing data into the relevant DownloadInfo object, and the DownloadThread checks that object at appropriate times during the download.

Because the DownloadService updates the DownloadInfo objects asynchronously from everything else (it uses a dedicated thread for that purpose), a lot of care has to be taken when upgrading the DownloadInfo object. In fact, only the DownloadService's updateThread function can update that object, and it should be considered read-only to every other bit of code. Even within the updateThread function, some care must be taken to ensure that the DownloadInfos don't get out of sync with the provider.

On the other hand, the DownloadService's updateThread function does upgrade the DownloadInfo when it spawns new DownloadThreads (and in a few more circumstances), and when it does that it must also update the DownloadProvider (or risk seeing its DownloadInfo data get overwritten).

Because of all that, all code outside of the DowloadService's updateThread must neither read from DownloadProvider nor write to the DownloadInfo objects under any circumstances. The DownloadService's updateFunction is responsible for copying data from the DownloadProvider to the DownloadInfo objects, and must ensure that the DownloadProvider remains in sync with the information it writes into the DownloadInfo objects.

Implementation Documentation

How individual modules are implemented.

TODO To be completed

Database formats

Android 1.0, format version 31, table downloads:

Column Type
"_id" INTEGER PRIMARY KEY AUTOINCREMENT
"uri" TEXT
"method" INTEGER
"entity" TEXT
"no_integrity" BOOLEAN
"hint" TEXT
"otaupdate" BOOLEAN
"_data" TEXT
"mimetype" TEXT
"destination" INTEGER
"no_system" BOOLEAN
"visibility" INTEGER
"control" INTEGER
"status" INTEGER
"numfailed" INTEGER
"lastmod" BIGINT
"notificationpackage" TEXT
"notificationclass" TEXT
"notificationextras" TEXT
"cookiedata" TEXT
"useragent" TEXT
"referer" TEXT
"total_bytes" INTEGER
"current_bytes" INTEGER
"etag" TEXT
"uid" INTEGER
"otheruid" INTEGER
"title" TEXT
"description" TEXT
"scanned" BOOLEAN

Cupcake, format version 100: Same as format version 31.

Future Directions

WARNING This section is for informative purposes only.

API

HTTP Handling

File names

UI

Handling of specific MIME types

Management of downloads based on environment

Management of simultaneous downloads

Minor functional changes, edge cases

Architecture and Implementation

Code style, refactoring

Browser changes