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,8 +180,9 @@ public class ContactInfo {
long now = new Date().getTime();
// Favicons
Log.i("Cleanup favicons");
File[] favicons = new File(context.getFilesDir(), "favicons").listFiles();
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) {
@ -190,6 +191,7 @@ public class ContactInfo {
Log.w("Error deleting " + file);
}
}
}
static void clearCache(Context context) {
clearCache(context, true);
@ -203,7 +205,8 @@ public class ContactInfo {
if (!files)
return;
final File dir = new File(context.getFilesDir(), "favicons");
for (String type : new String[]{"favicons", "generated"}) {
final File dir = new File(context.getFilesDir(), type);
executorFavicon.submit(new Runnable() {
@Override
public void run() {
@ -218,6 +221,7 @@ public class ContactInfo {
}
});
}
}
@NonNull
static ContactInfo[] get(Context context, long account, String folderType, String selector, Address[] addresses) {
@ -506,9 +510,23 @@ public class ContactInfo {
// Generated
boolean identicon = false;
if (info.bitmap == null && generated) {
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 (!TextUtils.isEmpty(info.email)) {
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());
}
}