Skip to content

Commit

Permalink
Adding a posible solution for the getContactsByEmail in Android
Browse files Browse the repository at this point in the history
  • Loading branch information
fjbatresv committed Sep 21, 2021
1 parent 4838a37 commit 0f3162e
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public void onMethodCall(MethodCall call, Result result) {
} case "getContactsForPhone": {
this.getContactsForPhone(call.method, (String)call.argument("phone"), (boolean)call.argument("withThumbnails"), (boolean)call.argument("photoHighResolution"), (boolean)call.argument("orderByGivenName"), (boolean)call.argument("androidLocalizedLabels"), result);
break;
} case "getContactsForEmail": {
this.getContactsForEmail(call.method, (String)call.argument("email"), (boolean)call.argument("withThumbnails"), (boolean)call.argument("photoHighResolution"), (boolean)call.argument("orderByGivenName"), (boolean)call.argument("androidLocalizedLabels"), result);
break;
} case "getAvatar": {
final Contact contact = Contact.fromMap((HashMap)call.argument("contact"));
this.getAvatar(contact, (boolean)call.argument("photoHighResolution"), result);
Expand Down Expand Up @@ -212,6 +215,10 @@ private void getContactsForPhone(String callMethod, String phone, boolean withTh
new GetContactsTask(callMethod, result, withThumbnails, photoHighResolution, orderByGivenName, localizedLabels).executeOnExecutor(executor, phone, true);
}

private void getContactsForEmail(String callMethod, String email, boolean withThumbnails, boolean photoHighResolution, boolean orderByGivenName, boolean localizedLabels, Result result) {
new GetContactsTask(callMethod, result, withThumbnails, photoHighResolution, orderByGivenName, localizedLabels).executeOnExecutor(executor, email, true);
}

@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
if (delegate instanceof ContactServiceDelegate) {
Expand Down Expand Up @@ -443,6 +450,7 @@ protected ArrayList<HashMap> doInBackground(Object... params) {
case "openDeviceContactPicker": contacts = getContactsFrom(getCursor(null, (String) params[0]), localizedLabels); break;
case "getContacts": contacts = getContactsFrom(getCursor((String) params[0], null), localizedLabels); break;
case "getContactsForPhone": contacts = getContactsFrom(getCursorForPhone(((String) params[0])), localizedLabels); break;
case "getContactsForEmail": contacts = getContactsFrom(getCursorForEmail(((String) params[0]))), localizedLabels); break;
default: return null;
}

Expand Down Expand Up @@ -537,6 +545,30 @@ private Cursor getCursorForPhone(String phone) {
return null;
}

private Cursor getCursorForEmail(String email) {
if (email.isEmpty())
return null;

Uri uri = Uri.withAppendedPath(Email.CONTENT_URI, Uri.encode(email));
String[] projection = new String[]{BaseColumns._ID};

ArrayList<String> contactIds = new ArrayList<>();
Cursor emailCursor = contentResolver.query(uri, projection, null, null, null);
while (emailCursor != null && emailCursor.moveToNext()){
contactIds.add(emailCursor.getString(emailCursor.getColumnIndex(BaseColumns._ID)));
}
if (emailCursor!= null)
emailCursor.close();

if (!contactIds.isEmpty()) {
String contactIdsListString = contactIds.toString().replace("[", "(").replace("]", ")");
String contactSelection = ContactsContract.Data.CONTACT_ID + " IN " + contactIdsListString;
return contentResolver.query(ContactsContract.Data.CONTENT_URI, PROJECTION, contactSelection, null, null);
}

return null;
}

/**
* Builds the list of contacts from the cursor
* @param cursor
Expand Down

0 comments on commit 0f3162e

Please sign in to comment.