From 63c0266b5d1fca4df859fe4fa3a9555d0783a2b6 Mon Sep 17 00:00:00 2001 From: Leon Scroggins Date: Thu, 18 Nov 2010 15:16:27 -0500 Subject: Move download requests to the background. Bug:3189668 Do not create a DownloadHandler since the methods can all be static. Do not pass the length to DownloadHandler, since it is no longer used. Change-Id: I280160f62906d1acb263b45fde57062210005a0a --- src/com/android/browser/DownloadHandler.java | 74 ++++++++++++++-------------- 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'src/com/android/browser/DownloadHandler.java') diff --git a/src/com/android/browser/DownloadHandler.java b/src/com/android/browser/DownloadHandler.java index b833bd7cc..cbf26f40f 100644 --- a/src/com/android/browser/DownloadHandler.java +++ b/src/com/android/browser/DownloadHandler.java @@ -29,6 +29,7 @@ import android.content.pm.ResolveInfo; import android.net.Uri; import android.net.WebAddress; import android.os.Environment; +import android.text.TextUtils; import android.util.Log; import android.webkit.CookieManager; import android.webkit.URLUtil; @@ -44,23 +45,17 @@ public class DownloadHandler { private static final String LOGTAG = "DLHandler"; - Activity mActivity; - - public DownloadHandler(Activity activity) { - mActivity = activity; - } - /** * Notify the host application a download should be done, or that * the data should be streamed if a streaming viewer is available. + * @param activity Activity requesting the download. * @param url The full url to the content that should be downloaded - * @param contentDisposition Content-disposition http header, if - * present. + * @param userAgent User agent of the downloading application. + * @param contentDisposition Content-disposition http header, if present. * @param mimetype The mimetype of the content reported by the server - * @param contentLength The file size reported by the server */ - public void onDownloadStart(String url, String userAgent, - String contentDisposition, String mimetype, long contentLength) { + public static void onDownloadStart(Activity activity, String url, + String userAgent, String contentDisposition, String mimetype) { // if we're dealing wih A/V content that's not explicitly marked // for download, check if it's streamable. if (contentDisposition == null @@ -70,10 +65,10 @@ public class DownloadHandler { // that matches. Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(url), mimetype); - ResolveInfo info = mActivity.getPackageManager().resolveActivity(intent, + ResolveInfo info = activity.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); if (info != null) { - ComponentName myName = mActivity.getComponentName(); + ComponentName myName = activity.getComponentName(); // If we resolved to ourselves, we don't want to attempt to // load the url only to try and download it again. if (!myName.getPackageName().equals( @@ -83,7 +78,7 @@ public class DownloadHandler { // someone (other than us) knows how to handle this mime // type with this scheme, don't download. try { - mActivity.startActivity(intent); + activity.startActivity(intent); return; } catch (ActivityNotFoundException ex) { if (LOGD_ENABLED) { @@ -97,7 +92,8 @@ public class DownloadHandler { } } } - onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength); + onDownloadStartNoStream(activity, url, userAgent, contentDisposition, + mimetype); } // This is to work around the fact that java.net.URI throws Exceptions @@ -133,14 +129,15 @@ public class DownloadHandler { /** * Notify the host application a download should be done, even if there * is a streaming viewer available for thise type. + * @param activity Activity requesting the download. * @param url The full url to the content that should be downloaded - * @param contentDisposition Content-disposition http header, if - * present. + * @param userAgent User agent of the downloading application. + * @param contentDisposition Content-disposition http header, if present. * @param mimetype The mimetype of the content reported by the server - * @param contentLength The file size reported by the server */ - /*package */ void onDownloadStartNoStream(String url, String userAgent, - String contentDisposition, String mimetype, long contentLength) { + /*package */ static void onDownloadStartNoStream(Activity activity, + String url, String userAgent, String contentDisposition, + String mimetype) { String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); @@ -153,14 +150,14 @@ public class DownloadHandler { // Check to see if the SDCard is busy, same as the music app if (status.equals(Environment.MEDIA_SHARED)) { - msg = mActivity.getString(R.string.download_sdcard_busy_dlg_msg); + msg = activity.getString(R.string.download_sdcard_busy_dlg_msg); title = R.string.download_sdcard_busy_dlg_title; } else { - msg = mActivity.getString(R.string.download_no_sdcard_dlg_msg, filename); + msg = activity.getString(R.string.download_no_sdcard_dlg_msg, filename); title = R.string.download_no_sdcard_dlg_title; } - new AlertDialog.Builder(mActivity) + new AlertDialog.Builder(activity) .setTitle(title) .setIcon(android.R.drawable.ic_dialog_alert) .setMessage(msg) @@ -184,34 +181,37 @@ public class DownloadHandler { String addressString = webAddress.toString(); Uri uri = Uri.parse(addressString); - DownloadManager.Request request = new DownloadManager.Request(uri); + final DownloadManager.Request request = new DownloadManager.Request(uri); request.setMimeType(mimetype); - request.setDestinationInExternalFilesDir(mActivity, null, filename); + request.setDestinationInExternalFilesDir(activity, null, filename); // let this downloaded file be scanned by MediaScanner - so that it can // show up in Gallery app, for example. request.allowScanningByMediaScanner(); request.setDescription(webAddress.getHost()); + // XXX: Have to use the old url since the cookies were stored using the + // old percent-encoded url. String cookies = CookieManager.getInstance().getCookie(url); request.addRequestHeader("cookie", cookies); request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 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. - values.put(FetchUrlMimeType.COOKIE_DATA, cookies); - values.put(FetchUrlMimeType.USER_AGENT, userAgent); - + if (TextUtils.isEmpty(addressString)) { + return; + } // 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(mActivity, request).execute(values); + new FetchUrlMimeType(activity, request, addressString, cookies, + userAgent).start(); } else { - DownloadManager manager - = (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE); - manager.enqueue(request); + final DownloadManager manager + = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE); + new Thread("Browser download") { + public void run() { + manager.enqueue(request); + } + }.start(); } - Toast.makeText(mActivity, R.string.download_pending, Toast.LENGTH_SHORT) + Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT) .show(); } -- cgit v1.2.3