FairEmail/app/src/main/java/eu/faircode/email/ContactInfo.java

218 lines
7.5 KiB
Java
Raw Normal View History

2019-01-19 13:21:21 +00:00
package eu.faircode.email;
import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
2019-01-26 09:58:37 +00:00
import android.content.SharedPreferences;
2019-01-19 13:21:21 +00:00
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
2019-04-04 12:17:36 +00:00
import android.util.TypedValue;
2019-01-19 13:21:21 +00:00
import java.io.InputStream;
2019-01-25 13:13:58 +00:00
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
2019-01-19 13:21:21 +00:00
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
2019-03-15 13:54:25 +00:00
import androidx.preference.PreferenceManager;
2019-01-19 13:21:21 +00:00
public class ContactInfo {
2019-01-26 09:58:37 +00:00
private String email;
private Bitmap bitmap;
2019-01-19 13:21:21 +00:00
private String displayName;
private Uri lookupUri;
2019-01-25 13:13:58 +00:00
private long time;
2019-03-31 07:09:32 +00:00
private static Map<String, LookupInfo> emailLookupInfo = new HashMap<>();
2019-01-25 13:13:58 +00:00
private static Map<String, ContactInfo> emailContactInfo = new HashMap<>();
2019-03-31 07:09:32 +00:00
private static final long CACHE_LOOKUP_DURATION = 120 * 60 * 1000L;
private static final long CACHE_CONTACT_DURATION = 60 * 1000L;
2019-01-19 13:21:21 +00:00
2019-01-26 09:58:37 +00:00
private ContactInfo() {
2019-01-19 13:21:21 +00:00
}
2019-01-26 09:58:37 +00:00
boolean hasPhoto() {
return (bitmap != null);
2019-01-19 13:21:21 +00:00
}
Bitmap getPhotoBitmap() {
2019-01-26 09:58:37 +00:00
return bitmap;
2019-01-19 13:21:21 +00:00
}
String getDisplayName(boolean name_email) {
if (!name_email && displayName != null)
2019-01-26 09:58:37 +00:00
return displayName;
else if (displayName == null)
return (email == null ? "" : email);
else
return displayName + " <" + email + ">";
2019-01-19 13:21:21 +00:00
}
2019-01-26 09:58:37 +00:00
boolean hasLookupUri() {
return (lookupUri != null);
2019-01-19 13:21:21 +00:00
}
Uri getLookupUri() {
return lookupUri;
}
2019-01-25 13:13:58 +00:00
private boolean isExpired() {
2019-03-31 07:09:32 +00:00
return (new Date().getTime() - time > CACHE_CONTACT_DURATION);
2019-01-25 13:13:58 +00:00
}
2019-01-26 09:58:37 +00:00
static void clearCache() {
2019-03-31 07:09:32 +00:00
synchronized (emailLookupInfo) {
emailLookupInfo.clear();
}
2019-01-26 09:58:37 +00:00
synchronized (emailContactInfo) {
emailContactInfo.clear();
}
}
2019-01-19 13:21:21 +00:00
2019-01-26 09:58:37 +00:00
static ContactInfo get(Context context, Address[] addresses, boolean cacheOnly) {
if (addresses == null || addresses.length == 0)
return new ContactInfo();
InternetAddress address = (InternetAddress) addresses[0];
2019-01-25 13:13:58 +00:00
2019-02-10 20:16:16 +00:00
String key = MessageHelper.formatAddresses(new Address[]{address});
2019-01-25 13:13:58 +00:00
synchronized (emailContactInfo) {
2019-01-30 08:06:10 +00:00
ContactInfo info = emailContactInfo.get(key);
2019-01-25 13:13:58 +00:00
if (info != null && !info.isExpired())
return info;
}
2019-01-26 09:58:37 +00:00
if (cacheOnly)
2019-01-25 13:13:58 +00:00
return null;
2019-01-26 09:58:37 +00:00
ContactInfo info = new ContactInfo();
2019-01-30 08:06:10 +00:00
info.email = address.getAddress();
2019-01-26 09:58:37 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2019-02-07 09:02:40 +00:00
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS))
2019-01-21 18:51:53 +00:00
try {
2019-02-22 15:59:23 +00:00
ContentResolver resolver = context.getContentResolver();
try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME
},
2019-03-01 07:49:21 +00:00
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ? COLLATE NOCASE",
2019-02-22 15:59:23 +00:00
new String[]{
address.getAddress()
}, null)) {
2019-01-26 09:58:37 +00:00
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);
boolean avatars = prefs.getBoolean("avatars", true);
if (avatars) {
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, lookupUri);
info.bitmap = BitmapFactory.decodeStream(is);
}
info.displayName = cursor.getString(colDisplayName);
info.lookupUri = lookupUri;
2019-01-25 13:13:58 +00:00
}
2019-01-19 13:21:21 +00:00
}
2019-01-26 09:58:37 +00:00
} catch (Throwable ex) {
Log.e(ex);
}
if (info.bitmap == null) {
boolean identicons = prefs.getBoolean("identicons", false);
if (identicons) {
2019-04-04 12:17:36 +00:00
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(R.attr.themeName, tv, true);
2019-01-26 09:58:37 +00:00
int dp = Helper.dp2pixels(context, 48);
2019-04-04 12:17:36 +00:00
info.bitmap = Identicon.generate(key, dp, 5, !"light".equals(tv.string));
2019-01-19 13:21:21 +00:00
}
}
2019-01-26 09:58:37 +00:00
if (info.displayName == null)
info.displayName = address.getPersonal();
synchronized (emailContactInfo) {
2019-01-30 08:06:10 +00:00
emailContactInfo.put(key, info);
2019-01-26 09:58:37 +00:00
}
info.time = new Date().getTime();
return info;
2019-01-19 13:21:21 +00:00
}
2019-02-04 11:45:38 +00:00
2019-04-05 06:33:43 +00:00
static Uri getLookupUri(Context context, Address[] addresses) {
2019-03-31 07:09:32 +00:00
if (!Helper.hasPermission(context, Manifest.permission.READ_CONTACTS))
return null;
2019-02-04 11:45:38 +00:00
if (addresses == null || addresses.length == 0)
return null;
InternetAddress address = (InternetAddress) addresses[0];
2019-04-05 06:33:43 +00:00
synchronized (emailLookupInfo) {
LookupInfo info = emailLookupInfo.get(address.getAddress());
if (info != null && !info.isExpired())
return info.uri;
}
2019-02-04 11:45:38 +00:00
try {
2019-02-22 15:59:23 +00:00
ContentResolver resolver = context.getContentResolver();
try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.Contacts.LOOKUP_KEY
},
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
new String[]{
address.getAddress()
}, null)) {
2019-02-04 11:45:38 +00:00
2019-03-31 07:09:32 +00:00
Uri uri = null;
2019-02-04 11:45:38 +00:00
if (cursor != null && cursor.moveToNext()) {
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
long contactId = cursor.getLong(colContactId);
String lookupKey = cursor.getString(colLookupKey);
2019-03-31 07:09:32 +00:00
uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
}
LookupInfo info = new LookupInfo();
info.uri = uri;
info.time = new Date().getTime();
synchronized (emailLookupInfo) {
emailLookupInfo.put(address.getAddress(), info);
}
return info.uri;
2019-02-04 11:45:38 +00:00
}
} catch (Throwable ex) {
Log.e(ex);
return null;
}
}
2019-03-31 07:09:32 +00:00
private static class LookupInfo {
private Uri uri;
private long time;
private boolean isExpired() {
return (new Date().getTime() - time > CACHE_LOOKUP_DURATION);
}
}
2019-01-19 13:21:21 +00:00
}