contacts coding help
satya - Wednesday, November 03, 2010 3:02:41 PM
read accounts
public void testAccounts()
{
    AccountManager am = AccountManager.get(this.mContext); 
    Account[] accounts = am.getAccounts();
    for(Account ac: accounts)
    {
        String acname=ac.name;
        String actype = ac.type;
        this.mReportTo.reportBack(tag,acname + ":" + actype);
    }
}
satya - Wednesday, November 03, 2010 3:03:13 PM
You need thsi permission
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
satya - Wednesday, November 03, 2010 3:34:37 PM
Here is how to print all the columns from a URI
private void printCursorColumnNames(Cursor c)
{
    int count = c.getColumnCount();
    StringBuffer cnamesBuffer = new StringBuffer();
    for (int i=0;i<count;i++)
    {
        String cname = c.getColumnName(i);
        cnamesBuffer.append(cname).append(';');
    }
    this.mReportTo.reportBack(tag,cnamesBuffer.toString());
}
private Cursor getContacts()
{
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
                         + " COLLATE LOCALIZED ASC";
    Activity a = (Activity)this.mContext;
    return a.managedQuery(uri, null, null, null, sortOrder);
}
Notice how the projection is passed as null. Subsequently we go after the column count and column names based on index. Useful for debugging.
satya - Wednesday, November 03, 2010 5:07:39 PM
Here are all the fields from the cotnact URI above
times_contacted;
contact_status;
custom_ringtone;
has_phone_number;
phonetic_name;
phonetic_name_style;
contact_status_label;
lookup;
contact_status_icon;
last_time_contacted;
display_name;
sort_key_alt;
in_visible_group;
_id;
starred;
sort_key;
display_name_alt;
contact_presence;
display_name_source;
contact_status_res_package;
contact_status_ts;
photo_id;
send_to_voicemail;
satya - Wednesday, November 03, 2010 9:23:49 PM
Source code of viewcontactactivity
Source code of viewcontactactivity
Hoping to find the answer to how contacts are aggregated
satya - Wednesday, November 03, 2010 9:49:36 PM
Looks like that was the old version. Here is the 2.0 source code
Looks like that was the old version. Here is the 2.0 source code
satya - Wednesday, November 03, 2010 10:58:00 PM
Here is the source code for the contacts provider
satya - Monday, November 08, 2010 11:39:21 AM
How to read an android aggregated contact
How to read an android aggregated contact
satya - Monday, November 08, 2010 11:39:58 AM
How can I construct an android contact lookup uri
How can I construct an android contact lookup uri
Search for: How can I construct an android contact lookup uri
satya - Monday, November 08, 2010 11:41:30 AM
How can I read various raw contact ids based on an aggregated contact
How can I read various raw contact ids based on an aggregated contact
Search for: How can I read various raw contact ids based on an aggregated contact
satya - Monday, November 08, 2010 11:42:13 AM
How does the contacts application fire off the lookup uri
How does the contacts application fire off the lookup uri
Search for: How does the contacts application fire off the lookup uri
satya - Monday, November 08, 2010 11:43:07 AM
Does the contacts application read the raw contacts for displaying an aggregated contact
Does the contacts application read the raw contacts for displaying an aggregated contact
Search for: Does the contacts application read the raw contacts for displaying an aggregated contact
satya - Monday, November 08, 2010 11:49:49 AM
Read up on contact lookup uri
satya - Tuesday, November 09, 2010 10:52:02 AM
Here is the structure of the contact uri
Contact name: 
C3-first C3-last
lookup url:
content://com.android.contacts/contacts/lookup
/2582r3-302982363C4E5052302982422C5052
The last part of this url is the lookup key for that contact
satya - Tuesday, November 09, 2010 10:53:38 AM
Getting a lookup key from the cursor
private String getLookupKey(Cursor cc)
{
 int lookupkeyIndex = 
    cc.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
 return cc.getString(lookupkeyIndex);
}
satya - Tuesday, November 09, 2010 10:55:33 AM
Constructing a lookup uri to lookup an aggregated contact
private String getLookupUri(String lookupkey)
{
    String luri = 
       ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + lookupkey;
    return luri;
}
satya - Tuesday, November 09, 2010 11:26:35 AM
The lookup uri cursor seem to be identical to the list of contacts cursor
times_contacted;
contact_status;
custom_ringtone;
has_phone_number;
phonetic_name;
phonetic_name_style;
contact_status_label;
lookup;
contact_status_icon;
last_time_contacted;
display_name;
sort_key_alt;
in_visible_group;
_id;
starred;
sort_key;
display_name_alt;
contact_presence;
display_name_source;
contact_status_res_package;
contact_status_ts;
photo_id;
send_to_voicemail;
satya - Tuesday, November 09, 2010 2:00:01 PM
managedquery arguments
uri
projection
selection
selectionargs
sortorder
satya - Tuesday, November 09, 2010 3:15:26 PM
Column names from a raw contact uri
times_contacted;
 phonetic_name;
 phonetic_name_style;
 contact_id;version;
 last_time_contacted;
 aggregation_mode;
 _id;
 name_verified;
 display_name_source;
 dirty;
 send_to_voicemail;
 account_type;
 custom_ringtone;
 sync4;sync3;sync2;sync1;
 deleted;
 account_name;
 display_name;
 sort_key_alt;
 starred;
 sort_key;
 display_name_alt;
 sourceid;
satya - Wednesday, November 10, 2010 10:03:35 AM
Example of an aggregated contact
public class AggregatedContact 
{
    public String id;
    public String lookupUri;
    public String lookupKey;
    public String displayName;
    
    public void fillinFrom(Cursor c)
    {
        id = Utils.getColumnValue(c,"_ID");
        lookupKey = Utils.getColumnValue(c,ContactsContract.Contacts.LOOKUP_KEY);
        lookupUri = ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + lookupKey;
        displayName = Utils.getColumnValue(c,ContactsContract.Contacts.DISPLAY_NAME);
    }
}
satya - Wednesday, November 10, 2010 10:04:49 AM
Example of a raw contact
public class RawContact 
{
    public String rawContactId;
    public String aggregatedContactId;
    public String accountName;
    public String accountType;
    public String displayName;
    
    public void fillinFrom(Cursor c)
    {
        rawContactId = Utils.getColumnValue(c,"_ID");
        accountName = Utils.getColumnValue(c,ContactsContract.RawContacts.ACCOUNT_NAME);
        accountType = Utils.getColumnValue(c,ContactsContract.RawContacts.ACCOUNT_TYPE);
        aggregatedContactId = Utils.getColumnValue(c,ContactsContract.RawContacts.CONTACT_ID);
        displayName = Utils.getColumnValue(c,"display_name");
    }
    public String toString()
    {
        return displayName
            + "/" + accountName + ":" + accountType
            + "/" + rawContactId
            + "/" + aggregatedContactId;
    }
}
satya - Wednesday, November 10, 2010 10:23:39 AM
So here is one important cursor: RawContactEntity
data_version;
contact_id;
version;
data12;data11;data10;
mimetype;
res_package;
_id;
data15;data14;data13;
name_verified;
is_restricted;
is_super_primary;
data_sync1;dirty;data_sync3;data_sync2;
data_sync4;account_type;data1;sync4;sync3;
data4;sync2;data5;sync1;
data2;data3;data8;data9;
deleted;
group_sourceid;
data6;data7;
account_name;
data_id;
starred;
sourceid;
is_primary;
