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

662 lines
26 KiB
Java
Raw Normal View History

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/>.
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2019-05-04 20:49:22 +00:00
*/
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;
2020-07-01 09:05:18 +00:00
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
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;
2020-05-06 20:30:47 +00:00
import android.text.TextUtils;
2019-01-19 13:21:21 +00:00
2020-02-09 11:14:11 +00:00
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
2020-06-30 09:19:17 +00:00
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.BufferedOutputStream;
import java.io.File;
2020-06-30 12:04:50 +00:00
import java.io.FileNotFoundException;
2020-06-30 09:19:17 +00:00
import java.io.FileOutputStream;
2020-01-17 15:59:29 +00:00
import java.io.IOException;
2019-01-19 13:21:21 +00:00
import java.io.InputStream;
2020-06-30 09:19:17 +00:00
import java.io.OutputStream;
2020-05-06 20:30:47 +00:00
import java.io.UnsupportedEncodingException;
2020-06-30 12:04:50 +00:00
import java.net.ConnectException;
2020-01-17 15:59:29 +00:00
import java.net.HttpURLConnection;
2020-07-01 09:05:18 +00:00
import java.net.SocketTimeoutException;
2020-01-17 15:59:29 +00:00
import java.net.URL;
2020-06-30 12:04:50 +00:00
import java.net.UnknownHostException;
2020-05-06 20:30:47 +00:00
import java.nio.charset.StandardCharsets;
2020-07-01 12:28:13 +00:00
import java.security.cert.CertPathValidatorException;
2020-06-30 12:04:50 +00:00
import java.security.cert.CertificateException;
2020-07-01 08:49:09 +00:00
import java.util.ArrayList;
2019-01-25 13:13:58 +00:00
import java.util.Date;
import java.util.HashMap;
2020-07-01 08:49:09 +00:00
import java.util.List;
2020-02-15 14:53:11 +00:00
import java.util.Locale;
2019-01-25 13:13:58 +00:00
import java.util.Map;
2020-07-01 08:49:09 +00:00
import java.util.concurrent.Callable;
2019-05-06 17:19:37 +00:00
import java.util.concurrent.ConcurrentHashMap;
2020-07-01 08:49:09 +00:00
import java.util.concurrent.ExecutionException;
2019-07-24 06:52:38 +00:00
import java.util.concurrent.ExecutorService;
2020-07-01 08:49:09 +00:00
import java.util.concurrent.Future;
2019-01-19 13:21:21 +00:00
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
2020-06-30 09:19:17 +00:00
import javax.net.ssl.HttpsURLConnection;
2020-06-30 12:04:50 +00:00
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLProtocolException;
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-10-02 11:36:07 +00:00
private boolean known;
2019-01-25 13:13:58 +00:00
private long time;
2020-05-06 20:30:47 +00:00
private static Map<String, Lookup> emailLookup = new ConcurrentHashMap<>();
2019-10-02 11:36:07 +00:00
private static final Map<String, ContactInfo> emailContactInfo = new HashMap<>();
2020-01-26 08:13:42 +00:00
private static final Map<String, Avatar> emailGravatar = new HashMap<>();
2020-06-30 09:19:17 +00:00
2020-07-01 08:49:09 +00:00
private static final ExecutorService executorLookup =
2019-10-10 11:26:44 +00:00
Helper.getBackgroundExecutor(1, "contact");
2019-07-24 06:52:38 +00:00
2020-07-01 08:49:09 +00:00
private static final ExecutorService executorFavicon =
Helper.getBackgroundExecutor(0, "favicon");
2020-01-21 10:12:28 +00:00
private static final int GRAVATAR_TIMEOUT = 5 * 1000; // milliseconds
2020-06-30 12:04:50 +00:00
private static final int FAVICON_CONNECT_TIMEOUT = 5 * 1000; // milliseconds
private static final int FAVICON_READ_TIMEOUT = 10 * 1000; // milliseconds
2020-06-30 12:29:50 +00:00
private static final int FAVICON_READ_BYTES = 2048;
2020-01-21 10:12:28 +00:00
private static final long CACHE_CONTACT_DURATION = 2 * 60 * 1000L; // milliseconds
2020-01-23 15:47:10 +00:00
private static final long CACHE_GRAVATAR_DURATION = 2 * 60 * 60 * 1000L; // milliseconds
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 getEmailAddress() {
return email;
}
2019-12-19 20:37:09 +00:00
String getDisplayName() {
return displayName;
}
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-10-02 11:36:07 +00:00
boolean isKnown() {
return known;
}
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
}
2020-06-30 10:51:16 +00:00
static void clearCache(Context context) {
2019-01-26 09:58:37 +00:00
synchronized (emailContactInfo) {
emailContactInfo.clear();
}
2020-06-30 10:51:16 +00:00
final File dir = new File(context.getCacheDir(), "favicons");
2020-07-01 08:49:09 +00:00
executorFavicon.submit(new Runnable() {
2020-06-30 10:51:16 +00:00
@Override
public void run() {
try {
File[] favicons = dir.listFiles();
if (favicons != null)
for (File favicon : favicons)
favicon.delete();
} catch (Throwable ex) {
Log.w(ex);
}
}
});
2019-01-26 09:58:37 +00:00
}
2019-01-19 13:21:21 +00:00
2020-02-09 11:14:11 +00:00
@NonNull
2020-07-05 06:59:10 +00:00
static ContactInfo[] get(Context context, long account, String folderType, Address[] addresses) {
return get(context, account, folderType, addresses, false);
2020-02-09 11:14:11 +00:00
}
2020-07-05 06:59:10 +00:00
static ContactInfo[] getCached(Context context, long account, String folderType, Address[] addresses) {
return get(context, account, folderType, addresses, true);
2020-02-09 11:14:11 +00:00
}
2020-07-05 06:59:10 +00:00
private static ContactInfo[] get(Context context, long account, String folderType, Address[] addresses, boolean cacheOnly) {
2019-01-26 09:58:37 +00:00
if (addresses == null || addresses.length == 0)
2020-02-09 11:14:11 +00:00
return new ContactInfo[]{new ContactInfo()};
ContactInfo[] result = new ContactInfo[addresses.length];
for (int i = 0; i < addresses.length; i++) {
2020-07-05 06:59:10 +00:00
result[i] = _get(context, account, folderType, (InternetAddress) addresses[i], cacheOnly);
2020-02-09 11:14:11 +00:00
if (result[i] == null)
return null;
}
return result;
}
2019-01-25 13:13:58 +00:00
2020-07-05 06:59:10 +00:00
private static ContactInfo _get(Context context, long account, String folderType, InternetAddress address, boolean cacheOnly) {
2019-10-14 10:22:07 +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);
2020-03-23 06:42:31 +00:00
boolean avatars = prefs.getBoolean("avatars", true);
boolean gravatars = prefs.getBoolean("gravatars", false);
2020-06-30 09:19:17 +00:00
boolean favicons = prefs.getBoolean("favicons", false);
2020-03-23 06:42:31 +00:00
boolean generated = prefs.getBoolean("generated_icons", true);
boolean identicons = prefs.getBoolean("identicons", false);
boolean circular = prefs.getBoolean("circular", true);
2019-01-26 09:58:37 +00:00
2020-06-30 09:19:17 +00:00
// Contact photo
2020-06-25 12:32:04 +00:00
if (!TextUtils.isEmpty(info.email) &&
Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
2019-07-23 16:51:03 +00:00
ContentResolver resolver = context.getContentResolver();
2019-10-23 07:35:55 +00:00
Uri uri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI,
2020-06-25 12:32:04 +00:00
Uri.encode(info.email.toLowerCase(Locale.ROOT)));
2019-10-23 07:35:55 +00:00
try (Cursor cursor = resolver.query(uri,
2019-07-23 16:51:03 +00:00
new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME
},
2019-10-23 07:35:55 +00:00
null, null, null)) {
2019-07-23 16:51:03 +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);
2020-04-15 16:01:45 +00:00
if (avatars)
try (InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
resolver, lookupUri, false)) {
info.bitmap = BitmapFactory.decodeStream(is);
} catch (Throwable ex) {
Log.e(ex);
}
2019-07-23 16:51:03 +00:00
info.displayName = cursor.getString(colDisplayName);
info.lookupUri = lookupUri;
2019-10-02 11:36:07 +00:00
info.known = true;
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
2020-06-30 09:19:17 +00:00
// Gravatar
2020-06-30 10:28:31 +00:00
if (info.bitmap == null && gravatars) {
if (!TextUtils.isEmpty(info.email)) {
2020-06-25 12:32:04 +00:00
String gkey = info.email.toLowerCase(Locale.ROOT);
2020-01-23 15:47:10 +00:00
boolean lookup;
synchronized (emailGravatar) {
2020-04-30 11:59:55 +00:00
Avatar avatar = emailGravatar.get(gkey);
2020-01-26 08:13:42 +00:00
lookup = (avatar == null || avatar.isExpired() || avatar.isAvailable());
2020-01-23 15:47:10 +00:00
}
if (lookup) {
HttpURLConnection urlConnection = null;
try {
2020-04-30 11:59:55 +00:00
String hash = Helper.md5(gkey.getBytes());
2020-01-23 15:47:10 +00:00
URL url = new URL("https://www.gravatar.com/avatar/" + hash + "?d=404");
2020-04-30 11:59:55 +00:00
Log.i("Gravatar key=" + gkey + " url=" + url);
2020-01-23 15:47:10 +00:00
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.setReadTimeout(GRAVATAR_TIMEOUT);
urlConnection.setConnectTimeout(GRAVATAR_TIMEOUT);
urlConnection.connect();
int status = urlConnection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
info.bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream());
2020-01-26 08:13:42 +00:00
// Positive reply
2020-01-23 15:47:10 +00:00
synchronized (emailGravatar) {
2020-04-30 11:59:55 +00:00
emailGravatar.put(gkey, new Avatar(true));
2020-01-23 15:47:10 +00:00
}
} else if (status == HttpURLConnection.HTTP_NOT_FOUND) {
2020-01-26 08:13:42 +00:00
// Negative reply
2020-01-23 15:47:10 +00:00
synchronized (emailGravatar) {
2020-04-30 11:59:55 +00:00
emailGravatar.put(gkey, new Avatar(false));
2020-01-23 15:47:10 +00:00
}
} else
throw new IOException("HTTP status=" + status);
} catch (Throwable ex) {
Log.w(ex);
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
2020-01-17 15:59:29 +00:00
}
}
}
2020-06-30 09:19:17 +00:00
// Favicon
2020-07-05 06:59:10 +00:00
if (info.bitmap == null && favicons &&
!EntityFolder.JUNK.equals(folderType)) {
2020-06-30 09:19:17 +00:00
int at = (info.email == null ? -1 : info.email.indexOf('@'));
String domain = (at < 0 ? null : info.email.substring(at + 1).toLowerCase(Locale.ROOT));
2020-06-30 10:28:31 +00:00
if (domain != null) {
2020-06-30 12:29:50 +00:00
if ("gmail.com".equals(domain) || "googlemail.com".equals(domain))
2020-06-30 10:35:38 +00:00
domain = "google.com";
2020-06-30 10:51:16 +00:00
File dir = new File(context.getCacheDir(), "favicons");
if (!dir.exists())
dir.mkdir();
File file = new File(dir, domain);
2020-06-30 09:19:17 +00:00
try {
2020-06-30 10:28:31 +00:00
// check cache
2020-06-30 09:19:17 +00:00
if (file.exists())
2020-06-30 10:51:16 +00:00
if (file.length() == 0)
Log.i("Favicon blacklisted domain=" + domain);
else
info.bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
2020-06-30 09:19:17 +00:00
else {
2020-07-01 08:49:09 +00:00
final URL base = new URL("https://" + domain);
final URL www = new URL("https://www." + domain);
2020-07-01 07:59:48 +00:00
2020-07-01 08:49:09 +00:00
List<Future<Bitmap>> futures = new ArrayList<>();
2020-06-30 12:04:50 +00:00
2020-07-01 08:49:09 +00:00
futures.add(executorFavicon.submit(new Callable<Bitmap>() {
@Override
public Bitmap call() throws Exception {
return parseFavicon(base);
2020-06-30 12:29:50 +00:00
}
2020-07-01 08:49:09 +00:00
}));
2020-06-30 12:29:50 +00:00
2020-07-01 08:49:09 +00:00
futures.add(executorFavicon.submit(new Callable<Bitmap>() {
@Override
public Bitmap call() throws Exception {
return parseFavicon(www);
2020-06-30 12:29:50 +00:00
}
2020-07-01 08:49:09 +00:00
}));
2020-07-01 07:59:48 +00:00
2020-07-01 08:49:09 +00:00
futures.add(executorFavicon.submit(new Callable<Bitmap>() {
@Override
public Bitmap call() throws Exception {
return getFavicon(new URL(base, "favicon.ico"));
}
}));
futures.add(executorFavicon.submit(new Callable<Bitmap>() {
@Override
public Bitmap call() throws Exception {
return getFavicon(new URL(www, "favicon.ico"));
}
}));
Throwable ex = null;
for (Future<Bitmap> future : futures)
2020-07-01 07:59:48 +00:00
try {
2020-07-01 08:49:09 +00:00
info.bitmap = future.get();
2020-07-01 11:04:19 +00:00
if (info.bitmap != null)
break;
2020-07-01 08:49:09 +00:00
} catch (ExecutionException exex) {
ex = exex.getCause();
} catch (Throwable exex) {
ex = exex;
2020-07-01 07:59:48 +00:00
}
2020-06-30 09:19:17 +00:00
2020-06-30 12:29:50 +00:00
if (info.bitmap == null)
2020-07-01 08:49:09 +00:00
throw ex;
// Add to cache
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
info.bitmap.compress(Bitmap.CompressFormat.PNG, 90, os);
}
2020-06-30 09:19:17 +00:00
}
} catch (Throwable ex) {
2020-07-06 15:39:21 +00:00
if (isRecoverable(ex, context))
Log.i(ex);
else {
Log.w(ex);
2020-06-30 10:51:16 +00:00
try {
file.createNewFile();
2020-06-30 12:04:50 +00:00
} catch (IOException ex1) {
Log.e(ex1);
2020-06-30 09:19:17 +00:00
}
2020-07-06 15:39:21 +00:00
}
2020-06-30 09:19:17 +00:00
}
}
}
// Generated
2019-09-30 18:27:36 +00:00
boolean identicon = false;
2020-06-30 10:28:31 +00:00
if (info.bitmap == null && generated) {
2019-10-01 19:12:30 +00:00
int dp = Helper.dp2pixels(context, 96);
2020-06-30 10:28:31 +00:00
if (!TextUtils.isEmpty(info.email)) {
2019-09-30 18:27:36 +00:00
if (identicons) {
identicon = true;
2019-10-13 11:54:18 +00:00
info.bitmap = ImageHelper.generateIdenticon(
2020-06-25 12:32:04 +00:00
info.email, dp, 5, context);
2019-09-30 18:27:36 +00:00
} else
2019-10-13 11:54:18 +00:00
info.bitmap = ImageHelper.generateLetterIcon(
2020-06-25 12:32:04 +00:00
info.email, address.getPersonal(), dp, context);
}
2019-01-19 13:21:21 +00:00
}
info.bitmap = ImageHelper.makeCircular(info.bitmap,
circular && !identicon ? null : Helper.dp2pixels(context, 3));
2019-04-25 19:20:20 +00:00
2019-01-26 09:58:37 +00:00
if (info.displayName == null)
info.displayName = address.getPersonal();
2020-06-25 12:32:04 +00:00
if (!info.known && !TextUtils.isEmpty(info.email)) {
2019-10-02 11:36:07 +00:00
DB db = DB.getInstance(context);
EntityContact contact = db.contact().getContact(account, EntityContact.TYPE_TO, info.email);
info.known = (contact != null);
}
2019-01-26 09:58:37 +00:00
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
2020-06-30 12:04:50 +00:00
private static Bitmap parseFavicon(URL base) throws IOException {
Log.i("GET favicon " + base);
HttpsURLConnection connection = (HttpsURLConnection) base.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(FAVICON_READ_TIMEOUT);
connection.setConnectTimeout(FAVICON_CONNECT_TIMEOUT);
connection.setInstanceFollowRedirects(true);
connection.connect();
2020-06-30 10:28:31 +00:00
2020-06-30 12:04:50 +00:00
String response;
try {
2020-06-30 12:29:50 +00:00
byte[] buffer = new byte[FAVICON_READ_BYTES];
int len = connection.getInputStream().read(buffer);
if (len < 0)
throw new IOException("length");
response = new String(buffer, 0, len, StandardCharsets.UTF_8.name());
2020-06-30 12:04:50 +00:00
} finally {
connection.disconnect();
}
2020-06-30 10:28:31 +00:00
2020-06-30 12:04:50 +00:00
Document doc = JsoupEx.parse(response);
2020-06-30 10:28:31 +00:00
2020-07-06 13:46:10 +00:00
Element link = doc.head().select("link[href~=.*\\.(ico|png|gif|svg)]").first();
2020-06-30 12:04:50 +00:00
String favicon = (link == null ? null : link.attr("href"));
2020-06-30 10:28:31 +00:00
2020-06-30 12:04:50 +00:00
if (TextUtils.isEmpty(favicon)) {
Element meta = doc.head().select("meta[itemprop=image]").first();
favicon = (meta == null ? null : meta.attr("content"));
}
2020-06-30 10:28:31 +00:00
2020-07-04 07:08:32 +00:00
if (!TextUtils.isEmpty(favicon))
return getFavicon(new URL(base, favicon));
2020-06-30 12:04:50 +00:00
return null;
2020-06-30 09:19:17 +00:00
}
2020-07-01 08:49:09 +00:00
@NonNull
2020-06-30 12:04:50 +00:00
private static Bitmap getFavicon(URL url) throws IOException {
2020-06-30 10:28:31 +00:00
Log.i("GET favicon " + url);
2020-07-04 07:08:32 +00:00
if (!"https".equals(url.getProtocol()))
throw new FileNotFoundException("http");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
2020-06-30 10:28:31 +00:00
connection.setRequestMethod("GET");
2020-06-30 12:04:50 +00:00
connection.setReadTimeout(FAVICON_READ_TIMEOUT);
connection.setConnectTimeout(FAVICON_CONNECT_TIMEOUT);
2020-06-30 10:28:31 +00:00
connection.setInstanceFollowRedirects(true);
connection.connect();
try {
2020-07-01 08:49:09 +00:00
Bitmap bitmap = BitmapFactory.decodeStream(connection.getInputStream());
if (bitmap == null)
throw new FileNotFoundException("decodeStream");
else
return bitmap;
2020-06-30 10:28:31 +00:00
} finally {
connection.disconnect();
}
}
2020-07-01 09:05:18 +00:00
private static boolean isRecoverable(Throwable ex, Context context) {
if (ex instanceof SocketTimeoutException) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = (cm == null ? null : cm.getActiveNetworkInfo());
return (ni == null || !ni.isConnected());
}
2020-06-30 12:29:50 +00:00
return !(ex instanceof ConnectException ||
(ex instanceof UnknownHostException &&
ex.getMessage() != null &&
ex.getMessage().contains("No address associated with hostname")) ||
2020-06-30 12:04:50 +00:00
ex instanceof FileNotFoundException ||
ex instanceof SSLPeerUnverifiedException ||
(ex instanceof SSLException &&
"Unable to parse TLS packet header".equals(ex.getMessage())) ||
(ex instanceof SSLHandshakeException &&
2020-07-01 12:28:13 +00:00
("connection closed".equals(ex.getMessage())) ||
"Connection closed by peer".equals(ex.getMessage())) ||
2020-07-01 17:44:01 +00:00
(ex instanceof SSLHandshakeException &&
ex.getMessage() != null &&
2020-07-19 06:01:14 +00:00
(ex.getMessage().contains("usually a protocol error") ||
ex.getMessage().contains("Unacceptable certificate"))) ||
2020-06-30 12:04:50 +00:00
(ex instanceof SSLHandshakeException &&
2020-07-01 12:28:13 +00:00
(ex.getCause() instanceof SSLProtocolException ||
ex.getCause() instanceof CertificateException ||
ex.getCause() instanceof CertPathValidatorException)));
2020-06-30 12:04:50 +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);
2020-07-01 08:49:09 +00:00
executorLookup.submit(new Runnable() {
2019-07-24 06:52:38 +00:00
@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
});
}
};
2020-07-01 08:49:09 +00:00
executorLookup.submit(new Runnable() {
2019-07-24 06:52:38 +00:00
@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
2020-06-28 07:50:49 +00:00
try {
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
Log.i("Observing uri=" + uri);
context.getContentResolver().registerContentObserver(uri, true, observer);
} catch (SecurityException ex) {
Log.w(ex);
/*
Should never happen, but:
Caused by: android.os.RemoteException:
at com.android.server.content.ContentService.registerContentObserver (ContentService.java:340)
at android.content.IContentService$Stub.onTransact (IContentService.java:76)
at com.android.server.content.ContentService.onTransact (ContentService.java:262)
at android.os.Binder.execTransact (Binder.java:731)
*/
}
2019-07-24 06:52:38 +00:00
}
2019-05-06 17:19:37 +00:00
}
2020-05-06 20:30:47 +00:00
static Uri getLookupUri(Address[] addresses) {
2019-05-06 17:19:37 +00:00
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();
2020-06-25 12:32:04 +00:00
if (!TextUtils.isEmpty(email)) {
Lookup lookup = emailLookup.get(email.toLowerCase(Locale.ROOT));
if (lookup != null)
return lookup.uri;
}
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;
}
2020-05-06 20:30:47 +00:00
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();
2020-06-25 12:32:04 +00:00
if (!TextUtils.isEmpty(email)) {
Lookup lookup = emailLookup.get(email.toLowerCase(Locale.ROOT));
if (lookup != null &&
(TextUtils.isEmpty(personal) || prefer_contact))
2020-05-06 20:30:47 +00:00
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<>();
2019-05-06 17:19:37 +00:00
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,
2020-05-06 20:30:47 +00:00
ContactsContract.CommonDataKinds.Email.ADDRESS,
ContactsContract.Contacts.DISPLAY_NAME
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);
2020-05-06 20:30:47 +00:00
String displayName = cursor.getString(3);
2019-05-06 17:19:37 +00:00
2020-05-06 20:30:47 +00:00
Lookup lookup = new Lookup();
lookup.uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
lookup.displayName = displayName;
2020-06-25 12:32:04 +00:00
all.put(email.toLowerCase(Locale.ROOT), lookup);
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
}
2020-01-23 15:47:10 +00:00
2020-05-06 20:30:47 +00:00
private static class Lookup {
Uri uri;
String displayName;
}
2020-01-26 08:13:42 +00:00
private static class Avatar {
2020-01-23 15:47:10 +00:00
private boolean available;
private long time;
2020-01-26 08:13:42 +00:00
Avatar(boolean available) {
2020-01-23 15:47:10 +00:00
this.available = available;
this.time = new Date().getTime();
}
boolean isAvailable() {
return available;
}
boolean isExpired() {
return (new Date().getTime() - time > CACHE_GRAVATAR_DURATION);
}
}
2019-05-06 17:19:37 +00:00
}