Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying contact information

I'm trying to insert and update a piece of information on an existing contact so I've created a sample application in order to develop the functionality. All I want my sample app to do is to insert (or if present) update an email address on a contact.

I'm selecting a contact through the system Intent like so:

    startActivityForResult(new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI), PICK_CONTACT_REQUEST);

The URI which is returned is that of the Contact (RawContact?) which was selected and comes in this form: content://com.android.contacts/contacts/lookup/0r2-2A90214945/2.

I can pull back all of the Data (RawContact?) items on this by performing the following code:

Cursor cursor = contentResolver.query(mContactUri, null, null, null, null);
try {
    if (cursor.moveToFirst()) {
        for(int i=0; i < cursor.getColumnCount(); i++) {
            String message = cursor.getColumnName(i);
            Log.v("", message);
        }
    }
} finally {
    cursor.close();
}

From this I should be able to determine if the contact already has an CommonDataTypes.Email Data member:

cursor.getColumnIndex(CommonDataKinds.Email.CONTENT_ITEM_TYPE) != -1;

And then perform one of the following to either Insert or Update the Data:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation.newInsert(mContactUri)
    .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
    .withValue(Email.DISPLAY_NAME, "[email protected]")
    .withValue(Email.TYPE, Email.TYPE_HOME)
    .build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

But this gives me an exception: java.lang.UnsupportedOperationException: URI: content://com.android.contacts/contacts/lookup/0r2-2A90314945/2, calling user:

Hopefully someone can see what I've missed.

PS, I'm using these permissions:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
like image 832
Graeme Avatar asked Jan 09 '12 11:01

Graeme


People also ask

Why am I not able to edit my contacts?

This can happen if the contacts are linked to an account that was not 'properly' removed from the phone, or when an application modifies contact entries in an incorrect fashion. Use the display options in the People app to figure out which contacts are linked to which account.

How do I restore deleted contacts?

On your computer, open the Google Contacts website and login using your Google account. Now, click on the 'Settings' icon at the top right corner. Click on 'Undo Changes' and select the restoration period.


1 Answers

The Android people need to update their documentation. It actually served to make me know less about what was happening than I would have gotten from guessing. It suggests that you can pull back a Contact, which will contain many RawContacts which will contain Data.

That interpretation is completely wrong. ContactContracts data is instead three normal average everyday database tables*:

ContactContract Tables

Table: Contacts

Access URI: Contacts.CONTENT_URI

Primary Key**: Data._ID

Description:

This table contains information about a Contact (when was it added, what's is it's user icon, does it have a custom ringtone).

Relationship: It has a 1-to-many relationship with the RawContact table.

Table: RawContacts

Access URI: RawContacts.CONTENT_URI

Primary Key: Data._ID

Foreign Key**: Data.CONTACT_ID

Description:

This table contains information about a related set of Data items. A RawContact could contain Email Type, Email Display Name, Phone Number, Phone Display Name, etc. A RawContact can be aggregated with other RawContacts to make a Contact as a user sees it. A Contact could contain just one RawContact.

Relationship: It has a 1-to-many relationship with the Data table.

Table: Data

Access URI: Data.CONTENT_URI

Primary Key: Data._ID

Foreign Key: Data.RAW_CONTACT_ID

Description:

This table contains a single field of information. An email address, A phone number, A phone number type (home/work), A nickname, A display name.

In answer to the question

I've uploaded the entire sample project to GitHub in order to allow others to see how to query, update and insert records using ContactContract.

You can find the project to download here: https://github.com/gwoodhouse/ContactContractSample

If you just want to look at the java code performing the query/update/insert here is the class file: https://github.com/gwoodhouse/ContactContractSample/blob/master/ContactsIntegration/src/com/woodhouse/example/activity/ContactsIntegrationActivity.java

Hope this helps!

*Not a table, but a ContentProvider

** not strictly true.

like image 195
Graeme Avatar answered Oct 07 '22 02:10

Graeme