Negative cache contact info

pull/214/head
M66B 2 years ago
parent 466ca8b3e3
commit ca5f405322

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

Loading…
Cancel
Save