1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2025-02-23 14:41:08 +00:00

Negative cache contact info

This commit is contained in:
M66B 2024-01-13 07:29:46 +01:00
parent 466ca8b3e3
commit ca5f405322

View file

@ -200,7 +200,7 @@ public class ContactInfo {
if (!files)
return;
for (String type : new String[]{"favicons", "generated"}) {
for (String type : new String[]{"notcontact", "favicons", "generated"}) {
final File dir = Helper.ensureExists(context, type);
Helper.getParallelExecutor().submit(new Runnable() {
@Override
@ -271,15 +271,7 @@ public class ContactInfo {
ContactInfo info = new ContactInfo();
info.email = address.getAddress();
// Maximum file name length: 255
// Maximum email address length: 320 (<local part = 64> @ <domain part = 255>)
final String ekey;
if (TextUtils.isEmpty(info.email))
ekey = null;
else
ekey = (info.email.length() > 255
? info.email.substring(0, 255)
: info.email).toLowerCase(Locale.ROOT);
final String ekey = (TextUtils.isEmpty(info.email) ? null : getKey(info.email));
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean avatars = prefs.getBoolean("avatars", true);
@ -297,6 +289,11 @@ public class ContactInfo {
if (!TextUtils.isEmpty(info.email) &&
(avatars || prefer_contact || distinguish_contacts) &&
Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
File dir = Helper.ensureExists(context, "notcontact");
File file = new File(dir, ekey);
if (file.exists())
Log.i("Contact negative cache key=" + ekey);
else {
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI,
@ -330,11 +327,15 @@ public class ContactInfo {
info.displayName = cursor.getString(colDisplayName);
info.lookupUri = lookupUri;
info.known = true;
} else {
file.createNewFile();
Log.i("Contact negative cached key=" + ekey);
}
} catch (Throwable ex) {
Log.e(ex);
}
}
}
// Favicon
if (info.bitmap == null &&
@ -1019,6 +1020,14 @@ public class ContactInfo {
}
}
static String getKey(String email) {
// Maximum file name length: 255
// Maximum email address length: 320 (<local part = 64> @ <domain part = 255>)
return (email.length() > 255
? email.substring(0, 255)
: email).toLowerCase(Locale.ROOT);
}
static Uri getLookupUri(List<Address> addresses) {
return getLookupUri(addresses.toArray(new Address[0]));
}
@ -1079,8 +1088,9 @@ public class ContactInfo {
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
Log.i("Reading email/uri");
ContentResolver resolver = context.getContentResolver();
File dir = Helper.ensureExists(context, "notcontact");
ContentResolver resolver = context.getContentResolver();
try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
@ -1100,6 +1110,15 @@ public class ContactInfo {
lookup.uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
lookup.displayName = displayName;
all.put(email.toLowerCase(Locale.ROOT), lookup);
if (!TextUtils.isEmpty(email)) {
String ekey = getKey(email);
File file = new File(dir, ekey);
if (file.exists()) {
Log.i("Contact delete cached key=" + ekey);
Helper.secureDelete(file);
}
}
}
} catch (Throwable ex) {
Log.e(ex);