Improved avatar/identicon caching

This commit is contained in:
M66B 2019-01-26 09:58:37 +00:00
parent 43b63af4b9
commit 2b6a426012
4 changed files with 118 additions and 145 deletions

View File

@ -32,7 +32,6 @@ import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
@ -75,7 +74,6 @@ import org.xml.sax.XMLReader;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.Collator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@ -130,18 +128,15 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private boolean threading;
private boolean contacts;
private boolean avatars;
private boolean identicons;
private boolean preview;
private boolean confirm;
private boolean debug;
private int dp24;
private float textSize;
private int colorPrimary;
private int colorAccent;
private int textColorSecondary;
private int colorUnread;
private String theme;
private boolean hasWebView;
private SelectionTracker<Long> selectionTracker = null;
@ -484,82 +479,58 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
boolean outgoing = (viewType != ViewType.THREAD && EntityFolder.isOutgoing(message.folderType));
if (avatars || identicons) {
final Address[] addresses = (outgoing ? message.to : message.from);
ContactInfo info = ContactInfo.get(context, addresses, true);
if (info == null) {
Bundle aargs = new Bundle();
aargs.putLong("id", message.id);
aargs.putSerializable("addresses", outgoing ? message.to : message.from);
aargs.putSerializable("addresses", addresses);
new SimpleTask<ContactInfo>() {
@Override
protected void onPreExecute(Bundle args) {
ivAvatar.setTag(message.id);
ivAvatar.setVisibility(View.INVISIBLE);
tvFrom.setTag(message.id);
Address[] addresses = (Address[]) args.getSerializable("addresses");
ContactInfo info = ContactInfo.get(context, addresses, true);
if (info != null && info.hasDisplayName())
setFrom(info, addresses);
else
tvFrom.setText(MessageHelper.formatAddresses(addresses, !compact, false));
ivAvatar.setVisibility(avatars ? View.INVISIBLE : View.GONE);
tvFrom.setText(MessageHelper.formatAddresses(addresses, !compact, false));
}
@Override
protected ContactInfo onExecute(Context context, Bundle args) {
Address[] addresses = (Address[]) args.getSerializable("addresses");
ContactInfo info = ContactInfo.get(context, addresses, false);
if ((info == null || !info.hasPhoto()) &&
identicons && addresses != null && addresses.length > 0) {
Drawable ident = new BitmapDrawable(
context.getResources(),
Identicon.generate(addresses[0].toString(),
dp24, 5, "light".equals(theme)));
info = new ContactInfo(ident, (info == null ? null : info.getDisplayName()));
}
return info;
return ContactInfo.get(context, addresses, false);
}
@Override
protected void onExecuted(Bundle args, ContactInfo info) {
long id = args.getLong("id");
Long id = args.getLong("id");
if ((long) ivAvatar.getTag() == id) {
if (info == null || !info.hasPhoto())
ivAvatar.setImageResource(R.drawable.baseline_person_24);
if (id.equals(ivAvatar.getTag())) {
if (info.hasPhoto())
ivAvatar.setImageBitmap(info.getPhotoBitmap());
else
ivAvatar.setImageDrawable(info.getPhotoDrawable());
ivAvatar.setVisibility(View.VISIBLE);
ivAvatar.setImageResource(R.drawable.baseline_person_24);
ivAvatar.setVisibility(avatars ? View.VISIBLE : View.GONE);
}
if ((long) tvFrom.getTag() == id) {
if (info != null && info.hasDisplayName()) {
Address[] addresses = (Address[]) args.getSerializable("addresses");
setFrom(info, addresses);
}
}
if (id.equals(tvFrom.getTag()))
tvFrom.setText(info.getDisplayName(compact));
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(context, owner, ex);
}
private void setFrom(ContactInfo info, Address[] addresses) {
try {
((InternetAddress) addresses[0]).setPersonal(info.getDisplayName());
tvFrom.setText(MessageHelper.formatAddresses(addresses, !compact, false));
} catch (UnsupportedEncodingException ex) {
Log.w(ex);
}
}
}.execute(context, owner, aargs, "message:avatar");
} else {
ivAvatar.setVisibility(View.GONE);
tvFrom.setText(MessageHelper.formatAddresses(outgoing ? message.to : message.from, !compact, false));
if (info.hasPhoto())
ivAvatar.setImageBitmap(info.getPhotoBitmap());
else
ivAvatar.setImageResource(R.drawable.baseline_person_24);
ivAvatar.setVisibility(avatars ? View.VISIBLE : View.GONE);
tvFrom.setText(info.getDisplayName(compact));
}
vwColor.setBackgroundColor(message.accountColor == null ? Color.TRANSPARENT : message.accountColor);
@ -2174,19 +2145,17 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
this.threading = prefs.getBoolean("threading", true);
this.contacts = (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED);
this.avatars = prefs.getBoolean("avatars", true);
this.identicons = prefs.getBoolean("identicons", false);
this.avatars = (prefs.getBoolean("avatars", true) ||
prefs.getBoolean("identicons", false));
this.preview = prefs.getBoolean("preview", false);
this.confirm = prefs.getBoolean("confirm", false);
this.debug = prefs.getBoolean("debug", false);
this.dp24 = Helper.dp2pixels(context, 24);
this.textSize = Helper.getTextSize(context, zoom);
this.colorPrimary = Helper.resolveColor(context, R.attr.colorPrimary);
this.colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
this.textColorSecondary = Helper.resolveColor(context, android.R.attr.textColorSecondary);
this.colorUnread = Helper.resolveColor(context, R.attr.colorUnread);
this.theme = prefs.getString("theme", "light");
PackageManager pm = context.getPackageManager();
this.hasWebView = pm.hasSystemFeature("android.software.webview");

View File

@ -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;
}
}

View File

@ -240,6 +240,7 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("avatars", checked).apply();
ContactInfo.clearCache();
}
});
@ -247,6 +248,7 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("identicons", checked).apply();
ContactInfo.clearCache();
}
});

View File

@ -488,14 +488,9 @@ public class ServiceSynchronize extends LifecycleService {
// Get contact info
Map<TupleMessageEx, ContactInfo> messageContact = new HashMap<>();
for (TupleMessageEx message : messages) {
ContactInfo info = ContactInfo.get(this, message.from, true);
if (info == null)
info = ContactInfo.get(this, message.from, false);
if (info == null)
info = new ContactInfo(MessageHelper.formatAddressesShort(message.from));
messageContact.put(message, info);
}
for (TupleMessageEx message : messages)
messageContact.put(message,
ContactInfo.get(this, message.from, false));
// Build pending intent
Intent view = new Intent(this, ActivityView.class);
@ -574,7 +569,7 @@ public class ServiceSynchronize extends LifecycleService {
DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
StringBuilder sb = new StringBuilder();
for (EntityMessage message : messages) {
sb.append("<strong>").append(messageContact.get(message).getDisplayName()).append("</strong>");
sb.append("<strong>").append(messageContact.get(message).getDisplayName(true)).append("</strong>");
if (!TextUtils.isEmpty(message.subject))
sb.append(": ").append(message.subject);
sb.append(" ").append(df.format(message.received));
@ -647,7 +642,7 @@ public class ServiceSynchronize extends LifecycleService {
mbuilder
.addExtras(args)
.setSmallIcon(R.drawable.baseline_email_white_24)
.setContentTitle(info.getDisplayName())
.setContentTitle(info.getDisplayName(true))
.setSubText(message.accountName + " · " + folderName)
.setContentIntent(piContent)
.setWhen(message.received)