mirror of https://github.com/M66B/FairEmail.git
Read contacts in background
This commit is contained in:
parent
6104ecfadb
commit
00400d7ef6
|
@ -105,7 +105,7 @@ public class ApplicationEx extends Application {
|
|||
CookieManager.getInstance().setAcceptCookie(false);
|
||||
|
||||
MessageHelper.setSystemProperties();
|
||||
ContactInfo.init(this, new Handler());
|
||||
ContactInfo.init(this);
|
||||
|
||||
try {
|
||||
WorkerWatchdog.init(this);
|
||||
|
|
|
@ -36,6 +36,7 @@ import android.graphics.Rect;
|
|||
import android.graphics.RectF;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.ContactsContract;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
@ -45,6 +46,8 @@ import java.util.Date;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import javax.mail.Address;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
|
@ -59,6 +62,9 @@ public class ContactInfo {
|
|||
private static Map<String, Uri> emailLookup = new ConcurrentHashMap<>();
|
||||
private static Map<String, ContactInfo> emailContactInfo = new HashMap<>();
|
||||
|
||||
private static final ExecutorService executor =
|
||||
Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
||||
|
||||
private static final long CACHE_CONTACT_DURATION = 120 * 1000L;
|
||||
|
||||
private ContactInfo() {
|
||||
|
@ -211,29 +217,42 @@ public class ContactInfo {
|
|||
return info;
|
||||
}
|
||||
|
||||
static void init(final Context context, Handler handler) {
|
||||
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS))
|
||||
try {
|
||||
ContentObserver observer = new ContentObserver(handler) {
|
||||
@Override
|
||||
public void onChange(boolean selfChange, Uri uri) {
|
||||
Log.i("Contact changed uri=" + uri);
|
||||
try {
|
||||
emailLookup = getEmailLookup(context);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
executor.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
emailLookup = getEmailLookup(context);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
emailLookup = getEmailLookup(context);
|
||||
|
||||
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
|
||||
Log.i("Observing uri=" + uri);
|
||||
context.getContentResolver().registerContentObserver(uri, true, observer);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
|
||||
Log.i("Observing uri=" + uri);
|
||||
context.getContentResolver().registerContentObserver(uri, true, observer);
|
||||
}
|
||||
}
|
||||
|
||||
static Uri getLookupUri(Context context, Address[] addresses) {
|
||||
|
@ -253,6 +272,7 @@ public class ContactInfo {
|
|||
Map<String, Uri> all = new ConcurrentHashMap<>();
|
||||
|
||||
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
|
||||
Log.i("Reading email/uri");
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
|
||||
long untrusted = -1;
|
||||
|
|
|
@ -385,6 +385,9 @@ public class FragmentSetup extends FragmentBase {
|
|||
break;
|
||||
}
|
||||
|
||||
if (has)
|
||||
ContactInfo.init(getContext());
|
||||
|
||||
tvPermissionsDone.setText(has ? R.string.title_setup_done : R.string.title_setup_to_do);
|
||||
tvPermissionsDone.setTextColor(has ? textColorPrimary : colorWarning);
|
||||
tvPermissionsDone.setCompoundDrawablesWithIntrinsicBounds(has ? check : null, null, null, null);
|
||||
|
|
Loading…
Reference in New Issue