Limit favicon resolution

pull/184/head
M66B 5 years ago
parent 33c00d0c9b
commit 1071e060b9

@ -317,6 +317,7 @@ public class ContactInfo {
else else
info.bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); info.bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
else { else {
final int scaleToPixels = Helper.dp2pixels(context, 48);
final URL base = new URL("https://" + domain); final URL base = new URL("https://" + domain);
final URL www = new URL("https://www." + domain); final URL www = new URL("https://www." + domain);
@ -325,28 +326,28 @@ public class ContactInfo {
futures.add(executorFavicon.submit(new Callable<Bitmap>() { futures.add(executorFavicon.submit(new Callable<Bitmap>() {
@Override @Override
public Bitmap call() throws Exception { public Bitmap call() throws Exception {
return parseFavicon(base); return parseFavicon(base, scaleToPixels);
} }
})); }));
futures.add(executorFavicon.submit(new Callable<Bitmap>() { futures.add(executorFavicon.submit(new Callable<Bitmap>() {
@Override @Override
public Bitmap call() throws Exception { public Bitmap call() throws Exception {
return parseFavicon(www); return parseFavicon(www, scaleToPixels);
} }
})); }));
futures.add(executorFavicon.submit(new Callable<Bitmap>() { futures.add(executorFavicon.submit(new Callable<Bitmap>() {
@Override @Override
public Bitmap call() throws Exception { public Bitmap call() throws Exception {
return getFavicon(new URL(base, "favicon.ico")); return getFavicon(new URL(base, "favicon.ico"), scaleToPixels);
} }
})); }));
futures.add(executorFavicon.submit(new Callable<Bitmap>() { futures.add(executorFavicon.submit(new Callable<Bitmap>() {
@Override @Override
public Bitmap call() throws Exception { public Bitmap call() throws Exception {
return getFavicon(new URL(www, "favicon.ico")); return getFavicon(new URL(www, "favicon.ico"), scaleToPixels);
} }
})); }));
@ -421,7 +422,7 @@ public class ContactInfo {
return info; return info;
} }
private static Bitmap parseFavicon(URL base) throws IOException { private static Bitmap parseFavicon(URL base, int scaleToPixels) throws IOException {
Log.i("GET favicon " + base); Log.i("GET favicon " + base);
HttpsURLConnection connection = (HttpsURLConnection) base.openConnection(); HttpsURLConnection connection = (HttpsURLConnection) base.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
@ -452,13 +453,13 @@ public class ContactInfo {
} }
if (!TextUtils.isEmpty(favicon)) if (!TextUtils.isEmpty(favicon))
return getFavicon(new URL(base, favicon)); return getFavicon(new URL(base, favicon), scaleToPixels);
return null; return null;
} }
@NonNull @NonNull
private static Bitmap getFavicon(URL url) throws IOException { private static Bitmap getFavicon(URL url, int scaleToPixels) throws IOException {
Log.i("GET favicon " + url); Log.i("GET favicon " + url);
if (!"https".equals(url.getProtocol())) if (!"https".equals(url.getProtocol()))
@ -472,7 +473,7 @@ public class ContactInfo {
connection.connect(); connection.connect();
try { try {
Bitmap bitmap = BitmapFactory.decodeStream(connection.getInputStream()); Bitmap bitmap = ImageHelper.getScaledBitmap(connection.getInputStream(), url.toString(), scaleToPixels);
if (bitmap == null) if (bitmap == null)
throw new FileNotFoundException("decodeStream"); throw new FileNotFoundException("decodeStream");
else { else {

@ -623,7 +623,7 @@ class ImageHelper {
break; break;
} }
bm = getScaledBitmap(urlConnection.getInputStream(), source, dm); bm = getScaledBitmap(urlConnection.getInputStream(), source, dm.widthPixels);
} finally { } finally {
if (urlConnection != null) if (urlConnection != null)
urlConnection.disconnect(); urlConnection.disconnect();
@ -646,7 +646,7 @@ class ImageHelper {
return d; return d;
} }
private static Bitmap getScaledBitmap(InputStream is, String source, DisplayMetrics dm) throws IOException { static Bitmap getScaledBitmap(InputStream is, String source, int scaleToPixels) throws IOException {
BufferedInputStream bis = new BufferedInputStream(is); BufferedInputStream bis = new BufferedInputStream(is);
Log.i("Probe " + source); Log.i("Probe " + source);
@ -655,7 +655,6 @@ class ImageHelper {
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(bis, null, options); BitmapFactory.decodeStream(bis, null, options);
int scaleToPixels = dm.widthPixels;
int factor = 1; int factor = 1;
while (options.outWidth / factor > scaleToPixels) while (options.outWidth / factor > scaleToPixels)
factor *= 2; factor *= 2;

Loading…
Cancel
Save