diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 177b27e8a0..93b0ed071b 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -581,7 +581,8 @@ public class ApplicationEx extends Application boolean reply_all = prefs.getBoolean("reply_all", false); if (reply_all) editor.remove("reply_all").putString("answer_action", "reply_all"); - } + } else if (version < 1841) + ContactInfo.clearCache(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !BuildConfig.DEBUG) editor.remove("background_service"); diff --git a/app/src/main/java/eu/faircode/email/ContactInfo.java b/app/src/main/java/eu/faircode/email/ContactInfo.java index ab6d6cf581..502452eace 100644 --- a/app/src/main/java/eu/faircode/email/ContactInfo.java +++ b/app/src/main/java/eu/faircode/email/ContactInfo.java @@ -115,6 +115,7 @@ public class ContactInfo { private static final int FAVICON_READ_TIMEOUT = 10 * 1000; // milliseconds private static final long CACHE_CONTACT_DURATION = 2 * 60 * 1000L; // milliseconds private static final long CACHE_FAVICON_DURATION = 2 * 7 * 24 * 60 * 60 * 1000L; // milliseconds + private static final float MIN_FAVICON_LUMINANCE = 0.2f; // https://css-tricks.com/prefetching-preloading-prebrowsing/ // https://developer.mozilla.org/en-US/docs/Web/Performance/dns-prefetch @@ -447,6 +448,19 @@ public class ContactInfo { try { Favicon favicon = future.get(); if (favicon != null) { + float lum = ImageHelper.getLuminance(favicon.bitmap); + if (lum < MIN_FAVICON_LUMINANCE) { + Bitmap bitmap = Bitmap.createBitmap( + favicon.bitmap.getWidth(), + favicon.bitmap.getHeight(), + favicon.bitmap.getConfig()); + bitmap.eraseColor(Color.WHITE); + Canvas canvas = new Canvas(bitmap); + canvas.drawBitmap(favicon.bitmap, 0, 0, null); + favicon.bitmap.recycle(); + favicon.bitmap = bitmap; + } + info.bitmap = favicon.bitmap; info.type = favicon.type; info.verified = favicon.verified; @@ -777,14 +791,7 @@ public class ContactInfo { Bitmap bitmap = ImageHelper.getScaledBitmap(connection.getInputStream(), url.toString(), mimeType, scaleToPixels); if (bitmap == null) throw new FileNotFoundException("decodeStream"); - else { - Bitmap favicon = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); - favicon.eraseColor(Color.WHITE); - Canvas canvas = new Canvas(favicon); - canvas.drawBitmap(bitmap, 0, 0, null); - bitmap.recycle(); - return new Favicon(favicon); - } + return new Favicon(bitmap); } finally { connection.disconnect(); } diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogTheme.java b/app/src/main/java/eu/faircode/email/FragmentDialogTheme.java index 5308046bc5..c0bdbf0c3c 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogTheme.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogTheme.java @@ -254,8 +254,6 @@ public class FragmentDialogTheme extends FragmentDialogBase { public void onClick(DialogInterface dialog, int which) { getActivity().getIntent().putExtra("tab", "display"); - ContactInfo.clearCache(context); - int optionId = rgThemeOptions.getCheckedRadioButtonId(); boolean reverse = (swReverse.isEnabled() && swReverse.isChecked()); boolean dark = (rgThemeOptions.isEnabled() && optionId == R.id.rbThemeDark); diff --git a/app/src/main/java/eu/faircode/email/ImageHelper.java b/app/src/main/java/eu/faircode/email/ImageHelper.java index 703d67d5fb..3fc4c22f04 100644 --- a/app/src/main/java/eu/faircode/email/ImageHelper.java +++ b/app/src/main/java/eu/faircode/email/ImageHelper.java @@ -912,6 +912,23 @@ class ImageHelper { } } + static float getLuminance(Bitmap bitmap) { + int n = 0; + float lum = 0; + int w = bitmap.getWidth(); + int h = bitmap.getHeight(); + for (int y = 0; y < h; y++) + for (int x = 0; x < w; x++) { + int color = bitmap.getPixel(x, y); + if (color != Color.TRANSPARENT) { + n++; + lum += ColorUtils.calculateLuminance(color); + } + } + + return (lum / n); + } + static class AnnotatedSource { private String source; private int width = 0;