Wednesday, June 9, 2010

Re: [android-developers] ContactContracts

Hi Vikram,

The data structure in the contacts DB has three levels:  
- Contact is an aggregate of RawContacts
- RawContact represents a specific contact data set from a particular source (e.g. Gmail or Exchange)
- Data contains all contact data: emails, phone numbers, birthday, etc one data element per row.

When you query the Data table, it is automatically joined with the corresponding Contact so you can get the aggregate's name and photo ID.

Specifically, 

- Lookup by email address: you can use either Email.CONTENT_LOOKUP_URI or Email.CONTENT_FILTER_URI. Use the latter if you want to search by incomplete email addresses. This is what email programs use for autocompletion.  In either case  Email.CONTACT_ID will give you the _ID of the corresponding aggregated contact.  The result will also include the contact name and photo ID.

- Phone lookup.  Despite its name, PhoneLookup is not what you need.  PhoneLookup is a special table used for caller ID in telephony. Try Phone.CONTENT_FILTER_URI instead, appending the encoded phone number to that URI.  It behaves just like Email.CONTENT_FILTER_URI.  Phone.CONTACT_ID will give you the contact ID  

- To do a lookup by name use Contacts.CONTENT_FILTER_URI.  In this case _ID is the contact _ID.  

Once you have the ID of the contact you needed, read Data for that Contact and find rows for phone number, email etc.  This bit is a little tricky because there might be several email addresses or none, and you will need to handle all those cases.  If the user has chosen a default email or phone number for that contact, the corresponding data row will have IS_SUPER_PRIMARY=1.

I hope this helps.

Cheers,
- Dmitri

On Wed, Jun 9, 2010 at 9:06 AM, Vikram <vikram.bodicherla@gmail.com> wrote:
My application user registers a phone number, name or an email ID with
my application. Now given one of these, I want to lookup the
ContactsContract provider and get the others.

Given
1. the email: I am querying
ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI and getting
{ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER}. This works.

2. given the number, I want to use the PhoneLookup.CONTENT_FILTER_URI.
I can get the name using DISPLAY_NAME, but I cant get the email, so
I'll have to use the CONTACT_ID. So in this table, does _ID correspond
to CONTACT_ID?

3. given the name, I have to use the DATA table, and I can get the
number. For the email I have to get the CONTACT_ID and read the email
from where?

Is my assumption that CONTACT_ID can get us name, number, email and
the contact's picture information correct? Do _IDs correspond to
CONTACT_ID (of course not at all places, but if a record is about a
user, can I assume that _ID = CONTACT_ID).

Is there any better way to do this? I do it only once for the whole
app's lifetime so I can compromise on efficiency for correctness: the
phone numbers and name inputs to my app can be malformatted, and I
want ContactsContract to help me out here.

Any suggestions?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment