1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-26 09:47:13 +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();
// 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());
}
}