Fast contact fill in, cleanup

This commit is contained in:
M66B 2020-05-06 22:30:47 +02:00
parent d77f7ebdcf
commit a6ca8630db
5 changed files with 55 additions and 61 deletions

View File

@ -827,8 +827,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
boolean outbox = EntityFolder.OUTBOX.equals(message.folderType);
boolean outgoing = isOutgoing(message);
boolean reverse = (outgoing && (viewType != ViewType.THREAD || !threading) && !show_recipients);
Address[] senders = (reverse ? message.to : message.senders);
Address[] recipients = (reverse ? message.from : message.recipients);
Address[] senders = ContactInfo.fillIn(reverse ? message.to : message.senders, prefer_contact);
Address[] recipients = ContactInfo.fillIn(reverse ? message.from : message.recipients, prefer_contact);
boolean authenticated =
!(Boolean.FALSE.equals(message.dkim) ||
Boolean.FALSE.equals(message.spf) ||
@ -1321,16 +1321,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibAvatar.setVisibility(main == null || !main.hasPhoto() ? View.GONE : View.VISIBLE);
}
Address[] _senders = fillIn(senders, map);
Address[] _recipients = fillIn(recipients, map);
if (_senders != null || _recipients != null)
if (show_recipients && recipients != null && recipients.length > 0)
tvFrom.setText(context.getString(R.string.title_from_to,
MessageHelper.formatAddresses(_senders == null ? senders : _senders, name_email, false),
MessageHelper.formatAddresses(_recipients == null ? recipients : _recipients, name_email, false)));
else
tvFrom.setText(MessageHelper.formatAddresses(_senders, name_email, false));
if (distinguish_contacts) {
boolean known = false;
if (senders != null)
@ -1347,40 +1337,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
}
private Address[] fillIn(Address[] addresses, Map<String, ContactInfo> map) {
if (addresses == null)
return null;
boolean updated = false;
List<Address> modified = new ArrayList<>();
for (Address a : addresses) {
String email = ((InternetAddress) a).getAddress();
if (TextUtils.isEmpty(email) || !map.containsKey(email))
modified.add(a);
else {
String displayName = map.get(email).getDisplayName();
if (TextUtils.isEmpty(displayName))
modified.add(a);
else {
String personal = ((InternetAddress) a).getPersonal();
if (TextUtils.isEmpty(personal) ||
(prefer_contact && !personal.equals(displayName)))
try {
modified.add(new InternetAddress(email, displayName, StandardCharsets.UTF_8.name()));
updated = true;
} catch (UnsupportedEncodingException ex) {
Log.w(ex);
modified.add(a);
}
else
modified.add(a);
}
}
}
return (updated ? modified.toArray(new Address[0]) : null);
}
private void bindExpandWarning(TupleMessageEx message, boolean expanded) {
if (viewType != ViewType.THREAD || expanded || message.content || message.uid == null || unmetered)
tvExpand.setVisibility(View.GONE);

View File

@ -31,14 +31,17 @@ import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.ContactsContract;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
@ -57,7 +60,7 @@ public class ContactInfo {
private boolean known;
private long time;
private static Map<String, Uri> emailLookup = new ConcurrentHashMap<>();
private static Map<String, Lookup> emailLookup = new ConcurrentHashMap<>();
private static final Map<String, ContactInfo> emailContactInfo = new HashMap<>();
private static final Map<String, Avatar> emailGravatar = new HashMap<>();
private static final ExecutorService executor =
@ -311,21 +314,47 @@ public class ContactInfo {
}
}
static Uri getLookupUri(Context context, Address[] addresses) {
static Uri getLookupUri(Address[] addresses) {
if (addresses == null)
return null;
for (Address from : addresses) {
String email = ((InternetAddress) from).getAddress();
if (emailLookup.containsKey(email))
return emailLookup.get(email);
return emailLookup.get(email).uri;
}
return null;
}
private static Map<String, Uri> getEmailLookup(Context context) {
Map<String, Uri> all = new ConcurrentHashMap<>();
static Address[] fillIn(Address[] addresses, boolean prefer_contact) {
if (addresses == null)
return null;
Address[] modified = new Address[addresses.length];
for (int i = 0; i < addresses.length; i++) {
InternetAddress address = (InternetAddress) addresses[i];
String email = address.getAddress();
String personal = address.getPersonal();
if (!TextUtils.isEmpty(email) && emailLookup.containsKey(email)) {
Lookup lookup = emailLookup.get(email);
if (TextUtils.isEmpty(personal) ||
(prefer_contact && !personal.equals(lookup.displayName)))
personal = lookup.displayName;
}
try {
modified[i] = new InternetAddress(email, personal, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException ex) {
Log.e(ex);
modified[i] = address;
}
}
return modified;
}
private static Map<String, Lookup> getEmailLookup(Context context) {
Map<String, Lookup> all = new ConcurrentHashMap<>();
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
Log.i("Reading email/uri");
@ -335,7 +364,8 @@ public class ContactInfo {
new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.CommonDataKinds.Email.ADDRESS
ContactsContract.CommonDataKinds.Email.ADDRESS,
ContactsContract.Contacts.DISPLAY_NAME
},
ContactsContract.CommonDataKinds.Email.ADDRESS + " <> ''",
null, null)) {
@ -343,9 +373,12 @@ public class ContactInfo {
long contactId = cursor.getLong(0);
String lookupKey = cursor.getString(1);
String email = cursor.getString(2);
String displayName = cursor.getString(3);
Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
all.put(email, uri);
Lookup lookup = new Lookup();
lookup.uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
lookup.displayName = displayName;
all.put(email, lookup);
}
} catch (Throwable ex) {
Log.e(ex);
@ -356,6 +389,11 @@ public class ContactInfo {
return all;
}
private static class Lookup {
Uri uri;
String displayName;
}
private static class Avatar {
private boolean available;
private long time;

View File

@ -1619,7 +1619,7 @@ class Core {
message.identity = (identity == null ? null : identity.id);
message.sender = MessageHelper.getSortKey(message.from);
Uri lookupUri = ContactInfo.getLookupUri(context, message.from);
Uri lookupUri = ContactInfo.getLookupUri(message.from);
message.avatar = (lookupUri == null ? null : lookupUri.toString());
// No MX check
@ -2251,7 +2251,7 @@ class Core {
message.identity = (identity == null ? null : identity.id);
message.sender = MessageHelper.getSortKey(message.from);
Uri lookupUri = ContactInfo.getLookupUri(context, message.from);
Uri lookupUri = ContactInfo.getLookupUri(message.from);
message.avatar = (lookupUri == null ? null : lookupUri.toString());
boolean check_mx = prefs.getBoolean("check_mx", false);
@ -2479,7 +2479,7 @@ class Core {
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " browsed=" + browsed);
}
Uri uri = ContactInfo.getLookupUri(context, message.from);
Uri uri = ContactInfo.getLookupUri(message.from);
if (uri != null) {
String avatar = uri.toString();
if (!Objects.equals(message.avatar, avatar)) {
@ -2656,7 +2656,7 @@ class Core {
for (Address address : addresses) {
final String email = ((InternetAddress) address).getAddress();
final String name = ((InternetAddress) address).getPersonal();
final Uri avatar = ContactInfo.getLookupUri(context, new Address[]{address});
final Uri avatar = ContactInfo.getLookupUri(new Address[]{address});
try {
db.beginTransaction();

View File

@ -414,7 +414,7 @@ public class EntityRule {
reply.received = new Date().getTime();
reply.sender = MessageHelper.getSortKey(reply.from);
Uri lookupUri = ContactInfo.getLookupUri(context, reply.from);
Uri lookupUri = ContactInfo.getLookupUri(reply.from);
reply.avatar = (lookupUri == null ? null : lookupUri.toString());
reply.id = db.message().insertMessage(reply);

View File

@ -3450,7 +3450,7 @@ public class FragmentCompose extends FragmentBase {
data.draft.from = new InternetAddress[]{new InternetAddress(selected.email, selected.name)};
data.draft.sender = MessageHelper.getSortKey(data.draft.from);
Uri lookupUri = ContactInfo.getLookupUri(context, data.draft.from);
Uri lookupUri = ContactInfo.getLookupUri(data.draft.from);
data.draft.avatar = (lookupUri == null ? null : lookupUri.toString());
data.draft.received = new Date().getTime();
@ -3935,7 +3935,7 @@ public class FragmentCompose extends FragmentBase {
draft.subject = subject;
draft.signature = signature;
draft.sender = MessageHelper.getSortKey(draft.from);
Uri lookupUri = ContactInfo.getLookupUri(context, draft.from);
Uri lookupUri = ContactInfo.getLookupUri(draft.from);
draft.avatar = (lookupUri == null ? null : lookupUri.toString());
db.message().updateMessage(draft);
}