Understand contacts


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

contacts
rawcontacts
data

An account is an external concept to the contacts api

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.

The account id is kept in the raw contact table.

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.


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

A personal profile is introduced in 4.0

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

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 [[email protected]] = [email protected]).

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

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

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

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.

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

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.


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