1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-27 02:07:12 +00:00

Cache generated avatars

This commit is contained in:
M66B 2022-08-18 07:42:15 +02:00
parent 0d6c094f8f
commit fb9874dc27

View file

@ -180,15 +180,17 @@ public class ContactInfo {
long now = new Date().getTime(); long now = new Date().getTime();
// Favicons // Favicons
Log.i("Cleanup favicons"); Log.i("Cleanup avatars");
File[] favicons = new File(context.getFilesDir(), "favicons").listFiles(); for (String type : new String[]{"favicons", "generated"}) {
if (favicons != null) File[] favicons = new File(context.getFilesDir(), type).listFiles();
for (File file : favicons) if (favicons != null)
if (file.lastModified() + CACHE_FAVICON_DURATION < now) { for (File file : favicons)
Log.i("Deleting " + file); if (file.lastModified() + CACHE_FAVICON_DURATION < now) {
if (!file.delete()) Log.i("Deleting " + file);
Log.w("Error deleting " + file); if (!file.delete())
} Log.w("Error deleting " + file);
}
}
} }
static void clearCache(Context context) { static void clearCache(Context context) {
@ -203,20 +205,22 @@ public class ContactInfo {
if (!files) if (!files)
return; return;
final File dir = new File(context.getFilesDir(), "favicons"); for (String type : new String[]{"favicons", "generated"}) {
executorFavicon.submit(new Runnable() { final File dir = new File(context.getFilesDir(), type);
@Override executorFavicon.submit(new Runnable() {
public void run() { @Override
try { public void run() {
File[] favicons = dir.listFiles(); try {
if (favicons != null) File[] favicons = dir.listFiles();
for (File favicon : favicons) if (favicons != null)
favicon.delete(); for (File favicon : favicons)
} catch (Throwable ex) { favicon.delete();
Log.w(ex); } catch (Throwable ex) {
Log.w(ex);
}
} }
} });
}); }
} }
@NonNull @NonNull
@ -506,9 +510,23 @@ public class ContactInfo {
// Generated // Generated
boolean identicon = false; boolean identicon = false;
if (info.bitmap == null && generated) { if (info.bitmap == null && generated && !TextUtils.isEmpty(info.email)) {
int dp = Helper.dp2pixels(context, GENERATED_ICON_SIZE); File dir = new File(context.getFilesDir(), "generated");
if (!TextUtils.isEmpty(info.email)) { 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) { if (identicons) {
identicon = true; identicon = true;
info.bitmap = ImageHelper.generateIdenticon( info.bitmap = ImageHelper.generateIdenticon(
@ -519,6 +537,15 @@ public class ContactInfo {
info.email, address.getPersonal(), dp, context); info.email, address.getPersonal(), dp, context);
info.type = "letter"; 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());
} }
} }