|
|
@ -9,10 +9,11 @@ import android.util.Log; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.net.HttpURLConnection; |
|
|
|
|
|
|
|
import java.net.URL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import acr.browser.lightning.utils.Utils; |
|
|
|
import acr.browser.lightning.utils.Utils; |
|
|
|
|
|
|
|
import okhttp3.OkHttpClient; |
|
|
|
|
|
|
|
import okhttp3.Request; |
|
|
|
|
|
|
|
import okhttp3.Response; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* An image fetcher that creates image |
|
|
|
* An image fetcher that creates image |
|
|
@ -23,9 +24,9 @@ class ImageFetcher { |
|
|
|
private static final String TAG = "ImageFetcher"; |
|
|
|
private static final String TAG = "ImageFetcher"; |
|
|
|
|
|
|
|
|
|
|
|
@NonNull private final BitmapFactory.Options mLoaderOptions = new BitmapFactory.Options(); |
|
|
|
@NonNull private final BitmapFactory.Options mLoaderOptions = new BitmapFactory.Options(); |
|
|
|
|
|
|
|
@NonNull private final OkHttpClient mHttpClient = new OkHttpClient(); |
|
|
|
|
|
|
|
|
|
|
|
ImageFetcher() { |
|
|
|
ImageFetcher() {} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
Bitmap retrieveFaviconFromCache(@NonNull File cacheFile) { |
|
|
|
Bitmap retrieveFaviconFromCache(@NonNull File cacheFile) { |
|
|
@ -45,37 +46,50 @@ class ImageFetcher { |
|
|
|
Bitmap retrieveBitmapFromGoogle(@NonNull Uri uri) { |
|
|
|
Bitmap retrieveBitmapFromGoogle(@NonNull Uri uri) { |
|
|
|
FaviconUtils.assertUriSafe(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); |
|
|
|
return retrieveBitmapFromUrl(googleFaviconUrl); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
private Bitmap retrieveBitmapFromUrl(@NonNull String url) { |
|
|
|
private Bitmap retrieveBitmapFromUrl(@NonNull String url) { |
|
|
|
InputStream in = null; |
|
|
|
|
|
|
|
Bitmap icon = null; |
|
|
|
Bitmap icon = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InputStream boundsStream = null; |
|
|
|
|
|
|
|
InputStream iconStream = null; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
final URL urlDownload = new URL(url); |
|
|
|
mLoaderOptions.inSampleSize = 1; |
|
|
|
final HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection(); |
|
|
|
mLoaderOptions.inJustDecodeBounds = true; |
|
|
|
connection.setDoInput(true); |
|
|
|
|
|
|
|
connection.setConnectTimeout(1000); |
|
|
|
Request imageRequest = new Request.Builder().url(url).build(); |
|
|
|
connection.setReadTimeout(1000); |
|
|
|
|
|
|
|
connection.connect(); |
|
|
|
Response boundsResponse = mHttpClient.newCall(imageRequest).execute(); |
|
|
|
in = connection.getInputStream(); |
|
|
|
boundsStream = boundsResponse.body().byteStream(); |
|
|
|
|
|
|
|
|
|
|
|
if (in != null) { |
|
|
|
BitmapFactory.decodeStream(boundsStream, null, mLoaderOptions); |
|
|
|
icon = BitmapFactory.decodeStream(in, null, mLoaderOptions); |
|
|
|
|
|
|
|
} |
|
|
|
boundsResponse.body().close(); |
|
|
|
} catch (Exception ignored) { |
|
|
|
|
|
|
|
Log.d(TAG, "Could not download icon from: " + url); |
|
|
|
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 { |
|
|
|
} finally { |
|
|
|
Utils.close(in); |
|
|
|
Utils.close(boundsStream); |
|
|
|
|
|
|
|
Utils.close(iconStream); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return icon; |
|
|
|
return icon; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|