diff options
author | Narayan Kamath <narayan@google.com> | 2014-10-27 17:21:20 +0000 |
---|---|---|
committer | Narayan Kamath <narayan@google.com> | 2014-10-27 17:23:52 +0000 |
commit | 30ce2c541e4957b16cdbb3cce3a12494571d0254 (patch) | |
tree | cda3522247711276364b911e50e4ae9e3d958b64 | |
parent | fe4083510dc773911651456f150bf5432f81a6c0 (diff) | |
download | Browser-30ce2c541e4957b16cdbb3cce3a12494571d0254.tar.gz |
Rewrite calls to Proxy.getPreferredHttpHost
Use java.net.URLConnection which is the only maintained method of
making http requests from java.
These were the last calls to getPreferredHttpHost and that hidden
API will now be deleted.
Change-Id: I97e022507ba656f627754af4e889f4812314ea90
-rw-r--r-- | src/com/android/browser/AddBookmarkPage.java | 4 | ||||
-rw-r--r-- | src/com/android/browser/DownloadTouchIcon.java | 80 | ||||
-rw-r--r-- | src/com/android/browser/FetchUrlMimeType.java | 110 | ||||
-rw-r--r-- | src/com/android/browser/Tab.java | 3 |
4 files changed, 77 insertions, 120 deletions
diff --git a/src/com/android/browser/AddBookmarkPage.java b/src/com/android/browser/AddBookmarkPage.java index fdb34c48..6e82dbf0 100644 --- a/src/com/android/browser/AddBookmarkPage.java +++ b/src/com/android/browser/AddBookmarkPage.java @@ -770,7 +770,7 @@ public class AddBookmarkPage extends Activity Bookmarks.addBookmark(AddBookmarkPage.this, false, url, title, thumbnail, mCurrentFolder); if (touchIconUrl != null) { - new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl); + new DownloadTouchIcon(cr, url).execute(mTouchIconUrl); } mMessage.arg1 = 1; } catch (IllegalStateException e) { @@ -937,7 +937,7 @@ public class AddBookmarkPage extends Activity Message msg = Message.obtain(mHandler, TOUCH_ICON_DOWNLOADED); msg.setData(bundle); - DownloadTouchIcon icon = new DownloadTouchIcon(this, msg, + DownloadTouchIcon icon = new DownloadTouchIcon(msg, mMap.getString(USER_AGENT)); icon.execute(mTouchIconUrl); } else { diff --git a/src/com/android/browser/DownloadTouchIcon.java b/src/com/android/browser/DownloadTouchIcon.java index ba299b6d..2d12cc76 100644 --- a/src/com/android/browser/DownloadTouchIcon.java +++ b/src/com/android/browser/DownloadTouchIcon.java @@ -16,12 +16,9 @@ package com.android.browser; -import org.apache.http.HttpEntity; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.params.HttpClientParams; -import org.apache.http.conn.params.ConnRouteParams; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; import android.content.ContentResolver; import android.content.ContentValues; @@ -29,8 +26,6 @@ import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.net.Proxy; -import android.net.http.AndroidHttpClient; import android.os.AsyncTask; import android.os.Bundle; import android.os.Message; @@ -50,7 +45,6 @@ class DownloadTouchIcon extends AsyncTask<String, Void, Void> { private final String mUserAgent; // Sites may serve a different icon to different UAs private Message mMessage; - private final Context mContext; /* package */ Tab mTab; /** @@ -58,9 +52,8 @@ class DownloadTouchIcon extends AsyncTask<String, Void, Void> { * the originalUrl so we take account of redirects. Used when the user * bookmarks a page from outside the bookmarks activity. */ - public DownloadTouchIcon(Tab tab, Context ctx, ContentResolver cr, WebView view) { + public DownloadTouchIcon(Tab tab, ContentResolver cr, WebView view) { mTab = tab; - mContext = ctx.getApplicationContext(); mContentResolver = cr; // Store these in case they change. mOriginalUrl = view.getOriginalUrl(); @@ -75,9 +68,8 @@ class DownloadTouchIcon extends AsyncTask<String, Void, Void> { * TODO: Would be nice to set the user agent here so that there is no * potential for the three different ctors here to return different icons. */ - public DownloadTouchIcon(Context ctx, ContentResolver cr, String url) { + public DownloadTouchIcon(ContentResolver cr, String url) { mTab = null; - mContext = ctx.getApplicationContext(); mContentResolver = cr; mOriginalUrl = null; mUrl = url; @@ -89,9 +81,8 @@ class DownloadTouchIcon extends AsyncTask<String, Void, Void> { * the passed Message's data bundle with the key * {@link BrowserContract.Bookmarks#TOUCH_ICON} and then send the message. */ - public DownloadTouchIcon(Context context, Message msg, String userAgent) { + public DownloadTouchIcon(Message msg, String userAgent) { mMessage = msg; - mContext = context.getApplicationContext(); mContentResolver = null; mOriginalUrl = null; mUrl = null; @@ -107,48 +98,39 @@ class DownloadTouchIcon extends AsyncTask<String, Void, Void> { boolean inDatabase = mCursor != null && mCursor.getCount() > 0; - String url = values[0]; - if (inDatabase || mMessage != null) { - AndroidHttpClient client = null; - HttpGet request = null; - + HttpURLConnection connection = null; try { - client = AndroidHttpClient.newInstance(mUserAgent); - HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, url); - if (httpHost != null) { - ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); + URL url = new URL(values[0]); + connection = (HttpURLConnection) url.openConnection(); + if (mUserAgent != null) { + connection.addRequestProperty("User-Agent", mUserAgent); } - request = new HttpGet(url); - - // Follow redirects - HttpClientParams.setRedirecting(client.getParams(), true); - - HttpResponse response = client.execute(request); - if (response.getStatusLine().getStatusCode() == 200) { - HttpEntity entity = response.getEntity(); - if (entity != null) { - InputStream content = entity.getContent(); - if (content != null) { - Bitmap icon = BitmapFactory.decodeStream( - content, null, null); - if (inDatabase) { - storeIcon(icon); - } else if (mMessage != null) { - Bundle b = mMessage.getData(); - b.putParcelable(BrowserContract.Bookmarks.TOUCH_ICON, icon); - } + if (connection.getResponseCode() == 200) { + InputStream content = connection.getInputStream(); + Bitmap icon = null; + try { + icon = BitmapFactory.decodeStream( + content, null, null); + } finally { + try { + content.close(); + } catch (IOException ignored) { } } + + if (inDatabase) { + storeIcon(icon); + } else if (mMessage != null) { + Bundle b = mMessage.getData(); + b.putParcelable(BrowserContract.Bookmarks.TOUCH_ICON, icon); + } } - } catch (Exception ex) { - if (request != null) { - request.abort(); - } + } catch (IOException ignored) { } finally { - if (client != null) { - client.close(); + if (connection != null) { + connection.disconnect(); } } } diff --git a/src/com/android/browser/FetchUrlMimeType.java b/src/com/android/browser/FetchUrlMimeType.java index 6556b380..4c64d009 100644 --- a/src/com/android/browser/FetchUrlMimeType.java +++ b/src/com/android/browser/FetchUrlMimeType.java @@ -16,16 +16,11 @@ package com.android.browser; -import org.apache.http.Header; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpHead; -import org.apache.http.conn.params.ConnRouteParams; +import java.net.HttpURLConnection; +import java.net.URL; import android.app.DownloadManager; import android.content.Context; -import android.net.Proxy; -import android.net.http.AndroidHttpClient; import android.os.Environment; import android.util.Log; import android.webkit.MimeTypeMap; @@ -64,80 +59,61 @@ class FetchUrlMimeType extends Thread { @Override public void run() { - // User agent is likely to be null, though the AndroidHttpClient - // seems ok with that. - AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); - HttpHost httpHost; + String mimeType = null; + String contentDisposition = null; + HttpURLConnection connection = null; try { - httpHost = Proxy.getPreferredHttpHost(mContext, mUri); - if (httpHost != null) { - ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); + URL url = new URL(mUri); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("HEAD"); + + if (mUserAgent != null) { + connection.addRequestProperty("User-Agent", mUserAgent); } - } catch (IllegalArgumentException ex) { - Log.e(LOGTAG,"Download failed: " + ex); - client.close(); - return; - } - HttpHead request = new HttpHead(mUri); - if (mCookies != null && mCookies.length() > 0) { - request.addHeader("Cookie", mCookies); - } + if (mCookies != null && mCookies.length() > 0) { + connection.addRequestProperty("Cookie", mCookies); + } - HttpResponse response; - String mimeType = null; - String contentDisposition = null; - try { - response = client.execute(request); - // We could get a redirect here, but if we do lets let - // the download manager take care of it, and thus trust that - // the server sends the right mimetype - if (response.getStatusLine().getStatusCode() == 200) { - Header header = response.getFirstHeader("Content-Type"); - if (header != null) { - mimeType = header.getValue(); + if (connection.getResponseCode() == 200) { + mimeType = connection.getContentType(); + if (mimeType != null) { final int semicolonIndex = mimeType.indexOf(';'); if (semicolonIndex != -1) { mimeType = mimeType.substring(0, semicolonIndex); } } - Header contentDispositionHeader = response.getFirstHeader("Content-Disposition"); - if (contentDispositionHeader != null) { - contentDisposition = contentDispositionHeader.getValue(); - } + + contentDisposition = connection.getHeaderField("Content-Disposition"); } - } catch (IllegalArgumentException ex) { - if (request != null) { - request.abort(); + } catch (IOException ioe) { + Log.e(LOGTAG,"Download failed: " + ioe); + } finally { + if (connection != null) { + connection.disconnect(); } - } catch (IOException ex) { - if (request != null) { - request.abort(); + } + + if (mimeType != null) { + if (mimeType.equalsIgnoreCase("text/plain") || + mimeType.equalsIgnoreCase("application/octet-stream")) { + String newMimeType = + MimeTypeMap.getSingleton().getMimeTypeFromExtension( + MimeTypeMap.getFileExtensionFromUrl(mUri)); + if (newMimeType != null) { + mimeType = newMimeType; + mRequest.setMimeType(newMimeType); + } } - } finally { - client.close(); + String filename = URLUtil.guessFileName(mUri, contentDisposition, + mimeType); + mRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); } - if (mimeType != null) { - if (mimeType.equalsIgnoreCase("text/plain") || - mimeType.equalsIgnoreCase("application/octet-stream")) { - String newMimeType = - MimeTypeMap.getSingleton().getMimeTypeFromExtension( - MimeTypeMap.getFileExtensionFromUrl(mUri)); - if (newMimeType != null) { - mimeType = newMimeType; - mRequest.setMimeType(newMimeType); - } - } - String filename = URLUtil.guessFileName(mUri, contentDisposition, - mimeType); - mRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); - } - - // Start the download - DownloadManager manager = (DownloadManager) mContext.getSystemService( - Context.DOWNLOAD_SERVICE); - manager.enqueue(mRequest); + // Start the download + DownloadManager manager = (DownloadManager) mContext.getSystemService( + Context.DOWNLOAD_SERVICE); + manager.enqueue(mRequest); } } diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java index 5d564a1f..78bf70e4 100644 --- a/src/com/android/browser/Tab.java +++ b/src/com/android/browser/Tab.java @@ -795,8 +795,7 @@ class Tab implements PictureListener { } // Have only one async task at a time. if (mTouchIconLoader == null) { - mTouchIconLoader = new DownloadTouchIcon(Tab.this, - mContext, cr, view); + mTouchIconLoader = new DownloadTouchIcon(Tab.this, cr, view); mTouchIconLoader.execute(url); } } |