diff options
author | Leon Scroggins <scroggo@google.com> | 2010-10-22 15:06:59 -0400 |
---|---|---|
committer | Leon Scroggins <scroggo@google.com> | 2010-10-27 10:31:11 -0400 |
commit | c6076533b489c17493640c14935324a30f582903 (patch) | |
tree | 01252ddbd00264d35af86451f57135b2166428e6 /src/com/android | |
parent | 7d132b17c9dd1a816c68bf8505be2028691aaee3 (diff) | |
download | packages_apps_Browser-c6076533b489c17493640c14935324a30f582903.zip packages_apps_Browser-c6076533b489c17493640c14935324a30f582903.tar.gz packages_apps_Browser-c6076533b489c17493640c14935324a30f582903.tar.bz2 |
Use the new public API for DownloadManager.
Bug:3116742
Change-Id: I5d0d9a12e1bd601cf6a95198578ce8f9acd81372
Diffstat (limited to 'src/com/android')
-rw-r--r-- | src/com/android/browser/BrowserActivity.java | 41 | ||||
-rw-r--r-- | src/com/android/browser/FetchUrlMimeType.java | 33 |
2 files changed, 36 insertions, 38 deletions
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java index 319ffad..2fe9e1b 100644 --- a/src/com/android/browser/BrowserActivity.java +++ b/src/com/android/browser/BrowserActivity.java @@ -3212,33 +3212,28 @@ public class BrowserActivity extends Activity return; } - // XXX: Have to use the old url since the cookies were stored using the - // old percent-encoded url. - String cookies = CookieManager.getInstance().getCookie(url); - - ContentValues values = new ContentValues(); - values.put(Downloads.Impl.COLUMN_URI, webAddress.toString()); - values.put(Downloads.Impl.COLUMN_COOKIE_DATA, cookies); - values.put(Downloads.Impl.COLUMN_USER_AGENT, userAgent); - values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE, - getPackageName()); - values.put(Downloads.Impl.COLUMN_NOTIFICATION_CLASS, - OpenDownloadReceiver.class.getCanonicalName()); - values.put(Downloads.Impl.COLUMN_VISIBILITY, - Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); - values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimetype); - values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename); - values.put(Downloads.Impl.COLUMN_DESCRIPTION, webAddress.getHost()); - if (contentLength > 0) { - values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, contentLength); - } + String addressString = webAddress.toString(); + Uri uri = Uri.parse(addressString); + DownloadManager.Request request = new DownloadManager.Request(uri); + request.setMimeType(mimetype); + request.setDestinationInExternalFilesDir(this, null, filename); + request.setDescription(webAddress.getHost()); if (mimetype == null) { + ContentValues values = new ContentValues(); + values.put(FetchUrlMimeType.URI, addressString); + // XXX: Have to use the old url since the cookies were stored using the + // old percent-encoded url. + String cookies = CookieManager.getInstance().getCookie(url); + values.put(FetchUrlMimeType.COOKIE_DATA, cookies); + values.put(FetchUrlMimeType.USER_AGENT, userAgent); + // We must have long pressed on a link or image to download it. We // are not sure of the mimetype in this case, so do a head request - new FetchUrlMimeType(this).execute(values); + new FetchUrlMimeType(this, request).execute(values); } else { - final Uri contentUri = - getContentResolver().insert(Downloads.Impl.CONTENT_URI, values); + DownloadManager manager + = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); + manager.enqueue(request); } Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT) .show(); diff --git a/src/com/android/browser/FetchUrlMimeType.java b/src/com/android/browser/FetchUrlMimeType.java index 9bd0cf9..4e2733f 100644 --- a/src/com/android/browser/FetchUrlMimeType.java +++ b/src/com/android/browser/FetchUrlMimeType.java @@ -16,7 +16,9 @@ package com.android.browser; +import android.app.DownloadManager; import android.content.ContentValues; +import android.content.Context; import android.net.Proxy; import android.net.Uri; import android.net.http.AndroidHttpClient; @@ -30,7 +32,6 @@ import org.apache.http.conn.params.ConnRouteParams; import java.io.IOException; import android.os.AsyncTask; -import android.provider.Downloads; import android.webkit.MimeTypeMap; import android.webkit.URLUtil; @@ -49,11 +50,17 @@ import android.webkit.URLUtil; */ class FetchUrlMimeType extends AsyncTask<ContentValues, String, String> { + public static final String URI = "uri"; + public static final String USER_AGENT = "user_agent"; + public static final String COOKIE_DATA = "cookie_data"; BrowserActivity mActivity; ContentValues mValues; + DownloadManager.Request mRequest; - public FetchUrlMimeType(BrowserActivity activity) { + public FetchUrlMimeType(BrowserActivity activity, + DownloadManager.Request request) { mActivity = activity; + mRequest = request; } @Override @@ -61,7 +68,7 @@ class FetchUrlMimeType extends AsyncTask<ContentValues, String, String> { mValues = values[0]; // Check to make sure we have a URI to download - String uri = mValues.getAsString(Downloads.Impl.COLUMN_URI); + String uri = mValues.getAsString(URI); if (uri == null || uri.length() == 0) { return null; } @@ -69,23 +76,18 @@ class FetchUrlMimeType extends AsyncTask<ContentValues, String, String> { // User agent is likely to be null, though the AndroidHttpClient // seems ok with that. AndroidHttpClient client = AndroidHttpClient.newInstance( - mValues.getAsString(Downloads.Impl.COLUMN_USER_AGENT)); + mValues.getAsString(USER_AGENT)); HttpHost httpHost = Proxy.getPreferredHttpHost(mActivity, uri); if (httpHost != null) { ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); } HttpHead request = new HttpHead(uri); - String cookie = mValues.getAsString(Downloads.Impl.COLUMN_COOKIE_DATA); + String cookie = mValues.getAsString(COOKIE_DATA); if (cookie != null && cookie.length() > 0) { request.addHeader("Cookie", cookie); } - String referer = mValues.getAsString(Downloads.Impl.COLUMN_REFERER); - if (referer != null && referer.length() > 0) { - request.addHeader("Referer", referer); - } - HttpResponse response; String mimeType = null; try { @@ -117,24 +119,25 @@ class FetchUrlMimeType extends AsyncTask<ContentValues, String, String> { @Override public void onPostExecute(String mimeType) { if (mimeType != null) { - String url = mValues.getAsString(Downloads.Impl.COLUMN_URI); + String url = mValues.getAsString(URI); if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension( MimeTypeMap.getFileExtensionFromUrl(url)); if (newMimeType != null) { - mValues.put(Downloads.Impl.COLUMN_MIME_TYPE, newMimeType); + mRequest.setMimeType(newMimeType); } } String filename = URLUtil.guessFileName(url, null, mimeType); - mValues.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename); + mRequest.setDestinationInExternalFilesDir(mActivity, null, filename); } // Start the download - final Uri contentUri = - mActivity.getContentResolver().insert(Downloads.Impl.CONTENT_URI, mValues); + DownloadManager manager = (DownloadManager) mActivity.getSystemService( + Context.DOWNLOAD_SERVICE); + manager.enqueue(mRequest); } } |