From fb9874dc276e95063ecf163583735931b17b6058 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 18 Aug 2022 07:42:15 +0200 Subject: [PATCH] Cache generated avatars --- .../java/eu/faircode/email/ContactInfo.java | 77 +++++++++++++------ 1 file changed, 52 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ContactInfo.java b/app/src/main/java/eu/faircode/email/ContactInfo.java index 2d190a153d..9e7cf814e7 100644 --- a/app/src/main/java/eu/faircode/email/ContactInfo.java +++ b/app/src/main/java/eu/faircode/email/ContactInfo.java @@ -180,15 +180,17 @@ public class ContactInfo { long now = new Date().getTime(); // Favicons - Log.i("Cleanup favicons"); - File[] favicons = new File(context.getFilesDir(), "favicons").listFiles(); - if (favicons != null) - for (File file : favicons) - if (file.lastModified() + CACHE_FAVICON_DURATION < now) { - Log.i("Deleting " + file); - if (!file.delete()) - Log.w("Error deleting " + file); - } + Log.i("Cleanup avatars"); + for (String type : new String[]{"favicons", "generated"}) { + File[] favicons = new File(context.getFilesDir(), type).listFiles(); + if (favicons != null) + for (File file : favicons) + if (file.lastModified() + CACHE_FAVICON_DURATION < now) { + Log.i("Deleting " + file); + if (!file.delete()) + Log.w("Error deleting " + file); + } + } } static void clearCache(Context context) { @@ -203,20 +205,22 @@ public class ContactInfo { if (!files) return; - final File dir = new File(context.getFilesDir(), "favicons"); - executorFavicon.submit(new Runnable() { - @Override - public void run() { - try { - File[] favicons = dir.listFiles(); - if (favicons != null) - for (File favicon : favicons) - favicon.delete(); - } catch (Throwable ex) { - Log.w(ex); + for (String type : new String[]{"favicons", "generated"}) { + final File dir = new File(context.getFilesDir(), type); + executorFavicon.submit(new Runnable() { + @Override + public void run() { + try { + File[] favicons = dir.listFiles(); + if (favicons != null) + for (File favicon : favicons) + favicon.delete(); + } catch (Throwable ex) { + Log.w(ex); + } } - } - }); + }); + } } @NonNull @@ -506,9 +510,23 @@ public class ContactInfo { // Generated boolean identicon = false; - if (info.bitmap == null && generated) { - int dp = Helper.dp2pixels(context, GENERATED_ICON_SIZE); - if (!TextUtils.isEmpty(info.email)) { + if (info.bitmap == null && generated && !TextUtils.isEmpty(info.email)) { + File dir = new File(context.getFilesDir(), "generated"); + if (!dir.exists()) + dir.mkdir(); + + File[] files = dir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File file, String name) { + return name.startsWith(info.email); + } + }); + if (files != null && files.length == 1) { + Log.i("Generated from cache=" + files[0].getName()); + info.bitmap = BitmapFactory.decodeFile(files[0].getAbsolutePath()); + info.type = Helper.getExtension(files[0].getName()); + } else { + int dp = Helper.dp2pixels(context, GENERATED_ICON_SIZE); if (identicons) { identicon = true; info.bitmap = ImageHelper.generateIdenticon( @@ -519,6 +537,15 @@ public class ContactInfo { info.email, address.getPersonal(), dp, context); info.type = "letter"; } + + // Add to cache + File output = new File(dir, info.email + "." + info.type); + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(output))) { + info.bitmap.compress(Bitmap.CompressFormat.PNG, 90, os); + } catch (IOException ex) { + Log.e(ex); + } + Log.i("Generated to cache=" + output.getName()); } }