2019-01-19 13:21:21 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2019-05-04 20:49:22 +00:00
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2019-01-19 13:21:21 +00:00
|
|
|
import android.Manifest;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
2019-01-26 09:58:37 +00:00
|
|
|
import android.content.SharedPreferences;
|
2019-05-06 17:19:37 +00:00
|
|
|
import android.database.ContentObserver;
|
2019-01-19 13:21:21 +00:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2019-04-25 19:20:20 +00:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.PorterDuffXfermode;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.RectF;
|
2019-01-19 13:21:21 +00:00
|
|
|
import android.net.Uri;
|
2019-05-06 17:19:37 +00:00
|
|
|
import android.os.Handler;
|
2019-07-24 06:52:38 +00:00
|
|
|
import android.os.Looper;
|
2019-01-19 13:21:21 +00:00
|
|
|
import android.provider.ContactsContract;
|
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
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-05-06 17:19:37 +00:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2019-07-24 06:52:38 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
2019-01-19 13:21:21 +00:00
|
|
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
|
|
|
|
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-05-06 17:19:37 +00:00
|
|
|
private static Map<String, Uri> emailLookup = new ConcurrentHashMap<>();
|
2019-01-25 13:13:58 +00:00
|
|
|
private static Map<String, ContactInfo> emailContactInfo = new HashMap<>();
|
|
|
|
|
2019-07-24 06:52:38 +00:00
|
|
|
private static final ExecutorService executor =
|
|
|
|
Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
|
|
|
|
2019-05-18 19:34:25 +00:00
|
|
|
private static final long CACHE_CONTACT_DURATION = 120 * 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
|
|
|
}
|
|
|
|
|
2019-02-08 08:27:17 +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() {
|
|
|
|
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-07-23 16:51:03 +00:00
|
|
|
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
|
|
|
|
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
|
|
|
|
},
|
|
|
|
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ? COLLATE NOCASE",
|
|
|
|
new String[]{
|
|
|
|
address.getAddress()
|
|
|
|
}, 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);
|
|
|
|
|
|
|
|
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);
|
2019-01-25 13:13:58 +00:00
|
|
|
}
|
2019-07-23 16:51:03 +00:00
|
|
|
|
|
|
|
info.displayName = cursor.getString(colDisplayName);
|
|
|
|
info.lookupUri = lookupUri;
|
2019-01-19 13:21:21 +00:00
|
|
|
}
|
2019-01-26 09:58:37 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-07-23 16:51:03 +00:00
|
|
|
}
|
2019-01-26 09:58:37 +00:00
|
|
|
|
2019-09-30 18:27:36 +00:00
|
|
|
boolean identicon = false;
|
2019-01-26 09:58:37 +00:00
|
|
|
if (info.bitmap == null) {
|
2019-10-01 19:12:30 +00:00
|
|
|
int dp = Helper.dp2pixels(context, 96);
|
2019-05-13 12:29:52 +00:00
|
|
|
boolean dark = Helper.isDarkTheme(context);
|
2019-07-22 08:40:03 +00:00
|
|
|
boolean generated = prefs.getBoolean("generated_icons", true);
|
|
|
|
if (generated) {
|
|
|
|
boolean identicons = prefs.getBoolean("identicons", false);
|
2019-09-30 18:27:36 +00:00
|
|
|
if (identicons) {
|
|
|
|
identicon = true;
|
2019-10-01 08:04:00 +00:00
|
|
|
info.bitmap = IconHelper.generateIdenticon(key, dp, 5, dark);
|
2019-09-30 18:27:36 +00:00
|
|
|
} else
|
2019-10-01 08:04:00 +00:00
|
|
|
info.bitmap = IconHelper.generateLetterIcon(key, dp, dark);
|
2019-07-22 08:40:03 +00:00
|
|
|
}
|
2019-01-19 13:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 19:20:20 +00:00
|
|
|
boolean circular = prefs.getBoolean("circular", true);
|
2019-08-12 18:13:20 +00:00
|
|
|
if (info.bitmap != null) {
|
2019-04-25 19:20:20 +00:00
|
|
|
int w = info.bitmap.getWidth();
|
|
|
|
int h = info.bitmap.getHeight();
|
|
|
|
|
|
|
|
Rect source;
|
|
|
|
if (w > h) {
|
|
|
|
int off = (w - h) / 2;
|
|
|
|
source = new Rect(off, 0, w - off, h);
|
|
|
|
} else if (w < h) {
|
|
|
|
int off = (h - w) / 2;
|
|
|
|
source = new Rect(0, off, w, h - off);
|
|
|
|
} else
|
|
|
|
source = new Rect(0, 0, w, h);
|
|
|
|
|
|
|
|
Rect dest = new Rect(0, 0, source.width(), source.height());
|
|
|
|
|
|
|
|
Bitmap round = Bitmap.createBitmap(source.width(), source.height(), Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas canvas = new Canvas(round);
|
|
|
|
|
|
|
|
Paint paint = new Paint();
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas.drawARGB(0, 0, 0, 0);
|
|
|
|
paint.setColor(Color.GRAY);
|
2019-09-30 18:27:36 +00:00
|
|
|
if (circular && !identicon)
|
2019-08-12 18:13:20 +00:00
|
|
|
canvas.drawOval(new RectF(dest), paint);
|
|
|
|
else {
|
|
|
|
float radius = Helper.dp2pixels(context, 3);
|
|
|
|
canvas.drawRoundRect(new RectF(dest), radius, radius, paint);
|
|
|
|
}
|
2019-04-25 19:20:20 +00:00
|
|
|
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
|
|
|
canvas.drawBitmap(info.bitmap, source, dest, paint);
|
|
|
|
|
|
|
|
info.bitmap.recycle();
|
|
|
|
info.bitmap = round;
|
|
|
|
}
|
|
|
|
|
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-07-24 06:52:38 +00:00
|
|
|
static void init(final Context context) {
|
|
|
|
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
|
|
|
|
Handler handler = new Handler(Looper.getMainLooper());
|
|
|
|
|
|
|
|
ContentObserver observer = new ContentObserver(handler) {
|
|
|
|
@Override
|
|
|
|
public void onChange(boolean selfChange, Uri uri) {
|
|
|
|
Log.i("Contact changed uri=" + uri);
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
emailLookup = getEmailLookup(context);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-07-23 16:51:03 +00:00
|
|
|
}
|
2019-07-24 06:52:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
emailLookup = getEmailLookup(context);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2019-07-16 10:01:57 +00:00
|
|
|
}
|
2019-07-24 06:52:38 +00:00
|
|
|
}
|
|
|
|
});
|
2019-07-23 16:51:03 +00:00
|
|
|
|
2019-07-24 06:52:38 +00:00
|
|
|
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
|
|
|
|
Log.i("Observing uri=" + uri);
|
|
|
|
context.getContentResolver().registerContentObserver(uri, true, observer);
|
|
|
|
}
|
2019-05-06 17:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Uri getLookupUri(Context context, Address[] addresses) {
|
|
|
|
if (addresses == null)
|
2019-02-04 11:45:38 +00:00
|
|
|
return null;
|
|
|
|
|
2019-05-06 17:19:37 +00:00
|
|
|
for (Address from : addresses) {
|
|
|
|
String email = ((InternetAddress) from).getAddress();
|
|
|
|
if (emailLookup.containsKey(email))
|
|
|
|
return emailLookup.get(email);
|
2019-04-05 06:33:43 +00:00
|
|
|
}
|
2019-02-04 11:45:38 +00:00
|
|
|
|
2019-05-06 17:19:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Map<String, Uri> getEmailLookup(Context context) {
|
|
|
|
Map<String, Uri> all = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
|
2019-07-24 06:52:38 +00:00
|
|
|
Log.i("Reading email/uri");
|
2019-02-22 15:59:23 +00:00
|
|
|
ContentResolver resolver = context.getContentResolver();
|
2019-07-21 08:31:34 +00:00
|
|
|
|
2019-02-22 15:59:23 +00:00
|
|
|
try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
|
|
|
new String[]{
|
|
|
|
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
|
2019-05-06 17:19:37 +00:00
|
|
|
ContactsContract.Contacts.LOOKUP_KEY,
|
|
|
|
ContactsContract.CommonDataKinds.Email.ADDRESS
|
2019-02-22 15:59:23 +00:00
|
|
|
},
|
2019-05-06 17:19:37 +00:00
|
|
|
ContactsContract.CommonDataKinds.Email.ADDRESS + " <> ''",
|
|
|
|
null, null)) {
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
long contactId = cursor.getLong(0);
|
|
|
|
String lookupKey = cursor.getString(1);
|
|
|
|
String email = cursor.getString(2);
|
|
|
|
|
|
|
|
Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
|
|
|
|
all.put(email, uri);
|
2019-03-31 07:09:32 +00:00
|
|
|
}
|
2019-07-23 16:51:03 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2019-02-04 11:45:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-31 07:09:32 +00:00
|
|
|
|
2019-05-06 17:19:37 +00:00
|
|
|
Log.i("Read email/uri=" + all.size());
|
|
|
|
return all;
|
2019-03-31 07:09:32 +00:00
|
|
|
}
|
2019-05-06 17:19:37 +00:00
|
|
|
}
|