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:
parent
466ca8b3e3
commit
ca5f405322
1 changed files with 61 additions and 42 deletions
|
@ -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,42 +289,51 @@ 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)) {
|
||||||
ContentResolver resolver = context.getContentResolver();
|
File dir = Helper.ensureExists(context, "notcontact");
|
||||||
Uri uri = Uri.withAppendedPath(
|
File file = new File(dir, ekey);
|
||||||
ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI,
|
if (file.exists())
|
||||||
Uri.encode(info.email.toLowerCase(Locale.ROOT)));
|
Log.i("Contact negative cache key=" + ekey);
|
||||||
try (Cursor cursor = resolver.query(uri,
|
else {
|
||||||
new String[]{
|
ContentResolver resolver = context.getContentResolver();
|
||||||
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
|
Uri uri = Uri.withAppendedPath(
|
||||||
ContactsContract.Contacts.LOOKUP_KEY,
|
ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI,
|
||||||
ContactsContract.Contacts.DISPLAY_NAME
|
Uri.encode(info.email.toLowerCase(Locale.ROOT)));
|
||||||
},
|
try (Cursor cursor = resolver.query(uri,
|
||||||
null, null, null)) {
|
new String[]{
|
||||||
|
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
|
||||||
|
ContactsContract.Contacts.LOOKUP_KEY,
|
||||||
|
ContactsContract.Contacts.DISPLAY_NAME
|
||||||
|
},
|
||||||
|
null, null, null)) {
|
||||||
|
|
||||||
if (cursor != null && cursor.moveToNext()) {
|
if (cursor != null && cursor.moveToNext()) {
|
||||||
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
|
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
|
||||||
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
|
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
|
||||||
int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
|
int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
|
||||||
|
|
||||||
long contactId = cursor.getLong(colContactId);
|
long contactId = cursor.getLong(colContactId);
|
||||||
String lookupKey = cursor.getString(colLookupKey);
|
String lookupKey = cursor.getString(colLookupKey);
|
||||||
Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
|
Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
|
||||||
|
|
||||||
if (avatars)
|
if (avatars)
|
||||||
try (InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
|
try (InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
|
||||||
resolver, lookupUri, false)) {
|
resolver, lookupUri, false)) {
|
||||||
info.bitmap = BitmapFactory.decodeStream(is);
|
info.bitmap = BitmapFactory.decodeStream(is);
|
||||||
info.type = "contact";
|
info.type = "contact";
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
Log.e(ex);
|
Log.e(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
Log.e(ex);
|
||||||
}
|
}
|
||||||
} catch (Throwable ex) {
|
|
||||||
Log.e(ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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…
Reference in a new issue