Understand contacts

satya - Friday, November 18, 2011 11:20:21 AM

there are 4 concepts to understand contacts


accounts
contacts
raw contacts
raw contact data (often just called data)

satya - Friday, November 18, 2011 11:35:01 AM

key tables


contacts
rawcontacts
data

satya - Friday, November 18, 2011 11:35:15 AM

An account is an external concept to the contacts api

An account is an external concept to the contacts api

satya - Friday, November 18, 2011 11:44:53 AM

A raw contact holds the key for understanding contacts

a raw contact holds all the details pertaining to a contact such as email, first name, phone number, address etc. All of these details are maintained in the data table with the raw contact id as one of the keys.

satya - Friday, November 18, 2011 11:45:30 AM

An account has a number of raw contacts

The account id is kept in the raw contact table.

satya - Friday, November 18, 2011 11:47:15 AM

Multiple raw contacts from multiple accounts are joined together

Multiple raw contacts from multiple accounts are joined together when they are a match based on an algorithm such as, same firstname, last name etc.

satya - Friday, November 18, 2011 11:49:21 AM

That is all there is to it


account1-ac1
   rc1-rawcontact1
      email, name, address (as data)
   rc2-rawcontact2
      email, name, address (as data)
account2-ac2
   rc1-rawcontact1
      email, name, address (as data)
   rc2-rawcontact2
      email, name, address (as data)
contact1
   (ac1)rc1-rawcontact1
   (ac2)rc1-rawcontact1
contact2
   (ac1)rc2-rawcontact2
   (ac2)rc2-rawcontact2

satya - Friday, November 18, 2011 11:49:42 AM

A personal profile is introduced in 4.0

A personal profile is introduced in 4.0

satya - Friday, November 18, 2011 11:58:21 AM

Personal profile is just a SINGLE distinct contact

that gets its own set of raw contacts and data elements. Unlike the other contacts there is only ONE CONTACT that is ME

satya - Tuesday, November 22, 2011 9:55:12 AM

Two raw contacts are considered a match

1. They have matching names.

2. Their names consist of the same words but in different order (for example, "Bob Parr" and "Parr, Bob")

3. One of them has a common short name for the other (for example, "Bob Parr" and "Robert Parr")

4. One of them has just a first or last name and it matches the other raw contact. This rule is less reliable, so it only applies if the two raw contacts are also sharing some other data like a phone number, an email address or a nickname (for example, Helen ["elastigirl"] = Helen Parr ["elastigirl"])

5. At least one of the two raw contacts is missing the name altogether and they are sharing a phone number, an email address or a nickname (for example, Bob Parr [incredible@android.com] = incredible@android.com).

satya - Thursday, November 24, 2011 9:47:44 AM

How can you identify the unique id of a raw contact in its source account?

we know that raw contacts are tied to accounts such as your email accounts. The raw contact is uniquely identified on the device by its account and a uniquely generated id.

A question arises. Does android store the unique id of this raw contact as listed in the source account.

For example if the raw contact came from gmail, will the unique id of this contact as known to the gmail is stored?? Yes it is stored.

The "sourceid" field of the raw contacts table holds the unique id of the contact as known to the source account

satya - Thursday, November 24, 2011 9:50:34 AM

There is a table that defines the mime types

this is a code set table that translates the mime type text based on a mime type id indicated in the data table.

satya - Thursday, November 24, 2011 9:53:44 AM

when a raw contact is inserted you can tell it not to be aggregated

when a raw contact is inserted you can tell it not to be aggregated

satya - Thursday, November 24, 2011 9:56:57 AM

the idea of a look up key

This lookup field is an aggregation (concatenation) of the account and the unique ID of this contact in that account for each raw contact. This information is further codified so that it can be passed as a URL parameter to retrieve the latest aggregated contact ID. Android looks at the lookup key and sees which underlying raw contact IDs are there for this lookup key. It then uses a best-fit algorithm to return a suitable (or perhaps new) aggregated contact ID.

satya - Thursday, November 24, 2011 10:07:30 AM

ContactsContract.Contacts.CONTENT_LOOKUP_URI

You know from the discussion of the contact lookup URIs that each lookup URI represents a collection of raw contact identities that have been concatenated. That being the case, you might have expected the lookup URI to return a series of matching raw contacts. However, the test above (Listing 27?29) is showing that it is not returning a cursor of raw contacts but instead a cursor of contacts.

--Taken from ProAndroid 3, chapter 27

satya - Thursday, November 24, 2011 10:10:04 AM

Further more

One consequence of this is that there is no public mechanism available to go from the look up key to its constituent raw contacts. Instead, you have to find the contact id for that lookup key and then fire off a raw contact URI for that contact ID to retrieve the corresponding raw contacts.

satya - Thursday, November 24, 2011 10:14:26 AM

Here is the crux of getting to the raw contacts and data based on contact id


Uri uri = ContactsContract.RawContactsEntity.CONTENT_URI;
c = this.getACursor(uri,"contact_id in (3,4,5)");

ofcourse to know the values for the contact_id you have to use the lookup key to that you may have stored to locate the contact_id