Small improvement

This commit is contained in:
M66B 2019-07-23 18:51:03 +02:00
parent 3704960fd4
commit d2f673dafc
1 changed files with 48 additions and 46 deletions

View File

@ -119,42 +119,41 @@ public class ContactInfo {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) {
try { ContentResolver resolver = context.getContentResolver();
ContentResolver resolver = context.getContentResolver(); try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{
new String[]{ ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID, ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.Contacts.DISPLAY_NAME
ContactsContract.Contacts.DISPLAY_NAME },
}, ContactsContract.CommonDataKinds.Email.ADDRESS + " = ? COLLATE NOCASE",
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ? COLLATE NOCASE", new String[]{
new String[]{ address.getAddress()
address.getAddress() }, null)) {
}, null)) {
if (cursor != null && cursor.moveToNext()) { if (cursor != null && cursor.moveToNext()) {
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID); int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY); int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
long contactId = cursor.getLong(colContactId); long contactId = cursor.getLong(colContactId);
String lookupKey = cursor.getString(colLookupKey); String lookupKey = cursor.getString(colLookupKey);
Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey); Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
boolean avatars = prefs.getBoolean("avatars", true); boolean avatars = prefs.getBoolean("avatars", true);
if (avatars) { if (avatars) {
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, lookupUri); InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, lookupUri);
info.bitmap = BitmapFactory.decodeStream(is); info.bitmap = BitmapFactory.decodeStream(is);
}
info.displayName = cursor.getString(colDisplayName);
info.lookupUri = lookupUri;
} }
info.displayName = cursor.getString(colDisplayName);
info.lookupUri = lookupUri;
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }
}
if (info.bitmap == null) { if (info.bitmap == null) {
int dp = Helper.dp2pixels(context, 48); int dp = Helper.dp2pixels(context, 48);
@ -213,29 +212,28 @@ public class ContactInfo {
} }
static void init(final Context context, Handler handler) { static void init(final Context context, Handler handler) {
if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) { if (Helper.hasPermission(context, Manifest.permission.READ_CONTACTS))
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);
}
}
};
try { 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);
}
}
};
emailLookup = getEmailLookup(context); emailLookup = getEmailLookup(context);
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
Log.i("Observing uri=" + uri);
context.getContentResolver().registerContentObserver(uri, true, observer);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(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) { static Uri getLookupUri(Context context, Address[] addresses) {
@ -266,6 +264,8 @@ public class ContactInfo {
null)) { null)) {
if (cursor != null && cursor.moveToNext()) if (cursor != null && cursor.moveToNext())
untrusted = cursor.getLong(0); untrusted = cursor.getLong(0);
} catch (Throwable ex) {
Log.e(ex);
} }
Log.i("Untrusted group=" + untrusted); Log.i("Untrusted group=" + untrusted);
@ -303,6 +303,8 @@ public class ContactInfo {
Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey); Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
all.put(email, uri); all.put(email, uri);
} }
} catch (Throwable ex) {
Log.e(ex);
} }
} }