- Uri contactUri = Uri.fromParts("mailto", attendee.mEmail, null);
-
- Intent contactIntent = new Intent(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
- contactIntent.setData(contactUri);
-
- // Pass along full E-mail string for possible create dialog
- Rfc822Token sender = new Rfc822Token(attendee.mName, attendee.mEmail, null);
- contactIntent.putExtra(Contacts.Intents.EXTRA_CREATE_DESCRIPTION,
- sender.toString());
-
- // Mark target position using on-screen coordinates
- // TODO uncomment when contacts code is in.
- // contactIntent.putExtra(Intents.EXTRA_TARGET_RECT, rect);
+ // First perform lookup query to find existing contact
+ final ContentResolver resolver = getContentResolver();
+ final String address = attendee.mEmail;
+ final Uri dataUri = Uri.withAppendedPath(CommonDataKinds.Email.CONTENT_FILTER_URI,
+ Uri.encode(address));
+ final Uri lookupUri = ContactsContract.Data.getContactLookupUri(resolver, dataUri);
+
+ if (lookupUri != null) {
+ // Found matching contact, trigger QuickContact
+ QuickContact.showQuickContact(this, rect, lookupUri, QuickContact.MODE_MEDIUM, null);
+ } else {
+ // No matching contact, ask user to create one
+ final Uri mailUri = Uri.fromParts("mailto", address, null);
+ final Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, mailUri);
+
+ // Pass along full E-mail string for possible create dialog
+ Rfc822Token sender = new Rfc822Token(attendee.mName, attendee.mEmail, null);
+ intent.putExtra(Intents.EXTRA_CREATE_DESCRIPTION, sender.toString());
+
+ // Only provide personal name hint if we have one
+ final String senderPersonal = attendee.mName;
+ if (!TextUtils.isEmpty(senderPersonal)) {
+ intent.putExtra(Intents.Insert.NAME, senderPersonal);
+ }