diff --git a/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java b/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java index f8645a6..55c2330 100644 --- a/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java +++ b/android/src/main/java/flutter/plugins/contactsservice/contactsservice/ContactsServicePlugin.java @@ -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); @@ -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) { @@ -443,6 +450,7 @@ protected ArrayList 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; } @@ -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 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