diff options
author | Leon Scroggins <scroggo@google.com> | 2010-11-18 15:16:27 -0500 |
---|---|---|
committer | Leon Scroggins <scroggo@google.com> | 2010-11-19 10:14:24 -0500 |
commit | 63c0266b5d1fca4df859fe4fa3a9555d0783a2b6 (patch) | |
tree | 9d13c00d9278d4804409201d783000983e71bfc5 /src/com/android/browser/FetchUrlMimeType.java | |
parent | f47d0af5b694e1f0113942c07a8659d58518acc8 (diff) | |
download | android_packages_apps_Gello-63c0266b5d1fca4df859fe4fa3a9555d0783a2b6.tar.gz android_packages_apps_Gello-63c0266b5d1fca4df859fe4fa3a9555d0783a2b6.tar.bz2 android_packages_apps_Gello-63c0266b5d1fca4df859fe4fa3a9555d0783a2b6.zip |
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
Diffstat (limited to 'src/com/android/browser/FetchUrlMimeType.java')
-rw-r--r-- | src/com/android/browser/FetchUrlMimeType.java | 72 |
1 files changed, 25 insertions, 47 deletions
diff --git a/src/com/android/browser/FetchUrlMimeType.java b/src/com/android/browser/FetchUrlMimeType.java index 0481806e..2538d90a 100644 --- a/src/com/android/browser/FetchUrlMimeType.java +++ b/src/com/android/browser/FetchUrlMimeType.java @@ -24,11 +24,9 @@ import org.apache.http.conn.params.ConnRouteParams; import android.app.Activity; import android.app.DownloadManager; -import android.content.ContentValues; import android.content.Context; import android.net.Proxy; import android.net.http.AndroidHttpClient; -import android.os.AsyncTask; import android.webkit.MimeTypeMap; import android.webkit.URLUtil; @@ -37,59 +35,48 @@ import java.io.IOException; /** * This class is used to pull down the http headers of a given URL so that * we can analyse the mimetype and make any correction needed before we give - * the URL to the download manager. The ContentValues class holds the - * content that would be provided to the download manager, so that on - * completion of checking the mimetype, we can issue the download to - * the download manager. + * the URL to the download manager. * This operation is needed when the user long-clicks on a link or image and * we don't know the mimetype. If the user just clicks on the link, we will * do the same steps of correcting the mimetype down in * android.os.webkit.LoadListener rather than handling it here. * */ -class FetchUrlMimeType extends AsyncTask<ContentValues, String, ContentValues> { +class FetchUrlMimeType extends Thread { - public static final String URI = "uri"; - public static final String USER_AGENT = "user_agent"; - public static final String COOKIE_DATA = "cookie_data"; + private Activity mActivity; + private DownloadManager.Request mRequest; + private String mUri; + private String mCookies; + private String mUserAgent; - Activity mActivity; - ContentValues mValues; - DownloadManager.Request mRequest; - - public FetchUrlMimeType(Activity activity, - DownloadManager.Request request) { + public FetchUrlMimeType(Activity activity, DownloadManager.Request request, + String uri, String cookies, String userAgent) { mActivity = activity; mRequest = request; + mUri = uri; + mCookies = cookies; + mUserAgent = userAgent; } @Override - public ContentValues doInBackground(ContentValues... values) { - mValues = values[0]; - - // Check to make sure we have a URI to download - String uri = mValues.getAsString(URI); - if (uri == null || uri.length() == 0) { - return null; - } - + public void run() { // User agent is likely to be null, though the AndroidHttpClient // seems ok with that. - AndroidHttpClient client = AndroidHttpClient.newInstance( - mValues.getAsString(USER_AGENT)); - HttpHost httpHost = Proxy.getPreferredHttpHost(mActivity, uri); + AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); + HttpHost httpHost = Proxy.getPreferredHttpHost(mActivity, mUri); if (httpHost != null) { ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); } - HttpHead request = new HttpHead(uri); + HttpHead request = new HttpHead(mUri); - String cookie = mValues.getAsString(COOKIE_DATA); - if (cookie != null && cookie.length() > 0) { - request.addHeader("Cookie", cookie); + if (mCookies != null && mCookies.length() > 0) { + request.addHeader("Cookie", mCookies); } HttpResponse response; - ContentValues result = new ContentValues(); + String mimeType = null; + String contentDisposition = null; try { response = client.execute(request); // We could get a redirect here, but if we do lets let @@ -98,16 +85,15 @@ class FetchUrlMimeType extends AsyncTask<ContentValues, String, ContentValues> { if (response.getStatusLine().getStatusCode() == 200) { Header header = response.getFirstHeader("Content-Type"); if (header != null) { - String mimeType = header.getValue(); + mimeType = header.getValue(); final int semicolonIndex = mimeType.indexOf(';'); if (semicolonIndex != -1) { mimeType = mimeType.substring(0, semicolonIndex); } - result.put("Content-Type", mimeType); } Header contentDispositionHeader = response.getFirstHeader("Content-Disposition"); if (contentDispositionHeader != null) { - result.put("Content-Disposition", contentDispositionHeader.getValue()); + contentDisposition = contentDispositionHeader.getValue(); } } } catch (IllegalArgumentException ex) { @@ -118,26 +104,18 @@ class FetchUrlMimeType extends AsyncTask<ContentValues, String, ContentValues> { client.close(); } - return result; - } - - @Override - public void onPostExecute(ContentValues values) { - final String mimeType = values.getAsString("Content-Type"); - final String contentDisposition = values.getAsString("Content-Disposition"); if (mimeType != null) { - String url = mValues.getAsString(URI); if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension( - MimeTypeMap.getFileExtensionFromUrl(url)); + MimeTypeMap.getFileExtensionFromUrl(mUri)); if (newMimeType != null) { mRequest.setMimeType(newMimeType); } } - String filename = URLUtil.guessFileName(url, - contentDisposition, mimeType); + String filename = URLUtil.guessFileName(mUri, contentDisposition, + mimeType); mRequest.setDestinationInExternalFilesDir(mActivity, null, filename); } |