1<html> 2 3<head> 4 <title>Download Provider</title> 5</head> 6<body> 7<a name="PageTop"></a> 8<div> 9 <h1>Download Provider</h1> 10<p /> 11<h2><a name="_Contents"> </a> Contents </h2> 12<p /> 13<p /> 14<ul> 15<li> <a href="#High_level_requirements">High-level requirements</a> 16<ul> 17<li> <a href="#Requirements_for_Android">Requirements for Android</a> 18</li> 19<li> <a href="#Requirements_for_Cupcake">Requirements for Cupcake</a> 20</li> 21<li> <a href="#Requirements_for_Donut">Requirements for Donut</a> 22</li> 23</ul> 24</li> 25<li> <a href="#Technology_Evaluation">Technology Evaluation</a> 26<ul> 27<li> <a href="#Possible_scope_for_Cupcake">Possible scope for Cupcake</a> 28</li> 29<li> <a href="#Reducing_the_number_of_classes">Reducing the number of classes</a> 30</li> 31<li> <a href="#Reducing_the_list_of_visible_col">Reducing the list of visible columns</a> 32</li> 33<li> <a href="#Hiding_the_URI_column">Hiding the URI column</a> 34</li> 35<li> <a href="#Handling_redirects">Handling redirects</a> 36</li> 37<li> <a href="#ContentProvider_for_download_UI">ContentProvider for download UI</a> 38</li> 39<li> <a href="#Getting_rid_of_OTHER_UID">Getting rid of OTHER_UID</a> 40</li> 41<li> <a href="#Only_using_SDK_APIs_">Only using SDK APIs.</a> 42</li> 43</ul> 44</li> 45<li> <a href="#Schedule">Schedule</a> 46</li> 47<li> <a href="#Detailed_Requirements">Detailed Requirements</a> 48</li> 49<li> <a href="#System_Architecture">System Architecture</a> 50<ul> 51<li> <a href="#Internal_Product_Dependencies">Internal Product Dependencies</a> 52</li> 53</ul> 54</li> 55<li> <a href="#Interface_Documentation">Interface Documentation</a> 56<ul> 57<li> <a href="#Permissions">Permissions</a> 58</li> 59<li> <a href="#Content_Provider">Content Provider</a> 60<ul> 61<li> <a href="#URIs">URIs</a> 62</li> 63<li> <a href="#Columns">Columns</a> 64</li> 65<li> <a href="#Destination_Values">Destination Values</a> 66</li> 67<li> <a href="#Visibility_Values">Visibility Values</a> 68</li> 69<li> <a href="#Control_Values">Control Values</a> 70</li> 71<li> <a href="#Status_Values">Status Values</a> 72</li> 73<li> <a href="#Status_Helper_Functions">Status Helper Functions</a> 74</li> 75</ul> 76</li> 77<li> <a href="#Intents">Intents</a> 78</li> 79<li> <a href="#Differences_between_1_0_and_Cupc">Differences between 1.0 and Cupcake</a> 80<ul> 81<li> <a href="#Writing_code_that_works_on_both_">Writing code that works on both the 1.0 and Cupcake versions of the download manager.</a> 82</li> 83</ul> 84</li> 85</ul> 86</li> 87<li> <a href="#Functional_Specification">Functional Specification</a> 88<ul> 89<li> <a href="#Release_notes_for_Cupcake">Release notes for Cupcake</a> 90</li> 91</ul> 92</li> 93<li> <a href="#Product_Architecture">Product Architecture</a> 94</li> 95<li> <a href="#Implementation_Documentation">Implementation Documentation</a> 96<ul> 97<li> <a href="#Database_formats">Database formats</a> 98</li> 99</ul> 100</li> 101<li> <a href="#Future_Directions">Future Directions</a> 102<ul> 103<li> <a href="#API">API</a> 104</li> 105<li> <a href="#HTTP_Handling">HTTP Handling</a> 106</li> 107<li> <a href="#File_names">File names</a> 108</li> 109<li> <a href="#UI">UI</a> 110</li> 111<li> <a href="#Handling_of_specific_MIME_types">Handling of specific MIME types</a> 112</li> 113<li> <a href="#Management_of_downloads_based_on">Management of downloads based on environment</a> 114</li> 115<li> <a href="#Management_of_simultaneous_downl">Management of simultaneous downloads</a> 116</li> 117<li> <a href="#Minor_functional_changes_edge_ca">Minor functional changes, edge cases</a> 118</li> 119<li> <a href="#Architecture_and_Implementation">Architecture and Implementation</a> 120</li> 121<li> <a href="#Code_style_refactoring">Code style, refactoring</a> 122</li> 123<li> <a href="#Browser_changes">Browser changes</a> 124</li> 125</ul> 126</li> 127</ul> 128<p /> 129<hr /> 130<p /> 131<h2><a name="High_level_requirements"> </a> High-level requirements </h2> 132<!--_>small<High-level description of the major requirements, features and goals of the product/feature.>/small<_--> 133<p /> 134<h3><a name="Requirements_for_Android"> </a> Requirements for Android </h3> 135<p /> 136<table border="1" cellspacing="1" cellpadding="2"> 137<tr><th > Req# </th><th > Description </th><th > Detailed Requirements </th><th > Status </th></tr> 138<tr><td > DLMGR_A_1 </td><td > The Download Manager MUST satisfy the basic download needs of OTA Updates. </td><td > </td><td > <b>YES</b> done in 1.0 </td></tr> 139<tr><td > DLMGR_A_2 </td><td > The Download Manager MUST satisfy the basic download needs of Market. </td><td > </td><td > <b>YES</b> done in 1.0 </td></tr> 140<tr><td > DLMGR_A_3 </td><td > The Download Manager MUST satisfy the basic download needs of Gmail. </td><td > </td><td > <b>YES</b> done in 1.0 </td></tr> 141<tr><td > DLMGR_A_4 </td><td > The Download Manager MUST satisfy the basic download needs of the Browser. </td><td > </td><td > <b>YES</b> done in 1.0 </td></tr> 142<tr><td > DLMGR_A_5 </td><td > Downloads MUST happen and continue in the background, independently of the application in front. </td><td > </td><td > <b>YES</b> done in 1.0 </td></tr> 143<tr><td > DLMGR_A_6 </td><td > Downloads MUST be reliable even on unreliable network connections, within the capabilities of the underlying protocol and of the server. </td><td > </td><td > <b>YES</b> done in 1.0 </td></tr> 144<tr><td > DLMGR_A_7 </td><td > 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. </td><td > </td><td > <b>YES</b> done in 1.0 </td></tr> 145<tr><td > DLMGR_A_8 </td><td > OMA downloads SHOULD be supported. </td><td > </td><td > <b>NO</b> deferred beyond Cupcake (not enough time) </td></tr> 146<tr><td > DLMGR_A_9 </td><td > The Download Manager SHOULD only download when using high-speed (3G/wifi) links, or only when not roaming (if that can be detected). </td><td > </td><td > <b>NO</b> deferred beyond Cupcake (not enough time) </td></tr> 147<tr><td > DLMGR_A_10 </td><td > Downloads SHOULD be user-controllable (if allowed by the initiating application), including pause, resume, and restart of failed downloads. </td><td > 4001 </td><td > <b>NO</b> deferred beyond Cupcake (not enough time, need precise specifications) </td></tr> 148</table> 149<p /> 150<h3><a name="Requirements_for_Cupcake"> </a> Requirements for Cupcake </h3> 151<p /> 152<table border="1" cellspacing="1" cellpadding="2"> 153<tr><th > Req# </th><th > Description </th><th > Detailed Requirements </th><th > Status </th></tr> 154<tr><td > DLMGR_C_1 </td><td > The Download Manager SHOULD resume downloads where the socket got cleanly closed while the download was incomplete (common behavior on proxies and Google servers) </td><td > 2005 </td><td > <b>YES</b> done in Cupcake </td></tr> 155<tr><td > DLMGR_C_2 </td><td > The Download Manager SHOULD retry/resume downloads instead of aborting when the server returns a HTTP code 503 </td><td > 2006 </td><td > <b>YES</b> done in Cupcake </td></tr> 156<tr><td > DLMGR_C_3 </td><td > The Download Manager SHOULD randomize the retry delay </td><td > 2007 </td><td > <b>YES</b> done in Cupcake </td></tr> 157<tr><td > DLMGR_C_4 </td><td > The Download Manager SHOULD use the retry-after header (delta-seconds) for 503 responses </td><td > 2008 </td><td > <b>YES</b> done in Cupcake </td></tr> 158<tr><td > DLMGR_C_5 </td><td > The Download Manager MAY hide columns that aren't strictly necessary </td><td > 1010 </td><td > <b>YES</b> done in Cupcake </td></tr> 159<tr><td > DLMGR_C_6 </td><td > The Download Manager SHOULD allow the initiating app to pause an ongoing download </td><td > 1011 </td><td > <b>YES</b> done in Cupcake </td></tr> 160<tr><td > DLMGR_C_7 </td><td > The Download Manager SHOULD not display multiple notification icons for completed downloads. </td><td > 4002 </td><td > <b>NO</b> deferred beyond Cupcake (no precise specifications, need framework support) </td></tr> 161<tr><td > DLMGR_C_8 </td><td > The Download Manager SHOULD delete old files from /cache </td><td > 3006 </td><td > <b>NO</b> deferred beyond Cupcake (no precise specifications) </td></tr> 162<tr><td > DLMGR_C_9 </td><td > The Download Manager SHOULD handle redirects </td><td > 2009 </td><td > <b>YES</b> done in Cupcake </td></tr> 163</table> 164<p /> 165<h3><a name="Requirements_for_Donut"> </a> Requirements for Donut </h3> 166<p /> 167<table border="1" cellspacing="1" cellpadding="2"> 168<tr><th > Req# </th><th > Description </th><th > Detailed Requirements </th><th > Status </th></tr> 169</table> 170<p /> 171<h2><a name="Technology_Evaluation"> </a> Technology Evaluation </h2> 172<!--_>small<Initial Engineering evaluation based on the Statement of Requirements.>/small<_--> 173<p /> 174<b>ALSO</b> See also <a href="#Future_Directions">future directions</a> for possible additional technical changes. 175<p /> 176<h3><a name="Possible_scope_for_Cupcake"> </a> Possible scope for Cupcake </h3> 177<p /> 178Because there is no testing environment in place in the 1.0 code, and because the schedule between 1791.0 and Cupcake doesn't leave enough time to develop a testing environment solid enough to be able 180to test the database upgrades from 1.0 database scheme to a new scheme, any work done in Cupcake will 181have to work within the existing 1.0 database scheme. 182<p /> 183<h3><a name="Reducing_the_number_of_classes"> </a> Reducing the number of classes </h3> 184<p /> 185Each class in the system has a measurable RAM cost (because of the associated Class objects), 186and therefore reducing the number of classes when possible or relevant can reduce the memory 187requirements. That being said, classes that extend system classes and are necessary for the 188operation of the download manager can't be removed. 189<p /> 190<table border="1" cellspacing="1" cellpadding="2"> 191<tr><th > Class </th><th > Comments </th></tr> 192<tr><td > <code>com.android.providers.downloads.Constants</code> </td><td > Only contains constants, can be merged into another class. </td></tr> 193<tr><td > <code>com.android.providers.downloads.DownloadFileInfo</code> </td><td > Should be merged with <code>DownloadInfo</code>. </td></tr> 194<tr><td > <code>com.android.providers.downloads.DownloadInfo</code> </td><td > Once we only store information in RAM about downloads that are explicitly active, can be merged with <code>DownloadThread</code>. </td></tr> 195<tr><td > <code>com.android.providers.downloads.DownloadNotification</code> </td><td > Looks like it could be merged with <code>DownloadProvider</code> or <code>DownloadService</code>. </td></tr> 196<tr><td > <code>com.android.providers.downloads.DownloadNotification.NotificationItem</code> </td><td > Can probably be eliminated by using queries intelligently. </td></tr> 197<tr><td > <code>com.android.providers.downloads.DownloadProvider</code> </td><td > Extends <code>ContentProvider</code>, can't be eliminated. </td></tr> 198<tr><td > <code>com.android.providers.downloads.DownloadProvider.DatabaseHelper</code> </td><td > Can probably be eliminated by re-implementing by hand the logic of <code>SQLiteOpenHelper</code>. </td></tr> 199<tr><td > <code>com.android.providers.downloads.DownloadProvider.ReadOnlyCursorWrapper</code> </td><td > Can be eliminated once <code>Cursor</code> is read-only system-wide. </td></tr> 200<tr><td > <code>com.android.providers.downloads.DownloadReceiver</code> </td><td > Extends <code>BroadcastReceiver</code>, can't be eliminated. </td></tr> 201<tr><td > <code>com.android.providers.downloads.DownloadService</code> </td><td > Extends <code>Service</code>, unlikely that this can be eliminated. TBD. </td></tr> 202<tr><td > <code>com.android.providers.downloads.DownloadService.DownloadManagerContentObserver</code> </td><td > Extends <code>ContentObserver</code>, can be eliminated if the download manager can be re-architected to not depend on <code>ContentObserver</code> any more. </td></tr> 203<tr><td > <code>com.android.providers.downloads.DownloadService.MediaScannerConnection</code> </td><td > Can probably be merged into another class. </td></tr> 204<tr><td > <code>com.android.providers.downloads.DownloadService.UpdateThread</code> </td><td > Can probably be made to implement <code>Runnable</code> instead and merged into another class, can be eliminated if the download manager can be re-architected to not depend on <code>ContentObserver</code> any more. </td></tr> 205<tr><td > <code>com.android.providers.downloads.DownloadThread</code> </td><td > Can probably be made to implement <code>Runnable</code> 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). </td></tr> 206<tr><td > <code>com.android.providers.downloads.Helpers</code> </td><td > Can't be instantiated, can be merged into another class. </td></tr> 207<tr><td > <code>com.android.providers.downloads.Helpers.Lexer</code> </td><td > Keeps state about an ongoing lex, can probably be merged into another class by making the lexer <code>synchronized</code>, since the operation is short-lived. </td></tr> 208</table> 209<p /> 210<h3><a name="Reducing_the_list_of_visible_col"> </a> Reducing the list of visible columns </h3> 211<p /> 212Security in the download provider is primarily enforced with two separate mechanisms: 213<p /> 214<ul> 215<li> Column restrictions, such that only a small number of the download provider's columns can be read or queried by applications. 216</li> 217<li> UID restrictions, such that only the application that initiated a download can access information about that download. 218</li> 219</ul> 220<p /> 221The first mechanism is expected to be fairly robust (the implementation is quite simple, based on projection maps, which are highly 222structured), but the second one relies on arbitrary strings (URIs and SQL fragments) passed by applications and is therefore at a 223higher risk of being compromised. Therefore, sensitive information stored in unrestricted columns (for which the first mechanism 224doesn't apply) is at a greater risk than other information. 225<p /> 226Here's the list of columns that can currently be read/queried, with comments: 227<p /> 228<table border="1" cellspacing="1" cellpadding="2"> 229<tr><th > Column </th><th > Notes </th></tr> 230<tr><td > <code>_ID</code> </td><td > 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. </td></tr> 231<tr><td > <code>_DATA</code> </td><td > Probably should not be visible to applications. <b>WARNING</b> 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. </td></tr> 232<tr><td > <code>MIMETYPE</code> </td><td > Needs to be visible so that app can display the icon matching the mime type. Intended to be visible by 3rd-party download UIs. <b>TODO</b> Security TBD before we implement support for 3rd-party UIs. </td></tr> 233<tr><td > <code>VISIBILITY</code> </td><td > Needs to be visible in case an app has both visible and invisible downloads. No obvious security concern. </td></tr> 234<tr><td > <code>DESTINATION</code> </td><td > 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. </td></tr> 235<tr><td > <code>STATUS</code> </td><td > Needs to be visible (1004). No obvious security concern. </td></tr> 236<tr><td > <code>LAST_MODIFICATION</code> </td><td > 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. </td></tr> 237<tr><td > <code>NOTIFICATION_PACKAGE</code> </td><td > Allows individual apps running under shared UIDs to identify their own downloads. No security concern: can be queried through package manager. </td></tr> 238<tr><td > <code>NOTIFICATION_CLASS</code> </td><td > See <code>NOTIFICATION_PACKAGE</code>. </td></tr> 239<tr><td > <code>TOTAL_BYTES</code> </td><td > 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. </td></tr> 240<tr><td > <code>CURRENT_BYTES</code> </td><td > See <code>TOTAL_BYTES</code>. </td></tr> 241<tr><td > <code>TITLE</code> </td><td > Intended to be visible by 3rd-party download UIs. <b>TODO</b> Security and Privacy TBD before we implement support for 3rd-party UIs. </td></tr> 242<tr><td > <code>DESCRIPTION</code> </td><td > See <code>TITLE</code>. </td></tr> 243</table> 244<p /> 245<h3><a name="Hiding_the_URI_column"> </a> Hiding the URI column </h3> 246<p /> 247The <code>URI</code> 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 <code>URI</code> could then be hidden. For Cupcake, without changing the database schema, the <code>ENTITY</code> column could be re-used as it's currently unused. 248<p /> 249<h3><a name="Handling_redirects"> </a> Handling redirects </h3> 250<p /> 251There are two important aspects to handle redirects: 252<p /> 253<ul> 254<li> Storing the intermediate URIs in the provider. 255</li> 256<li> Protecting against redirect loops. 257</li> 258</ul> 259<p /> 260If the <code>URI</code> column gets hidden, it could be used to store the intermediate URIs. After 1.0 the only available integer columns were <code>METHOD</code> and <code>CONTROL</code>. <code>CONTROL</code> was re-exposed to applications and can't be used. <code>METHOD</code> 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 <code>FAILED_CONNECTIONS</code>, 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). 261<p /> 262Because of the pattern used to read/write <code>DownloadInfo</code> and <code>DownloadProvider</code>, 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. 263<p /> 264<h3><a name="ContentProvider_for_download_UI"> </a> ContentProvider for download UI </h3> 265<p /> 266In 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. 267<p /> 268Beyond 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. 269<p /> 270<h3><a name="Getting_rid_of_OTHER_UID"> </a> Getting rid of OTHER_UID </h3> 271<p /> 272Right now <code>OTHER_UID</code> 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). 273<p /> 274Getting rid of <code>OTHER_UID</code> 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. 275<p /> 276<h3><a name="Only_using_SDK_APIs_"> </a> Only using SDK APIs. </h3> 277<p /> 278It'd be good if the download manager could be built against the SDK as much as possible. 279<p /> 280Here'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: 281<p /> 282<pre> 283com.google.android.collect.Lists 284android.drm.mobile1.DrmRawContent 285android.media.IMediaScannerService 286android.net.http.AndroidHttpClient 287android.os.FileUtils 288android.provider.DrmStore 289</pre> 290<p /> 291<!-- ---++ Sales Validation--> 292<!--_>small<Whether there's a market for the product/feature.>/small<_--> 293<p /> 294<!--The 1.0 "must" features are all mandatory in all versions of Android.--> 295<p /> 296<!-- ---++ Business Case--> 297<!--_>small<Whether the high-level evaluations (technical and sales) done so far justify further investment.>/small<_--> 298<p /> 299<!--There are enough common requirements between the various applications that need to perform reliable background downloads that implementing a centralized download manager is more cost-effective than having each application implement their own subset of the overall feature set.--> 300<p /> 301<h2><a name="Schedule"> </a> Schedule </h2> 302<p /> 303<!--_>small<The relevant milestones for this project.>/small<_--> 304<p /> 305<ul> 306<li> No future milestones currently defined 307</li> 308</ul> 309<p /> 310<h2><a name="Detailed_Requirements"> </a> Detailed Requirements </h2> 311<!--_>small<Detailed lists of all requirements.>/small<_--> 312<p /> 313<!--$S$ _Do not change requirement numbers, do not delete requirements (mark them obsolete if necessary), so that the numbers remain unique over time_--> 314<p /> 315<table border="1" cellspacing="1" cellpadding="2"> 316<tr><th > Req# </th><th > History </th><th > Status </th><th > Feature </th><th > Description </th><th > Notes </th></tr> 317<tr><td > <em>1xxx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>API</em> </td><td > </td><td > </td></tr> 318<tr><td > 1001 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Download Manager API </td><td > The download manager provides an API that allows applications to initiate downloads. </td><td > </td></tr> 319<tr><td > 1002 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Cookies </td><td > The download manager API allows applications to pass cookies to the download manager. </td><td > </td></tr> 320<tr><td > 1003 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Security </td><td > The download manager provides a mechanism that prevents arbitrary applications from accessing meta-data about current and past downloads. </td><td > In 1.0, known holes between apps </td></tr> 321<tr><td > 1004 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Status to Initiator </td><td > The download manager allows the application initiating a download to query the status of that download. </td><td > </td></tr> 322<tr><td > 1005 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Cancel by Initiator </td><td > The download manager allows the application initiating a download to cancel that download. </td><td > </td></tr> 323<tr><td > 1006 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Notify Initiator </td><td > The download manager notifies the application initiating a download when the download completes (with success or failure) </td><td > </td></tr> 324<tr><td > 1007 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Fire and Forget </td><td > The download manager does not rely on the initiating application when saving the file to a shared filesystem area </td><td > </td></tr> 325<tr><td > 1008 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Short User-Readable Description </td><td > The download manager allows the initiating application to optionally provide a user-readable short description of the download </td><td > </td></tr> 326<tr><td > 1009 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Long User-Readable Description </td><td > The download manager allows the initiating application to optionally provide a user-readable full description of the download </td><td > </td></tr> 327<tr><td > 1010 </td><td > Cupcake <b>YES</b> </td><td > Cupcake P3 <b>YES</b> </td><td > Restrict column access </td><td > The download provider doesn't allow access to columns that aren't strictly necessary. </td><td > </td></tr> 328<tr><td > 1011 </td><td > Cupcake <b>YES</b> </td><td > Cupcake P2 <b>YES</b> </td><td > Pause downloads </td><td > The download provider allows the application initiating a download to pause that download. </td><td > </td></tr> 329<tr><td > <em>2xxx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>HTTP</em> </td><td > </td><td > </td></tr> 330<tr><td > 2001 </td><td > 1.0 <b>YES</b> </td><td > </td><td > HTTP Support </td><td > The download manager supports all the features of HTTP 1.1 (RFC 2616) that are relevant to file downloads. </td><td > Codes 3xx weren't considered relevant when those requirements were written </td></tr> 331<tr><td > 2002 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Resume Downloads </td><td > The download manager resumes downloads that get interrupted. </td><td > </td></tr> 332<tr><td > 2003 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Flaky Downloads </td><td > The download manager has mechanism that prevent excessive retries of downloads that consistently fail or get interrupted. </td><td > </td></tr> 333<tr><td > 2004 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Resume after Reboot </td><td > The download manager resumes downloads across device reboots. </td><td > </td></tr> 334<tr><td > 2005 </td><td > Cupcake <b>YES</b> </td><td > Cupcake P2 <b>YES</b> </td><td > Resume after socket closed </td><td > The download manager resumes incomplete downloads after the socket gets cleanly closed </td><td > 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. </td></tr> 335<tr><td > 2006 </td><td > Cupcake <b>YES</b> </td><td > Cupcake P2 <b>YES</b> </td><td > Resume after 503 </td><td > The download manager resumes or retries downloads when the server returns an HTTP code 503 </td><td > </td></tr> 336<tr><td > 2007 </td><td > Cupcake <b>YES</b> </td><td > Cupcake P2 <b>YES</b> </td><td > Random retry delay </td><td > The download manager uses partial randomness when retrying downloads on an exponential backoff pattern. </td><td > </td></tr> 337<tr><td > 2008 </td><td > Cupcake <b>YES</b> </td><td > Cupcake P2 <b>YES</b> </td><td > Retry-after delta-seconds </td><td > The download manager uses the retry-after header in a 503 response to decide when to retry the request </td><td > Handling of absolute dates will be covered by a separate requirement. </td></tr> 338<tr><td > 2009 </td><td > Cupcake <b>YES</b> </td><td > Cupcake P2 <b>YES</b> </td><td > Redirects </td><td > The download manager handles common redirects. </td><td > </td></tr> 339<tr><td > <em>25xx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>HTTP/Conditions</em> </td><td > </td><td > </td></tr> 340<tr><td > <em>3xxx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>Storage</em> </td><td > </td><td > </td></tr> 341<tr><td > 3001 </td><td > 1.0 <b>YES</b> </td><td > </td><td > File Storage </td><td > The download manager stores the results of downloads in persistent storage. </td><td > </td></tr> 342<tr><td > 3002 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Max File Size </td><td > The download manager is able to handle large files (order of magnitude: 50MB) </td><td > </td></tr> 343<tr><td > 3003 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Destination File Permissions </td><td > The download manager restricts access to the internal storage to applications with the appropriate permissions </td><td > </td></tr> 344<tr><td > 3004 </td><td > 1.0 <b>YES</b> </td><td > </td><td > Initiator File Access </td><td > The download manager allows the initiating application to access the destination file </td><td > </td></tr> 345<tr><td > 3005 </td><td > 1.0 <b>YES</b> </td><td > </td><td > File Types </td><td > The download manager does not save files that can't be displayed by any currently installed application </td><td > </td></tr> 346<tr><td > 3006 </td><td > Cupcake <b>NO</b> </td><td > Cupcake P3 <b>NO</b> </td><td > Old Files in /cache </td><td > The download manager deletes old files in /cache </td><td > </td></tr> 347<tr><td > <em>35xx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>Storage/Filename</em> </td><td > </td><td > </td></tr> 348<tr><td > <em>4xxx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>UI</em> </td><td > </td><td > </td></tr> 349<tr><td > 4001 </td><td > 1.0 <b>NO</b> </td><td > </td><td > Download Manager UI </td><td > The download manager provides a UI that lets user get information about current downloads and control them. </td><td > Didn't get spec on time to be able to even consider it. </td></tr> 350<tr><td > 4002 </td><td > Cupcake <b>NO</b> </td><td > Cupcake P2 <b>NO</b> </td><td > Single Notification Icon </td><td > The download manager displays a single icon in the notification bar regardless of the number of ongoing and completed downloads. </td><td > No spec in Cupcake timeframe. </td></tr> 351<tr><td > <em>5xxx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>MIME</em> </td><td > </td><td > </td></tr> 352<tr><td > <em>52xx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>MIME/DRM</em> </td><td > </td><td > </td></tr> 353<tr><td > 5201 </td><td > 1.0 <b>YES</b> </td><td > </td><td > DRM </td><td > The download manager respects the DRM information it receives with the responses </td><td > </td></tr> 354<tr><td > <em>54xx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>MIME/OMA</em> </td><td > </td><td > </td></tr> 355<tr><td > <em>60xx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>Misc</em> </td><td > </td><td > </td></tr> 356<tr><td > <em>65xx</em> </td><td > <em>N/A</em> </td><td > </td><td > <em>Misc/Browser</em> </td><td > </td><td > </td></tr> 357</table> 358<p /> 359<h2><a name="System_Architecture"> </a> System Architecture </h2> 360<!--_>small<How the product/feature fits with outside entities in the big picture.>/small<_--> 361<p /> 362<pre> 363+----------------------+ +--------------------------------------+ +-------------+ 364| | | | | | 365| Download Manager | | Browser / Gmail / Market / Updater | | Viewer App | 366| | | | | | 367+----------------------+ +--------------------------------------+ +-------------+ 368 ^ | ^ ^ ^ 369 | | | | | 370 | | | | | 371 | | | +---------------------------+ | | 372 | | | | | | | 373 | | | | | | | 374 | | +-------- - - - - - - - - - - - - ------------+ | 375 | | | | | 376 | | | | | 377 | +------------- - - - - - - - - - - - - -----------------------------+ 378 | | | 379 | | | 380 +--------------->| Android framework | 381 | | 382 | | 383 +---------------------------+ 384</pre> 385<p /> 386<pre> 387 Application Download Manager Viewer App 388 | | | 389 | initiate download | | 390 |------------------------------------>| | 391 |<------------------------------------| | 392 | content provider URI | | 393 | | | 394 | | | 395 | | | 396 | | | 397 | query download | | 398 |------------------------------------>| | 399 |<------------------------------------| | 400 | Cursor | | 401 | | | 402 | register ContentObserver | | 403 |------------------------------------>| | 404 | | | 405 | ContentObserver notification | | 406 |<------------------------------------| | 407 | | | 408 | | | 409 | | | 410 | | | 411 | intent "notification clicked" | | 412 |<------------------------------------| | 413 | | | 414 | | | 415 | | | 416 | | | 417 | intent "download complete" | | 418 |<------------------------------------| | 419 | | | 420 | | | 421 | | | 422 | | | 423 | | intent "view" | 424 | |------------------------------------>| 425 | | | 426 | | | 427 | | | 428 | | | 429 | update download | | 430 |------------------------------------>| | 431 | | | 432 | | | 433 | | | 434 | | | 435 | open downloaded file | | 436 |------------------------------------>| | 437 |<------------------------------------| | 438 | ParcelFileDescriptor | | 439 | | | 440 | | | 441 | | | 442 | | | 443 | delete download | | 444 |------------------------------------>| | 445 | | | 446 | | | 447 | | | 448 | | | 449 v v v 450</pre> 451<p /> 452<h3><a name="Internal_Product_Dependencies"> </a> Internal Product Dependencies </h3> 453<p /> 454<ul> 455<li> <em>[reverse dependency]</em> GMail depends on the download manager to download attachments. 456</li> 457<li> <em>[reverse dependency]</em> OTA Update depends on the download manager to download system images. 458</li> 459<li> <em>[reverse dependency]</em> Vending Machine depends on the download manager to download content. 460</li> 461<li> <em>[reverse dependency]</em> Browser depends on the download manager to download non-browsable. 462</li> 463<li> Download Manager depends on a notification system to let the user know when downloads complete or otherwise require attention. 464</li> 465<li> Download Manager depends on a mechanism to share files with another app (letting apps access its files, or accessing apps' files). 466</li> 467<li> Download Manager depends on the ability to associate individual downloads with separate applications and to restrict apps to only access their own downloads. 468</li> 469<li> Download Manager depends on an HTTP stack that predictably processes all relevant kinds of HTTP requests and responses. 470</li> 471<li> Download Manager depends on a connectivity manager that reports accurate information about the status of the different data connections. 472</li> 473</ul> 474<p /> 475<h2><a name="Interface_Documentation"> </a> Interface Documentation </h2> 476<!--_>small<Details of all the interfaces exposed by all the modules that implement the product/feature.>/small<_--> 477<p /> 478<b>WARNING</b> Since none of those APIs are public, they are all subject to change. If you're working in the Android source tree, do <em>NOT</em> use the explicit values, <em>ONLY</em> use the symbolic constants, unless you <em>REALLY</em> know what you're doing and are willing to deal with the consequences; you've been warned. 479<p /> 480The various constants that are meant to be used by applications are all defined in the <code>android.provider.Downloads</code> class. Whenever possible, the constants should be used instead of the explicit values. 481<p /> 482<h3><a name="Permissions"> </a> Permissions </h3> 483<p /> 484<table border="1" cellspacing="1" cellpadding="2"> 485<tr><th > Constant name </th><th > Permission name </th><th > Access restrictions </th><th > Description </th></tr> 486<tr><td > <code>Downloads.PERMISSION_ACCESS</code> </td><td > <code>"android.permission.ACCESS_DOWNLOAD_MANAGER"</code> </td><td > Signature or System </td><td > Applications that want to access the Download Manager MUST have this permission. </td></tr> 487<tr><td > <code>Downloads.PERMISSION_ACCESS_ADVANCED</code> </td><td > <code>"android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED"</code> </td><td > Signature or System </td><td > This permission protects some legacy APIs that new applications SHOULD NOT use. </td></tr> 488<tr><td > <code>Downloads.PERMISSION_CACHE</code> </td><td > <code>"android.permission.ACCESS_CACHE_FILESYSTEM"</code> </td><td > Signature </td><td > 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 </td></tr> 489<tr><td > <code>Downloads.PERMISSION_SEND_INTENTS</code> </td><td > <code>"android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS"</code> </td><td > Signature </td><td > The download manager holds this permission, and the receivers through which applications get intents of completed downloads SHOULD require this permission from the sender </td></tr> 490</table> 491<p /> 492<h3><a name="Content_Provider"> </a> Content Provider </h3> 493<p /> 494The primary interface that applications use to communicate with the download manager is exposed as a ContentProvider. 495<p /> 496<h4><a name="URIs"> </a> URIs </h4> 497<p /> 498The URIs for the Content Provider are: 499<p /> 500<table border="1" cellspacing="1" cellpadding="2"> 501<tr><th > Constant name </th><th > URI </th><th > Description </th></tr> 502<tr><td > <code>Downloads.CONTENT_URI</code> </td><td > <code>Uri.parse("content://downloads/download")</code> </td><td > The URI of the whole Content Provider, used to insert new rows, or to query all rows. </td></tr> 503<tr><td > N/A </td><td > <code>ContentUris.withAppendedId(CONTENT_URI, <id>)</code> </td><td > The URI of an individual download. <code><id></code> is the value of the <code>Download._ID</code> column </td></tr> 504</table> 505<p /> 506<h4><a name="Columns"> </a> Columns </h4> 507<p /> 508The following columns are available: 509<p /> 510<table border="1" cellspacing="1" cellpadding="2"> 511<tr><th > Constant name </th><th > Column name </th><th > SQL Type </th><th > Access </th><th > Description </th><th > Notes </th></tr> 512<tr><td > <code>Downloads._ID</code> </td><td > <code>"_id"</code> </td><td > Integer </td><td > Read </td><td > </td><td > Inherited from <code>BaseColumns._ID</code>. </td></tr> 513<tr><td > <code>Downloads.URI</code> </td><td > <code>"uri"</code> </td><td > Text </td><td > Init </td><td > </td><td > Used to be readable. </td></tr> 514<tr><td > <code>Downloads.APP_DATA</code> </td><td > <code>"entity"</code> </td><td > Text </td><td > Init/Read/Modify </td><td > </td><td > Actual column name will change in the future. </td></tr> 515<tr><td > <code>Downloads.NO_INTEGRITY</code> </td><td > <code>"no_integrity"</code> </td><td > Boolean </td><td > Init </td><td > </td><td > Used to be readable. </td></tr> 516<tr><td > <code>Downloads.FILENAME_HINT</code> </td><td > <code>"hint"</code> </td><td > Text </td><td > Init </td><td > </td><td > Used to be readable. </td></tr> 517<tr><td > <code>Downloads._DATA</code> </td><td > <code>"_data"</code> </td><td > Text </td><td > Read </td><td > </td><td > Used to be <code>Downloads.FILENAME</code> and <code>"filename"</code>. </td></tr> 518<tr><td > <code>Downloads.MIMETYPE</code> </td><td > <code>"mimetype"</code> </td><td > Text </td><td > Init/Read </td><td > </td><td > </td></tr> 519<tr><td > <code>Downloads.DESTINATION</code> </td><td > <code>"destination"</code> </td><td > Integer </td><td > Init </td><td > </td><td > See <a href="#DestinationValues">Destination codes</a> for details of legal values. Used to be readable. </td></tr> 520<tr><td > <code>Downloads.VISIBILITY</code> </td><td > <code>"visibility"</code> </td><td > Integer </td><td > Init/Read/Modify </td><td > </td><td > See <a href="#VisibilityValues">Visibility codes</a> for details of legal values. </td></tr> 521<tr><td > <code>Downloads.CONTROL</code> </td><td > <code>"control"</code> </td><td > Integer </td><td > Init/Read/Modify </td><td > </td><td > See <a href="#ControlValues">Control codes</a> for details of legal values. </td></tr> 522<tr><td > <code>Downloads.STATUS</code> </td><td > <code>"status"</code> </td><td > Integer </td><td > Read </td><td > </td><td > See <a href="#StatusValues">Status codes</a> for details of possible values. </td></tr> 523<tr><td > <code>Downloads.LAST_MODIFICATION</code> </td><td > <code>"lastmod"</code> </td><td > Bigint </td><td > Read </td><td > </td><td > </td></tr> 524<tr><td > <code>Downloads.NOTIFICATION_PACKAGE</code> </td><td > <code>"notificationpackage"</code> </td><td > Text </td><td > Init/Read </td><td > </td><td > </td></tr> 525<tr><td > <code>Downloads.NOTIFICATION_CLASS</code> </td><td > <code>"notificationclass"</code> </td><td > Text </td><td > Init/Read </td><td > </td><td > </td></tr> 526<tr><td > <code>Downloads.NOTIFICATION_EXTRAS</code> </td><td > <code>"notificationextras"</code> </td><td > Text </td><td > Init </td><td > </td><td > </td></tr> 527<tr><td > <code>Downloads.COOKIE_DATA</code> </td><td > <code>"cookiedata"</code> </td><td > Text </td><td > Init </td><td > </td><td > </td></tr> 528<tr><td > <code>Downloads.USER_AGENT</code> </td><td > <code>"useragent"</code> </td><td > Text </td><td > Init </td><td > </td><td > </td></tr> 529<tr><td > <code>Downloads.REFERER</code> </td><td > <code>"referer"</code> </td><td > Text </td><td > Init </td><td > </td><td > </td></tr> 530<tr><td > <code>Downloads.TOTAL_BYTES</code> </td><td > <code>"total_bytes"</code> </td><td > Integer </td><td > Read </td><td > </td><td > Might gain Init access in the future. </td></tr> 531<tr><td > <code>Downloads.CURRENT_BYTES</code> </td><td > <code>"current_bytes"</code> </td><td > Integer </td><td > Read </td><td > </td><td > </td></tr> 532<tr><td > <code>Downloads.OTHER_UID</code> </td><td > <code>"otheruid"</code> </td><td > Integer </td><td > Init </td><td > </td><td > Requires the <code>android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED</code> permission. Used to be readable and writable. Might disappear entirely if possible. </td></tr> 533<tr><td > <code>Downloads.TITLE</code> </td><td > <code>"title"</code> </td><td > String </td><td > Init/Read/Modify </td><td > </td><td > </td></tr> 534<tr><td > <code>Downloads.DESCRIPTION</code> </td><td > <code>"description"</code> </td><td > String </td><td > Init/Read/Modify </td><td > </td><td > </td></tr> 535</table> 536<p /> 537<h4><a name="Destination_Values"> </a> Destination Values </h4> 538<p /> 539<table border="1" cellspacing="1" cellpadding="2"> 540<tr><th > Constants name </th><th > Constant value </th><th > Description </th><th > Notes </th></tr> 541<tr><td > <code>Downloads.DESTINATION_EXTERNAL</code> </td><td > <code>0</code> </td><td > Saves the file to the SD card. </td><td > Default value. Fails is SD card is not present. </td></tr> 542<tr><td > <code>Downloads.DESTINATION_CACHE_PARTITION</code> </td><td > <code>1</code> </td><td > Saves the file to the internal cache partition. </td><td > Requires the <code>"android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED"</code> permission </td></tr> 543<tr><td > <code>Downloads.DESTINATION_CACHE_PARTITION_PURGEABLE</code> </td><td > <code>2</code> </td><td > Saves the file to the internal cache partition. </td><td > The download can get deleted at any time by the download manager when it needs space. </td></tr> 544</table> 545<p /> 546<h4><a name="Visibility_Values"> </a> Visibility Values </h4> 547<p /> 548<table border="1" cellspacing="1" cellpadding="2"> 549<tr><th > Constants name </th><th > Constant value </th><th > Description </th><th > Notes </th></tr> 550<tr><td > <code>Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED</code> </td><td > <code>0</code> </td><td > The download is visible in download UIs, and it shows up in the notification area during download and after completion. </td><td > Default value for external downloads. </td></tr> 551<tr><td > <code>Downloads.VISIBILITY_VISIBLE</code> </td><td > <code>1</code> </td><td > The download is visible in download UIs, and it shows up in the notification area during download but not after completion. </td><td > </td></tr> 552<tr><td > <code>Downloads.VISIBILITY_HIDDEN</code> </td><td > <code>2</code> </td><td > The download is hidden from download UIs and doesn't show up in the notification area. </td><td > Default value for internal downloads. </td></tr> 553</table> 554<p /> 555<h4><a name="Control_Values"> </a> Control Values </h4> 556<p /> 557<table border="1" cellspacing="1" cellpadding="2"> 558<tr><th > Constants name </th><th > Constant value </th><th > Description </th><th > Notes </th></tr> 559<tr><td > <code>Downloads.CONTROL_RUN</code> </td><td > <code>0</code> </td><td > The download is allowed to run. </td><td > Default value. </td></tr> 560<tr><td > <code>Downloads.CONTROL_PAUSED</code> </td><td > <code>0</code> </td><td > The download is paused. </td><td > </td></tr> 561</table> 562<p /> 563<h4><a name="Status_Values"> </a> Status Values </h4> 564<p /> 565<table border="1" cellspacing="1" cellpadding="2"> 566<tr><th > Constants name </th><th > Constant value </th><th > Description </th><th > Notes </th></tr> 567<tr><td > <code>Downloads.STATUS_PENDING</code> </td><td > <code>190</code> </td><td > Download hasn't started. </td><td > </td></tr> 568<tr><td > <code>Downloads.STATUS_PENDING_PAUSED</code> </td><td > <code>191</code> </td><td > Download hasn't started and can't start immediately (network unavailable, paused by user). </td><td > Not currently used. </td></tr> 569<tr><td > <code>Downloads.STATUS_RUNNING</code> </td><td > <code>192</code> </td><td > Download has started and is running </td><td > </td></tr> 570<tr><td > <code>Downloads.STATUS_RUNNING_PAUSED</code> </td><td > <code>193</code> </td><td > Download has started, but can't run at the moment (network unavailable, paused by user). </td><td > </td></tr> 571<tr><td > <code>Downloads.STATUS_SUCCESS</code> </td><td > <code>200</code> </td><td > Download completed successfully. </td><td > </td></tr> 572<tr><td > <code>Downloads.STATUS_BAD_REQUEST</code> </td><td > <code>400</code> </td><td > Couldn't initiate the request, or server response 400. </td><td > </td></tr> 573<tr><td > <code>Downloads.STATUS_NOT_ACCEPTABLE</code> </td><td > <code>406</code> </td><td > No handler to view the file (external downloads), or server response 406. </td><td > External downloads are meant to be user-visible, and are aborted if there's no application to handle the relevant MIME type. </td></tr> 574<tr><td > <code>Downloads.STATUS_LENGTH_REQUIRED</code> </td><td > <code>411</code> </td><td > The download manager can't know the length of the download. </td><td > 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 <code>Downloads.NO_INTEGRITY</code> flag. </td></tr> 575<tr><td > <code>Downloads.STATUS_PRECONDITION_FAILED</code> </td><td > <code>412</code> </td><td > The download manager can't resume an interrupted download, because it didn't receive enough information from the server to be able to resume. </td><td > </td></tr> 576<tr><td > <code>Downloads.STATUS_CANCELED</code> </td><td > <code>490</code> </td><td > The download was canceled by a cause outside the Download Manager. </td><td > Formerly known as <code>Downloads.STATUS_CANCELLED</code>. Might be impossible to observe in 1.0. </td></tr> 577<tr><td > <code>Downloads.STATUS_UNKNOWN_ERROR</code> </td><td > <code>491</code> </td><td > The download was aborted because of an unknown error. </td><td > Formerly known as <code>Downloads.STATUS_ERROR</code>. Typically the result of a runtime exception that is not explicitly handled. </td></tr> 578<tr><td > <code>Downloads.STATUS_FILE_ERROR</code> </td><td > <code>492</code> </td><td > The download was aborted because the data couldn't be saved. </td><td > Most commonly happens when the filesystem is full. </td></tr> 579<tr><td > <code>Downloads.STATUS_UNHANDLED_REDIRECT</code> </td><td > <code>493</code> </td><td > The download was aborted because the server returned a redirect code 3xx that the download manager doesn't handle. </td><td > The download manager currently handles 301, 302 and 307. </td></tr> 580<tr><td > <code>Downloads.STATUS_TOO_MANY_REDIRECTS</code> </td><td > <code>494</code> </td><td > The download was aborted because the download manager received too many redirects while trying to find the actual file. </td><td > </td></tr> 581<tr><td > <code>Downloads.STATUS_UNHANDLED_HTTP_CODE</code> </td><td > <code>495</code> </td><td > The download was aborted because the server returned a status code that the download manager doesn't handle. </td><td > Standard codes 3xx, 4xx and 5xx don't trigger this. </td></tr> 582<tr><td > <code>Downloads.STATUS_HTTP_DATA_ERROR</code> </td><td > <code>496</code> </td><td > The download was aborted because of an unrecoverable error trying to get data over the network. </td><td > 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. </td></tr> 583<tr><td > </td><td > <code>4xx</code> </td><td > standard HTTP/1.1 4xx codes returned by the server are used as-is. </td><td > Don't rely on non-standard values being passed through, especially the higher values that would collide with download manager codes. </td></tr> 584<tr><td > </td><td > <code>5xx</code> </td><td > standard HTTP/1.1 5xx codes returned by the server are used as-is. </td><td > Don't rely on non-standard values being passed through. </td></tr> 585</table> 586<p /> 587<h4><a name="Status_Helper_Functions"> </a> Status Helper Functions </h4> 588<p /> 589<table border="1" cellspacing="1" cellpadding="2"> 590<tr><th > Function signature </th><th > Description </th></tr> 591<tr><td > <code>public static boolean Downloads.isStatusInformational(int status)</code> </td><td > Returns whether the status code matches a download that hasn't completed yet. </td></tr> 592<tr><td > <code>public static boolean Downloads.isStatusSuspended(int status)</code> </td><td > Returns whether the download hasn't completed yet but isn't currently making progress. </td></tr> 593<tr><td > <code>public static boolean Downloads.isStatusSuccess(int status)</code> </td><td > Returns whether the download successfully completed. </td></tr> 594<tr><td > <code>public static boolean Downloads.isStatusError(int status)</code> </td><td > Returns whether the download failed. </td></tr> 595<tr><td > <code>public static boolean Downloads.isStatusClientError(int status)</code> </td><td > Returns whether the download failed because of a client error. </td></tr> 596<tr><td > <code>public static boolean Downloads.isStatusServerError(int status)</code> </td><td > Returns whether the download failed because of a server error. </td></tr> 597<tr><td > <code>public static boolean Downloads.isStatusCompleted(int status)</code> </td><td > Returns whether the download completed (with no distinction between success and failure). </td></tr> 598</table> 599<p /> 600<h3><a name="Intents"> </a> Intents </h3> 601<p /> 602The download manager sends an intent broadcast <code>Downloads.DOWNLOAD_COMPLETED_ACTION</code> when a download completes. 603<p /> 604The download manager sends an intent broadcast <code>Downloads.NOTIFICATION_CLICKED_ACTION</code> 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). 605<p /> 606The download manager starts an activity with <code>Intent.ACTION_VIEW</code> when the user clicks a download notification that matches a download that can be opened. 607<p /> 608<h3><a name="Differences_between_1_0_and_Cupc"> </a> Differences between 1.0 and Cupcake </h3> 609<p /> 610<b>WARNING</b> 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. 611<p /> 612<ul> 613<li> Cursors returned by the content provider are now read-only. 614</li> 615<li> SQL "where" statements are now verified and must follow a rigid syntax (the most visible aspect being that all parameters much be single-quoted). 616</li> 617<li> Columns and constants that were unused or ineffective are gone: METHOD, NO_SYSTEM_FILES, DESTINATION_DATA_CACHE, OTA_UPDATE. 618</li> 619<li> OTHER_UID and DESTINATION_CACHE_PARTITION require android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED. 620</li> 621<li> The list of columns that can be queried and read is limited: <code>_ID</code>, <code>APP_DATA</code>, <code>_DATA</code>, <code>MIMETYPE</code>, <code>CONTROL</code>, <code>STATUS</code>, <code>LAST_MODIFICATION</code>, <code>NOTIFICATION_PACKAGE</code>, <code>NOTIFICATION_CLASS</code>, <code>TOTAL_BYTES</code>, <code>CURRENT_BYTES</code>, <code>TITLE</code>, <code>DESCRIPTION</code>. 622</li> 623<li> The list of columns that can be initialized by apps is limited: <code>URI</code>, <code>APP_DATA</code>, <code>NO_INTEGRITY</code>, <code>FILENAME_HINT</code>, <code>MIMETYPE</code>, <code>DESTINATION</code>, <code>VISIBILITY</code>, <code>CONTROL</code>, <code>STATUS</code>, <code>LAST_MODIFICATION</code>, <code>NOTIFICATION_PACKAGE</code>, <code>NOTIFICATION_CLASS</code>, <code>NOTIFICATION_EXTRAS</code>, <code>COOKIE_DATA</code>, <code>USER_AGENT</code>, <code>REFERER</code>, <code>OTHER_UID</code>, <code>TITLE</code>, <code>DESCRIPTION</code>. 624</li> 625<li> The list of columns that can be updated by apps is limited: <code>APP_DATA</code>, <code>VISIBILITY</code>, <code>CONTROL</code>, <code>TITLE</code>, <code>DESCRIPTION</code>. 626</li> 627<li> Downloads to the SD card default to have notifications that are visible after completion, internal downloads default to notifications that are always hidden. 628</li> 629<li> The constant FILENAME was renamed Downloads._DATA. 630</li> 631<li> Downloads can be paused and resumed by writing to the CONTROL column. <code>Downloads.CONTROL_RUN</code> (Default) makes the download go, <code>Downloads.CONTROL_PAUSED</code> pauses it. 632</li> 633<li> New column APP_DATA that is untouched by the download manager, used to store app-specific info about the downloads. 634</li> 635<li> Minor differences unlikely to affect applications: 636<ul> 637<li> The notification class/package must now match the UID. 638</li> 639<li> The backdoor to see the entire provider (which was intended to implement UIs) is gone. 640</li> 641<li> Private column names were removed from the public API. 642</li> 643</ul> 644</li> 645</ul> 646<p /> 647<h4><a name="Writing_code_that_works_on_both_"> </a> Writing code that works on both the 1.0 and Cupcake versions of the download manager. </h4> 648<p /> 649If you're not 100% sure that you need to be reading this chapter, don't read it. 650<p /> 651Basic rule: don't use features that only exist in one of the two implementations (POST, downloads to /data...). 652<p /> 653Also, don't use columns in 1.0 that are protected or hidden in Cupcake. 654<p /> 655Unfortunately, that's not always entirely possible. 656<p /> 657Areas of concern: 658<ul> 659<li> Some columns were renamed. FILENAME became Downloads._DATA ("_data"), and ENTITY became APP_DATA ("entity"). 660<ul> 661<li> The difference can be used to distinguish between 1.0 and Cupcake, though reflection. 662</li> 663<li> The difference prevents from using any of the symbolic constants directly in source code: if the same binary wants to run on both 1.0 and Cupcake, it will have to hard-code the values of those constants or use reflection to get to them. 664</li> 665</ul> 666</li> 667<li> URI column accessible in 1.0 but protected in Cupcake. Code that relies on being able to re-read its URI should be using APP_DATA in Cupcake, but that column doesn't exist as such in 1.0. 668<ul> 669<li> If the code detects that it's running on Cupcake, write URI to APP_DATA in addition to URI, and query and read from the appropriate column. 670</li> 671<li> Since the underlying column for APP_DATA exists in both 1.0 and Cupcake even though it has different names, it can actually be used in both cases (see note above about renamed columns). 672</li> 673</ul> 674</li> 675<li> Some of the error codes have been renumbered. STATUS_UNHANDLED_HTTP_CODE and STATUS_HTTP_DATA_ERROR were bumped up from 494 and 495 to 495 and 496. 676</li> 677<li> Backward compatibility is not guaranteed: the download manager APIs weren't meant to be backward compatible yet. As such it's impossible to guarantee that code that uses the Cupcake download manager will be binary-compatible with future versions. 678<ul> 679<li> I intend to eventually change the column name for APP_DATA to "app_data". Because of that, code should use reflection to get to that name instead of hard-coding "entity", so that it always gets the right value for the string. 680</li> 681<li> I intend to refine the handling of filenames and content URIs, exposing separate columns for situations where a download can be accessed both as a file and through a content URI (e.g. stuff that is recognized by the media scanner). Unfortunately at this point this feature isn't clear in my mind. I'd recommend using reflection to look for the Downloads._DATA column, and if it isn't there to look for the FILENAME column (which has the advantage of also dealing with the difference between 1.0 and Cupcake). 682</li> 683<li> I intend to renumber the error codes, especially those in the 4xx range, and especially those below 490 (which overlap with standard HTTP error codes but will probably be separated). Reflection would improve the probability to getting to them in the future. Unfortunately, the names of the constants are likely to change in the process, in order to disambiguate codes coming from HTTP from those generated locally. I might try to stick to the following pattern: where a constant is currently named STATUS_XXX, its locally-generated version in the future might be named STATUS_LOCAL_XXX while the current constant name might disappear. Using reflection to try to get to the possible new name instead of using the old name might improve the probability of compatibility in the future. That being said, it is critically important to properly handle the full ranges or error codes, especially the 4xx range, as "expected" errors, and it is far preferable to not try to distinguish between those codes at all: use the functions Downloads.isError and Downloads.isClientError to easily recognize those entire ranges. In order of probability, the 1xx range is the second most likely to be affected. 684</li> 685</ul> 686</li> 687</ul> 688<p /> 689<h2><a name="Functional_Specification"> </a> Functional Specification </h2> 690<em><small>All the details about what the product does.</small></em> 691<p /> 692<b>TODO</b> 693<p /> 694<h3><a name="Release_notes_for_Cupcake"> </a> Release notes for Cupcake </h3> 695<p /> 696<ul> 697<li> HTTP codes 301, 302, 303 and 307 (redirects) are now supported 698</li> 699<li> HTTP code 503 is now handled, with support for retry-after in delay-seconds 700</li> 701<li> Downloads that were cleanly interrupted are now resumed instead of failing 702</li> 703<li> Applications can now pause their downloads 704</li> 705<li> Retry delays are now randomized 706</li> 707<li> Connectivity is now checked on all interfaces 708</li> 709<li> Downloads with invalid characters in file name can now be saved 710</li> 711<li> Various security fixes 712</li> 713<li> Minor API changes (see <a href="#Differences_between_1_0_and_Cupc">API differences between 1.0 and Cupcake</a>) 714</li> 715</ul> 716<p /> 717<h2><a name="Product_Architecture"> </a> Product Architecture </h2> 718<em><small>How the tasks are split between the different modules that implement the product/feature.</small></em> 719<p /> 720<b>TODO</b> To be completed 721<p /> 722<table border="1" cellspacing="1" cellpadding="2"> 723<tr><th > Class </th></tr> 724<tr><td > <code>com.android.providers.downloads.Constants</code> </td></tr> 725<tr><td > <code>com.android.providers.downloads.DownloadFileInfo</code> </td></tr> 726<tr><td > <code>com.android.providers.downloads.DownloadInfo</code> </td></tr> 727<tr><td > <code>com.android.providers.downloads.DownloadNotification</code> </td></tr> 728<tr><td > <code>com.android.providers.downloads.DownloadNotification.NotificationItem</code> </td></tr> 729<tr><td > <code>com.android.providers.downloads.DownloadProvider extends ContentProvider</code> </td></tr> 730<tr><td > <code>com.android.providers.downloads.DownloadProvider.DatabaseHelper extends SQLiteOpenHelper</code> </td></tr> 731<tr><td > <code>com.android.providers.downloads.DownloadProvider.ReadOnlyCursorWrapper extends CursorWrapper implements CrossProcessCursor</code> </td></tr> 732<tr><td > <code>com.android.providers.downloads.DownloadReceiver extends BroadcastReceiver</code> </td></tr> 733<tr><td > <code>com.android.providers.downloads.DownloadService extends Service</code> </td></tr> 734<tr><td > <code>com.android.providers.downloads.DownloadService.DownloadManagerContentObserver extends ContentObserver</code> </td></tr> 735<tr><td > <code>com.android.providers.downloads.DownloadService.MediaScannerConnection implements ServiceConnection</code> </td></tr> 736<tr><td > <code>com.android.providers.downloads.DownloadService.UpdateThread extends Thread</code> </td></tr> 737<tr><td > <code>com.android.providers.downloads.DownloadThread extends Thread</code> </td></tr> 738<tr><td > <code>com.android.providers.downloads.Helpers</code> </td></tr> 739<tr><td > <code>com.android.providers.downloads.Helpers.Lexer</code> </td></tr> 740</table> 741<p /> 742The 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. 743<p /> 744As 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. 745<p /> 746The 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. 747<p /> 748Communication 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. 749<p /> 750<p /> 751There are a few secondary classes that provide auxiliary functions. 752<p /> 753A 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. 754<p /> 755<p /> 756Finally, some helper classes provide support functions. 757<p /> 758Most 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. 759<p /> 760DownloadInfo and DownloadFileInfo hold pure data structures, with little or no actual logic. 761<p /> 762Lexer takes care of validating the snippets of SQL data that are received from applications, to avoid cases of SQL injection. 763<p /> 764<p /> 765<p /> 766<p /> 767<p /> 768<p /> 769The 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. 770<p /> 771Each 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. 772<p /> 773Because 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. 774<p /> 775On 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). 776<p /> 777Because 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. 778<p /> 779<p /> 780<p /> 781<h2><a name="Implementation_Documentation"> </a> Implementation Documentation </h2> 782<em><small>How individual modules are implemented.</small></em> 783<p /> 784<b>TODO</b> To be completed 785<p /> 786<h3><a name="Database_formats"> </a> Database formats </h3> 787<p /> 788Android 1.0, format version <code>31</code>, table <code>downloads</code>: 789<p /> 790<table border="1" cellspacing="1" cellpadding="2"> 791<tr><th > Column </th><th > Type </th></tr> 792<tr><td > <code>"_id"</code> </td><td > <code>INTEGER PRIMARY KEY AUTOINCREMENT</code> </td></tr> 793<tr><td > <code>"uri"</code> </td><td > <code>TEXT</code> </td></tr> 794<tr><td > <code>"method"</code> </td><td > <code>INTEGER</code> </td></tr> 795<tr><td > <code>"entity"</code> </td><td > <code>TEXT</code> </td></tr> 796<tr><td > <code>"no_integrity"</code> </td><td > <code>BOOLEAN</code> </td></tr> 797<tr><td > <code>"hint"</code> </td><td > <code>TEXT</code> </td></tr> 798<tr><td > <code>"otaupdate"</code> </td><td > <code>BOOLEAN</code> </td></tr> 799<tr><td > <code>"_data"</code> </td><td > <code>TEXT</code> </td></tr> 800<tr><td > <code>"mimetype"</code> </td><td > <code>TEXT</code> </td></tr> 801<tr><td > <code>"destination"</code> </td><td > <code>INTEGER</code> </td></tr> 802<tr><td > <code>"no_system"</code> </td><td > <code>BOOLEAN</code> </td></tr> 803<tr><td > <code>"visibility"</code> </td><td > <code>INTEGER</code> </td></tr> 804<tr><td > <code>"control"</code> </td><td > <code>INTEGER</code> </td></tr> 805<tr><td > <code>"status"</code> </td><td > <code>INTEGER</code> </td></tr> 806<tr><td > <code>"numfailed"</code> </td><td > <code>INTEGER</code> </td></tr> 807<tr><td > <code>"lastmod"</code> </td><td > <code>BIGINT</code> </td></tr> 808<tr><td > <code>"notificationpackage"</code> </td><td > <code>TEXT</code> </td></tr> 809<tr><td > <code>"notificationclass"</code> </td><td > <code>TEXT</code> </td></tr> 810<tr><td > <code>"notificationextras"</code> </td><td > <code>TEXT</code> </td></tr> 811<tr><td > <code>"cookiedata"</code> </td><td > <code>TEXT</code> </td></tr> 812<tr><td > <code>"useragent"</code> </td><td > <code>TEXT</code> </td></tr> 813<tr><td > <code>"referer"</code> </td><td > <code>TEXT</code> </td></tr> 814<tr><td > <code>"total_bytes"</code> </td><td > <code>INTEGER</code> </td></tr> 815<tr><td > <code>"current_bytes"</code> </td><td > <code>INTEGER</code> </td></tr> 816<tr><td > <code>"etag"</code> </td><td > <code>TEXT</code> </td></tr> 817<tr><td > <code>"uid"</code> </td><td > <code>INTEGER</code> </td></tr> 818<tr><td > <code>"otheruid"</code> </td><td > <code>INTEGER</code> </td></tr> 819<tr><td > <code>"title"</code> </td><td > <code>TEXT</code> </td></tr> 820<tr><td > <code>"description"</code> </td><td > <code>TEXT</code> </td></tr> 821<tr><td > <code>"scanned"</code> </td><td > <code>BOOLEAN</code> </td></tr> 822</table> 823<p /> 824Cupcake, format version <code>100</code>: Same as format version <code>31</code>. 825<p /> 826<h2><a name="Future_Directions"> </a> Future Directions </h2> 827<!--_>small<What could be done in the future to improve the product.>/small<_--> 828<p /> 829<b>WARNING</b> This section is for informative purposes only. 830<p /> 831<h3><a name="API"> </a> API </h3> 832<p /> 833<ul> 834<li> Expose Download Manager to 3rd party apps - security, robustness . 835</li> 836<li> Validate application-provided user agent, cookies, etc... to protect against e.g. header injection. 837</li> 838<li> Allow trust-and-verify MIME type. 839</li> 840<li> Extract response string from HTTP responses, extract entity from failed responses 841</li> 842<li> If app fails to be notified, retry later - don't give up because of a single failure. 843</li> 844<li> Support data: URIs . 845</li> 846<li> Download files to app-provided content provider . 847</li> 848<li> Don't pass HTTP codes above about 490 to the initiating app (figure out what the threshold should be). 849</li> 850<li> Allow initiating app to specify that it wants wifi-only downloads . 851</li> 852<li> Provide SQL "where" clauses for the different categories of status codes, matching isStatusXXX(). 853</li> 854<li> There has to be a mechanism by which old downloads are automatically purged from /cache if there's not enough space . 855</li> 856<li> Clicking the notification for a completed download should go through the initiating app instead of directly opening the file (but what about fire and forget?). 857</li> 858<li> Clean up the difference between pending_network (waiting for network) and running_paused (user paused the download). 859</li> 860<li> Allow any app to access the downloaded file (but no other column) of user-downloaded files . 861</li> 862<li> Provider should return more errors and throw fewer exceptions if possible. 863</li> 864<li> Add option to auto-dismiss notifications for completed downloads after a given time . 865</li> 866<li> Delete filename in case of failure - better handle the separation between filename and content URI. 867</li> 868<li> Allow the initiating application to specify that it wants to restart downloads from scratch if they're interrupted and can't be resumed . 869</li> 870<li> Save images directly from browser without re-downloading data . 871</li> 872<li> Give applications the ability to explicitly specify the full target filename (not just a hint). 873</li> 874<li> Give applications the ability to download multiple files into multiple set locations as if they were a single "package". 875</li> 876<li> Give applications the ability to download files only if they haven't been already downloaded. 877</li> 878<li> Give applications the ability to download files that have already been downloaded only if there's a newer version. 879</li> 880<li> Set-cookie in the response. 881</li> 882<li> basic auth . 883</li> 884<li> app-provided prompts for basic auth, ssl, redirects. 885</li> 886<li> File should be hidden from initiating application when DRM. 887</li> 888<li> Delay writing of app-visible filename column until file visible (split user-visible vs private files?). 889</li> 890<li> Separate locally-generated status codes from standard HTTP codes. 891</li> 892<li> Allow app to specify it doesn't want to resume downloads across reboots (because they might require additional work). 893</li> 894<li> Allow app to prioritize user-initiated downloads. 895</li> 896<li> Allow app to specify length of download (full trust, or trust-and-verify). 897</li> 898<li> Support POST. 899</li> 900<li> Support PUT. 901</li> 902<li> Support plugins for additional protocols and download descriptors. 903</li> 904<li> Rename columns to have an appropriate COLUMN_ prefix. 905</li> 906</ul> 907<p /> 908<h3><a name="HTTP_Handling"> </a> HTTP Handling </h3> 909<p /> 910<ul> 911<li> Fail download immediately on authoritative unresolved hostnames . 912</li> 913<li> The download manager should use the browser's user-agent by default. 914</li> 915<li> Redirect with HTTP Refresh headers (download current and target content with non-zero refresh). 916</li> 917<li> Handle content-encoding header. 918</li> 919<li> Handle transfer-encoding header. 920</li> 921<li> Find other ways to validate interrupted downloads (signature, last-mod/if-modified-since) . 922</li> 923<li> Make downloads time out in case of long time with no activity. 924</li> 925</ul> 926<p /> 927<h3><a name="File_names"> </a> File names </h3> 928<p /> 929<ul> 930<li> Protect against situations where there's already a "downloads" directory on SD card. 931</li> 932<li> Deal with filenames with invalid characters. 933</li> 934<li> Refine the logic that builds filenames to better match desktop browsers - drop the query string. 935</li> 936<li> URI-decode filenames generated from URIs. 937</li> 938<li> Better deal with filenames that end in '.'. 939</li> 940<li> Deal with URIs that end in '/' or '?'. 941</li> 942<li> Investigate how to better deal with filenames that have multiple extensions. 943</li> 944</ul> 945<p /> 946<h3><a name="UI"> </a> UI </h3> 947<p /> 948<ul> 949<li> Prompt for redirects across domains or cancel. 950</li> 951<li> Prompt for redirects from SSL or cancel. 952</li> 953<li> Prompt for basic auth or cancel. 954</li> 955<li> Prompt for SSL with untrusted/invalid/expired certificates or cancel. 956</li> 957<li> Reduce number of icons in the title bar, possibly as low as 1 (animated if there are ongoing downloads, fixed if all downloads have completed) . 958</li> 959<li> UI to cancel visible downloads. 960</li> 961<li> UI to pause visible downloads. 962</li> 963<li> Reorder downloads. 964</li> 965<li> View SSL certificates. 966</li> 967<li> Indicate secure downloads. 968</li> 969</ul> 970<p /> 971<h3><a name="Handling_of_specific_MIME_types"> </a> Handling of specific MIME types </h3> 972<p /> 973<ul> 974<li> Parse HTML for redirects with meta tag. 975</li> 976<li> Handle charsets and transcoding of text files. 977</li> 978<li> Deal with multiparts. 979</li> 980<li> Support OMA downloads with DD and data in same multipart, i.e. combined delivery. 981</li> 982<li> Assume application/octet-stream for http responses with no mime type. 983</li> 984<li> Download anything if an app supports application/octet-stream. 985</li> 986<li> Download any text/* if an application supports text/plain. 987</li> 988<li> Should the media scanner be invoked on DRM downloads? 989</li> 990<li> Refresh header with timer should be followed if content is not downloadable. 991</li> 992<li> Support OMA downloads. 993</li> 994<li> Support MIDP-OTA downloads. 995</li> 996<li> Support Sprint MCD downloads. 997</li> 998<li> Sniff content when receiving MIME-types known to be inaccurately sent by misconfigured servers. 999</li> 1000</ul> 1001<p /> 1002<h3><a name="Management_of_downloads_based_on"> </a> Management of downloads based on environment </h3> 1003<p /> 1004<ul> 1005<li> If the device routinely connects over wifi, delay non-interactive downloads by a certain amount of time in case wifi becomes available 1006</li> 1007<li> Turn on wifi if possible 1008</li> 1009<li> Fall back to cell when wifi is available but download doesn't proceed 1010</li> 1011<li> Be smarter about spurious losses (i.e. exceptions while network appears up) when the active network changes (e.g. turn on wifi while downloading over cell). 1012</li> 1013<li> Investigate the use of wifi locks, especially when performing non-resumable downloads. 1014</li> 1015<li> Poll network state (and maybe even try to connect) even without notifications from the connectivity manager (in case the notifications go AWOL or get inconsistent) . 1016</li> 1017<li> Pause when conditions degrade . 1018</li> 1019<li> Pause when roaming. 1020</li> 1021<li> Throttle or pause when user is active. 1022</li> 1023<li> Pause on slow networks (2G). 1024</li> 1025<li> Pause when battery is low. 1026</li> 1027<li> Throttle to not overwhelm the link. 1028</li> 1029<li> Pause when sync is active. 1030</li> 1031<li> Deal with situations where the active connection is down but there's another connection available 1032</li> 1033<li> Download files at night when the user is not explicitly waiting. 1034</li> 1035</ul> 1036<p /> 1037<h3><a name="Management_of_simultaneous_downl"> </a> Management of simultaneous downloads </h3> 1038<p /> 1039<ul> 1040<li> Pipeline requests on limited number of sockets, run downloads sequentially . 1041</li> 1042<li> Manage bandwidth to not starve foreground tasks. 1043</li> 1044<li> Run unsized downloads on their own (on a per-filesystem basis) to avoid failing multiple of them because of a full filesystem . 1045</li> 1046</ul> 1047<p /> 1048<h3><a name="Minor_functional_changes_edge_ca"> </a> Minor functional changes, edge cases </h3> 1049<p /> 1050<ul> 1051<li> The database could be somewhat checked when it's opened. 1052</li> 1053<li> <em>[DownloadProvider.java]</em> When upgrading the database, the numbering of ids should restart where it left off. 1054</li> 1055<li> <em>[DownloadProvider.java]</em> Handle errors when failing to start the service. 1056</li> 1057<li> <em>[DownloadProvider.java]</em> Explicitly populate all database columns that have documented default values, investigate whether that can be done at the SQL level. 1058</li> 1059<li> <em>[DownloadProvider.java]</em> It's possible that the last update time should be updated by the Sevice logic, not by the content provider. 1060</li> 1061<li> When relevant, combine logged messages on fewer lines. 1062</li> 1063<li> <em>[DownloadService.java]</em> Trim the database in the provider, not in the service. Notify application when trimming. Investigate why the row count seems off by one. Enforce on an ongoing basis. 1064</li> 1065<li> <em>[DownloadThread.java]</em> When download is restarted and MIME type wasn't provided by app, don't re-use MIME type. 1066</li> 1067<li> <em>[DownloadThread.java]</em> Deal with mistmatched file data sizes (between database and filesystem) when resuming a download, or with missing files that should be here. 1068</li> 1069<li> <em>[DownloadThread.java]</em> Validate that the response content-length can be properly parsed (i.e. presence of a string doesn't guarantee correctness). 1070</li> 1071<li> <em>[DownloadThread.java]</em> Be finer-grained with the way file permissions are managed in /cache - don't 0644 everything . 1072</li> 1073<li> Truncate files before deleting them, in case they're still open cross-process. 1074</li> 1075<li> Restart from scratch downloads that had very little progress . 1076</li> 1077<li> Deal with situations where /data is full as it prevents the database from growing (DoS) . 1078</li> 1079<li> Wait until file scanned to notify that download is completed. 1080</li> 1081<li> Missing some detailed logging about IOExceptions. 1082</li> 1083<li> Allow to disable LOGD debugging independently from system setting. 1084</li> 1085<li> Pulling the battery during a download corrupts files (lots of zeros written) . 1086</li> 1087<li> Should keep a bit of "emergency" database storage to initiate the download of an OTA update, in a file that is pre-allocated whenever possible (how to know it's an OTA update?). 1088</li> 1089<li> Figure out how to hook up into dumpsys and event log. 1090</li> 1091<li> Use the event log to log download progress. 1092</li> 1093<li> Use /cache to stage downloads that eventually go to the sd card, to avoid having sd files open too long in case the use pulls the card and to avoid partial files for too long. 1094</li> 1095<li> Maintain per-application usage statistics. 1096</li> 1097<li> There might be corner cases where the notifications are slightly off because different notifications might be using PendingIntents that can't be distinguished (differing only by their extras). 1098</li> 1099</ul> 1100<p /> 1101<h3><a name="Architecture_and_Implementation"> </a> Architecture and Implementation </h3> 1102<p /> 1103<ul> 1104<li> The content:// Uri of individual downloads could be cached instead of being re-built whenever it's needed. 1105</li> 1106<li> <em>[DownloadProvider.java]</em> Solidify extraction of id from URI 1107</li> 1108<li> <em>[DownloadProvider.java]</em> Use ContentURIs.parseId(uri) to extra the id from various functions. 1109</li> 1110<li> <em>[DownloadProvider.java]</em> Use StringBuilder to build the various queries. 1111</li> 1112<li> <em>[DownloadService.java]</em> Cache interface to the media scanner service more aggressively. 1113</li> 1114<li> <em>[DownloadService.java]</em> Investigate why unbinding from the media scanner service sometimes throws an exception 1115</li> 1116<li> <em>[DownloadService.java]</em> Handle exceptions in the service's UpdateThread - mark that there's no thread left. 1117</li> 1118<li> <em>[DownloadService.java]</em> At the end of UpdateThread, closing the cursor should be done even if there's an exception. Also log the exception, as we'd be in an inconsistent state. 1119</li> 1120<li> <em>[DownloadProvider.java]</em> Investigate whether the download provider should aggressively cache the result of <code>getContext()</code> and <code>getContext().getContentResolver()</code> 1121</li> 1122<li> Document the database columns that are most likely to stay unchanged throughout versions, to increase the chance being able to perform downgrades. 1123</li> 1124<li> <em>[DownloadService.java]</em> Sanity-check the ordering of the local cache when adding/removing rows. 1125</li> 1126<li> <em>[DownloadService.java]</em> Factor the code that checks for DRM types into a separate function. 1127</li> 1128<li> <em>[DownloadService.java]</em> Factor the code that notifies applications into a separate function (codepath with early 406 failure) 1129</li> 1130<li> <em>[DownloadService.java]</em> Check for errors when spawning download threads. 1131</li> 1132<li> <em>[DownloadService.java]</em> Potential race condition when a download completes at the same time as it gets deleted through the content provider - see deleteDownload(). 1133</li> 1134<li> <em>[DownloadService.java]</em> Catch all exceptions in scanFile - don't trust a remote process to the point where we'd let it crash us. 1135</li> 1136<li> <em>[DownloadService.java]</em> Limit number of attempts to scan a file. 1137</li> 1138<li> <em>[DownloadService.java]</em> Keep less data in RAM, especially about completed downloads. Investigating cutting unused columns if necessary 1139</li> 1140<li> <em>[DownloadThread.java]</em> Don't let exceptions out of run() - that'd kill the service, which'd accomplish no good. 1141</li> 1142<li> <em>[DownloadThread.java]</em> Keep track of content-length responses in a long, not in a string that we keep parsing . 1143</li> 1144<li> <em>[DownloadThread.java]</em> Use variable-size buffer to avoid thousands of operations on large downloads 1145</li> 1146<li> <em>[Helpers.java]</em> Deal with atomicity of checking/creating file. 1147</li> 1148<li> <em>[Helpers.java]</em> Handle differences between content-location separators and filesystem separators. 1149</li> 1150<li> Optimize database queries: use projections to reduce number of columns and get constant column numbers. 1151</li> 1152<li> Index last-mod date in DB, because of ordered searches. Investigate whether other columns need to be indexed (Hidden?) 1153</li> 1154<li> Deal with the fact that sqlite INTEGER matches java long (63-bit) . 1155</li> 1156<li> Use a single HTTP client for the entire download manager. 1157</li> 1158<li> Could use fewer alarms - currently setting new alarm each time database updated . 1159</li> 1160<li> Obsolete columns should be removed from the database . 1161</li> 1162<li> Assign relevant names to threads. 1163</li> 1164<li> Investigate and handle the different subclasses of IOException appropriately . 1165</li> 1166<li> There's potentially a race condition around read-modify-write cycles in the database, between the Service's updateFromProvider thread and the worker threads (and possibly more). Those should be synchronized appropriately, and the provider should be hardened to prevent asynchronous changes to sensitive data (or to synchronize when there's no other way, though I'd rather avoid that) . 1167</li> 1168<li> Temporary file leaks when downloads are deleted while the service isn't running . 1169</li> 1170<li> Increase priority of updaterThread while in the critical section (to avoid issues of priority inheritance with the main thread). 1171</li> 1172<li> Explicitly specify which interface to use for a given download (to get better sync with the connection manager). 1173</li> 1174<li> Cancel the requests on more kinds of errors instead of trusting the garbage collector. 1175</li> 1176<li> Issues with the fact that parseInt can throw exceptions on invalid server headers. 1177</li> 1178</ul> 1179<p /> 1180<h3><a name="Code_style_refactoring"> </a> Code style, refactoring </h3> 1181<p /> 1182<ul> 1183<li> Fix lint warnings 1184</li> 1185<li> Make sure that comments fit in 80 columns to match style guide 1186</li> 1187<li> Unify code style when dealing with lines longer than 100 characters 1188</li> 1189<li> <em>[Constants.java]</em> constants should be organized by logical groups to improve readability. 1190</li> 1191<li> Use fewer internal classes (Helpers, Constants...) . 1192</li> 1193</ul> 1194<p /> 1195<h3><a name="Browser_changes"> </a> Browser changes </h3> 1196<p /> 1197<ul> 1198<li> Move download UI outside of browser, so that browser doesn't need to access the provider. 1199</li> 1200<li> Make browser sniff zips and jars to see if they're apks. 1201</li> 1202<li> Live handoff of browser-initiated downloads (download in browser, browser update download UI, hand over to download manager on retry). 1203</li> 1204</ul> 1205</body> 1206</html> 1207