mirror of
https://github.com/M66B/FairEmail.git
synced 2026-04-04 08:08:16 +02:00
Improved avatar/identicon caching
This commit is contained in:
@@ -3,12 +3,13 @@ package eu.faircode.email;
|
||||
import android.Manifest;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.provider.ContactsContract;
|
||||
|
||||
import java.io.InputStream;
|
||||
@@ -22,8 +23,8 @@ import javax.mail.internet.InternetAddress;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
public class ContactInfo {
|
||||
private InputStream is;
|
||||
private Drawable photo;
|
||||
private String email;
|
||||
private Bitmap bitmap;
|
||||
private String displayName;
|
||||
private Uri lookupUri;
|
||||
private long time;
|
||||
@@ -32,118 +33,124 @@ public class ContactInfo {
|
||||
|
||||
private static final long CACHE_DURATION = 60 * 1000L;
|
||||
|
||||
ContactInfo() {
|
||||
}
|
||||
|
||||
ContactInfo(String displayName) {
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
ContactInfo(Drawable photo, String displayName) {
|
||||
this.photo = photo;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
Bitmap getPhotoBitmap() {
|
||||
return BitmapFactory.decodeStream(is);
|
||||
}
|
||||
|
||||
Drawable getPhotoDrawable() {
|
||||
if (photo != null)
|
||||
return photo;
|
||||
|
||||
if (is == null)
|
||||
return null;
|
||||
|
||||
return Drawable.createFromStream(is, displayName == null ? "Photo" : displayName);
|
||||
private ContactInfo() {
|
||||
}
|
||||
|
||||
boolean hasPhoto() {
|
||||
return (is != null || photo != null);
|
||||
return (bitmap != null);
|
||||
}
|
||||
|
||||
String getDisplayName() {
|
||||
return displayName;
|
||||
Bitmap getPhotoBitmap() {
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
boolean hasDisplayName() {
|
||||
return (displayName != null);
|
||||
}
|
||||
|
||||
Uri getLookupUri() {
|
||||
return lookupUri;
|
||||
String getDisplayName(boolean compact) {
|
||||
if (compact && displayName != null)
|
||||
return displayName;
|
||||
else if (displayName == null)
|
||||
return (email == null ? "" : email);
|
||||
else
|
||||
return displayName + " <" + email + ">";
|
||||
}
|
||||
|
||||
boolean hasLookupUri() {
|
||||
return (lookupUri != null);
|
||||
}
|
||||
|
||||
Uri getLookupUri() {
|
||||
return lookupUri;
|
||||
}
|
||||
|
||||
private boolean isExpired() {
|
||||
return (new Date().getTime() - time > CACHE_DURATION);
|
||||
}
|
||||
|
||||
static ContactInfo get(Context context, Address[] addresses, boolean cached) {
|
||||
static void clearCache() {
|
||||
synchronized (emailContactInfo) {
|
||||
emailContactInfo.clear();
|
||||
}
|
||||
}
|
||||
|
||||
static ContactInfo get(Context context, Address[] addresses, boolean cacheOnly) {
|
||||
if (addresses == null || addresses.length == 0)
|
||||
return null;
|
||||
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
|
||||
!= PackageManager.PERMISSION_GRANTED)
|
||||
return null;
|
||||
|
||||
String email = ((InternetAddress) addresses[0]).getAddress();
|
||||
return new ContactInfo();
|
||||
InternetAddress address = (InternetAddress) addresses[0];
|
||||
|
||||
String email = address.getAddress();
|
||||
synchronized (emailContactInfo) {
|
||||
ContactInfo info = emailContactInfo.get(email);
|
||||
if (info != null && !info.isExpired())
|
||||
return info;
|
||||
}
|
||||
if (cached)
|
||||
|
||||
if (cacheOnly)
|
||||
return null;
|
||||
|
||||
try {
|
||||
Cursor cursor = null;
|
||||
ContactInfo info = new ContactInfo();
|
||||
info.email = email;
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
|
||||
== PackageManager.PERMISSION_GRANTED)
|
||||
try {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
||||
new String[]{
|
||||
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
|
||||
ContactsContract.Contacts.LOOKUP_KEY,
|
||||
ContactsContract.Contacts.DISPLAY_NAME
|
||||
},
|
||||
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
|
||||
new String[]{
|
||||
email
|
||||
}, null);
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
||||
new String[]{
|
||||
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
|
||||
ContactsContract.Contacts.LOOKUP_KEY,
|
||||
ContactsContract.Contacts.DISPLAY_NAME
|
||||
},
|
||||
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
|
||||
new String[]{
|
||||
email
|
||||
}, null);
|
||||
|
||||
if (cursor != null && cursor.moveToNext()) {
|
||||
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
|
||||
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
|
||||
int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
|
||||
if (cursor != null && cursor.moveToNext()) {
|
||||
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
|
||||
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
|
||||
int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
|
||||
|
||||
long contactId = cursor.getLong(colContactId);
|
||||
String lookupKey = cursor.getString(colLookupKey);
|
||||
Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
|
||||
long contactId = cursor.getLong(colContactId);
|
||||
String lookupKey = cursor.getString(colLookupKey);
|
||||
Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
|
||||
|
||||
ContactInfo info = new ContactInfo();
|
||||
info.is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, lookupUri);
|
||||
info.displayName = cursor.getString(colDisplayName);
|
||||
info.lookupUri = lookupUri;
|
||||
info.time = new Date().getTime();
|
||||
boolean avatars = prefs.getBoolean("avatars", true);
|
||||
if (avatars) {
|
||||
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, lookupUri);
|
||||
info.bitmap = BitmapFactory.decodeStream(is);
|
||||
}
|
||||
|
||||
synchronized (emailContactInfo) {
|
||||
emailContactInfo.put(email, info);
|
||||
info.displayName = cursor.getString(colDisplayName);
|
||||
info.lookupUri = lookupUri;
|
||||
}
|
||||
|
||||
return info;
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
|
||||
if (info.bitmap == null) {
|
||||
boolean identicons = prefs.getBoolean("identicons", false);
|
||||
if (identicons) {
|
||||
String theme = prefs.getString("theme", "light");
|
||||
int dp = Helper.dp2pixels(context, 48);
|
||||
info.bitmap = Identicon.generate(email, dp, 5, "light".equals(theme));
|
||||
}
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
|
||||
return null;
|
||||
if (info.displayName == null)
|
||||
info.displayName = address.getPersonal();
|
||||
|
||||
synchronized (emailContactInfo) {
|
||||
emailContactInfo.put(email, info);
|
||||
}
|
||||
|
||||
info.time = new Date().getTime();
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user