From 1e6d41ce102a175b14c0e260e783eefac55f518f Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 30 Jan 2023 08:34:50 +0100 Subject: [PATCH] Use cached anonymous contact info --- .../java/eu/faircode/email/ContactInfo.java | 53 +++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ContactInfo.java b/app/src/main/java/eu/faircode/email/ContactInfo.java index 47a600705d..135cbd6954 100644 --- a/app/src/main/java/eu/faircode/email/ContactInfo.java +++ b/app/src/main/java/eu/faircode/email/ContactInfo.java @@ -29,6 +29,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.drawable.Drawable; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; @@ -97,6 +98,8 @@ public class ContactInfo { static final int FAVICON_READ_BYTES = 50 * 1024; + private static final Object lock = new Object(); + private static ContactInfo anonymous = null; private static Map emailLookup = new ConcurrentHashMap<>(); private static final Map emailContactInfo = new HashMap<>(); @@ -226,14 +229,27 @@ public class ContactInfo { } private static ContactInfo[] get(Context context, long account, String folderType, String selector, Address[] addresses, boolean cacheOnly) { - if (addresses == null || addresses.length == 0) - return new ContactInfo[]{new ContactInfo()}; + if (addresses == null || addresses.length == 0) { + ContactInfo anonymous = getAnonymous(context); + return new ContactInfo[]{anonymous == null ? new ContactInfo() : anonymous}; + } ContactInfo[] result = new ContactInfo[addresses.length]; for (int i = 0; i < addresses.length; i++) { result[i] = _get(context, account, folderType, selector, (InternetAddress) addresses[i], cacheOnly); - if (result[i] == null) - return null; + if (result[i] == null) { + if (cacheOnly) + return null; + ContactInfo anonymous = getAnonymous(context); + if (anonymous == null) + return null; + result[i] = anonymous; + } else if (result[i].bitmap == null) { + if (!cacheOnly) { + ContactInfo anonymous = getAnonymous(context); + result[i].bitmap = (anonymous == null ? null : anonymous.bitmap); + } + } } return result; @@ -577,6 +593,35 @@ public class ContactInfo { return info; } + private static ContactInfo getAnonymous(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean avatars = prefs.getBoolean("avatars", true); + boolean bimi = (prefs.getBoolean("bimi", false) && !BuildConfig.PLAY_STORE_RELEASE); + boolean gravatars = (prefs.getBoolean("gravatars", false) && !BuildConfig.PLAY_STORE_RELEASE); + boolean libravatars = (prefs.getBoolean("libravatars", false) && !BuildConfig.PLAY_STORE_RELEASE); + boolean favicons = prefs.getBoolean("favicons", false); + boolean generated = prefs.getBoolean("generated_icons", true); + boolean identicons = prefs.getBoolean("identicons", false); + if (avatars || bimi || gravatars || libravatars || favicons || generated || identicons) { + synchronized (lock) { + if (anonymous == null) { + Drawable d = context.getDrawable(R.drawable.twotone_person_24); + Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); + d.setTint(Helper.resolveColor(context, R.attr.colorSeparator)); + d.draw(canvas); + + anonymous = new ContactInfo(); + anonymous.bitmap = bitmap; + } + return anonymous; + } + } + + return null; + } + private static Favicon parseFavicon(URL base, int scaleToPixels, Context context) throws IOException { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean favicons_partial = prefs.getBoolean("favicons_partial", true);