Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I extend Androids Contacts database?

I was wondering is it possible to extend the Android Contacts database?

From here - http://d.android.com/reference/android/provider/ContactsContract.html

It says:

ContactsContract defines an extensible database of contact-related information

Extensible would suggest to me that I can add in more data to the contacts application outside the normal values such as Name, number, email, work number, home number etc..

However the examples of this page - http://d.android.com/reference/android/provider/ContactsContract.RawContacts.html only show how to insert the standard values like name and not how to add a new field to a contact.

Furthermore a search on the web does not turn up much information on extending the contacts data.

So I was wondering is it even possible or does the extensible refer to some other part of the contacts?

For example I would like to add in an additional field for contacts that have special privileges within my app so when a user looks at the contacts he or she knows what users they can use my app with.

Is this possible?

like image 506
Donal Rafferty Avatar asked Jan 19 '11 18:01

Donal Rafferty


1 Answers

You can store custom data in the contacts database. However "when a user looks at the contacts he or she knows what users they can use my app with," may not be possible if you are thinking users will be able to see the custom data you inserted while using the built-in Android Contacts application. You would have to display the custom data in your own application.

The javadocs for the ContactsContract.Data class should provide an explanation, as well as the Contacts article.

To use this you'll need to get a raw contact id by querying the RawContacts.

Here some example code that might help you get started...

private void makePowerful(int rawContactId) {
    ContentValues values = new ContentValues();
    values.put(Privilege.RAW_CONTACT_ID, rawContactId);
    values.put(Privilege.MIMETYPE, Privilege.CONTENT_ITEM_TYPE);
    values.put(Privilege.PRIVILEGE_LEVEL, Privilege.TYPE_POWERFUL);
    Uri uri = getContentResolver().insert(Data.CONTENT_URI, values);
}

public static final class Privilege implements ContactsContract.DataColumnsWithJoins, ContactsContract.CommonDataKinds.CommonColumns {
    public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/my_app_privilege";
    public static final int TYPE_POWERFUL = 1;
    public static final int TYPE_WEAK = 2;
    public static final String PRIVILEGE_LEVEL = DATA1;

    private Privilege() { }
}
like image 118
satur9nine Avatar answered Sep 23 '22 22:09

satur9nine