diff --git a/app/build.gradle b/app/build.gradle index 161c194..b67e13c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -94,6 +94,8 @@ dependencies { // proxy support compile 'net.i2p.android:client:0.8' + compile 'com.squareup.okhttp3:okhttp:3.7.0' + // tor proxy def netcipherVersion = '2.0.0-alpha1' compile "info.guardianproject.netcipher:netcipher:$netcipherVersion" diff --git a/app/src/main/java/acr/browser/lightning/favicon/FaviconUtils.java b/app/src/main/java/acr/browser/lightning/favicon/FaviconUtils.java index 9dbfea7..a25e691 100644 --- a/app/src/main/java/acr/browser/lightning/favicon/FaviconUtils.java +++ b/app/src/main/java/acr/browser/lightning/favicon/FaviconUtils.java @@ -6,9 +6,8 @@ import android.support.annotation.Nullable; import android.text.TextUtils; /** - * Created by anthonycr on 4/13/17. + * Simple utils for favicon fetching. */ - class FaviconUtils { @Nullable static Uri safeUri(@NonNull String url) { diff --git a/app/src/main/java/acr/browser/lightning/favicon/ImageFetcher.java b/app/src/main/java/acr/browser/lightning/favicon/ImageFetcher.java index d915026..6dc37ce 100644 --- a/app/src/main/java/acr/browser/lightning/favicon/ImageFetcher.java +++ b/app/src/main/java/acr/browser/lightning/favicon/ImageFetcher.java @@ -9,10 +9,11 @@ import android.util.Log; import java.io.File; import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; import acr.browser.lightning.utils.Utils; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; /** * An image fetcher that creates image @@ -23,9 +24,9 @@ class ImageFetcher { private static final String TAG = "ImageFetcher"; @NonNull private final BitmapFactory.Options mLoaderOptions = new BitmapFactory.Options(); + @NonNull private final OkHttpClient mHttpClient = new OkHttpClient(); - ImageFetcher() { - } + ImageFetcher() {} @Nullable Bitmap retrieveFaviconFromCache(@NonNull File cacheFile) { @@ -45,37 +46,50 @@ class ImageFetcher { Bitmap retrieveBitmapFromGoogle(@NonNull Uri uri) { FaviconUtils.assertUriSafe(uri); - String googleFaviconUrl = "https://www.google.com/s2/favicons?domain_url=" + uri.toString(); + String googleFaviconUrl = "https://www.google.com/s2/favicons?domain_url=" + uri.getHost(); return retrieveBitmapFromUrl(googleFaviconUrl); } - @Nullable private Bitmap retrieveBitmapFromUrl(@NonNull String url) { - InputStream in = null; Bitmap icon = null; + InputStream boundsStream = null; + InputStream iconStream = null; + try { - final URL urlDownload = new URL(url); - final HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection(); - connection.setDoInput(true); - connection.setConnectTimeout(1000); - connection.setReadTimeout(1000); - connection.connect(); - in = connection.getInputStream(); - - if (in != null) { - icon = BitmapFactory.decodeStream(in, null, mLoaderOptions); - } - } catch (Exception ignored) { - Log.d(TAG, "Could not download icon from: " + url); + mLoaderOptions.inSampleSize = 1; + mLoaderOptions.inJustDecodeBounds = true; + + Request imageRequest = new Request.Builder().url(url).build(); + + Response boundsResponse = mHttpClient.newCall(imageRequest).execute(); + boundsStream = boundsResponse.body().byteStream(); + + BitmapFactory.decodeStream(boundsStream, null, mLoaderOptions); + + boundsResponse.body().close(); + + int size = Utils.dpToPx(24); + + mLoaderOptions.inJustDecodeBounds = false; + mLoaderOptions.inSampleSize = Utils.calculateInSampleSize(mLoaderOptions, size, size); + + Response imageResponse = mHttpClient.newCall(imageRequest).execute(); + iconStream = imageResponse.body().byteStream(); + + icon = BitmapFactory.decodeStream(iconStream, null, mLoaderOptions); + + imageResponse.body().close(); + } catch (Exception e) { + Log.d(TAG, "Unable to download icon: " + url); } finally { - Utils.close(in); + Utils.close(boundsStream); + Utils.close(iconStream); } return icon; } - } diff --git a/app/src/main/java/acr/browser/lightning/utils/Utils.java b/app/src/main/java/acr/browser/lightning/utils/Utils.java index 7ce25c8..fe604b5 100644 --- a/app/src/main/java/acr/browser/lightning/utils/Utils.java +++ b/app/src/main/java/acr/browser/lightning/utils/Utils.java @@ -256,6 +256,7 @@ public final class Utils { * @param bitmap is the bitmap to pad. * @return the padded bitmap. */ + @NonNull public static Bitmap padFavicon(@NonNull Bitmap bitmap) { int padding = Utils.dpToPx(4);