Optional favicon white background

This commit is contained in:
M66B 2022-02-18 13:40:32 +01:00
parent 4708d86204
commit b95225fc2e
4 changed files with 34 additions and 11 deletions

View File

@ -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");

View File

@ -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();
}

View File

@ -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);

View File

@ -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;