Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a user profile in ContactsContract?

Tags:

android

I am trying to create a user profile in ContactsContract...because there is not one, and I need one for testing. I don't have a real-life Android device, and only have the AVD Emulator for testing.

Here is the code block that I am working from:

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

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
    .withValue(RawContacts.ACCOUNT_TYPE, null)
    .withValue(RawContacts.ACCOUNT_NAME, null)
    .build());        

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID, 0)
    .withValue(Data.MIMETYPE, Profile.CONTENT_RAW_CONTACTS_URI)
    .withValue(Profile.IS_USER_PROFILE, 1)
    .build()); 

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID, 0)
    .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
    .withValue(StructuredName.DISPLAY_NAME, name)
    .build());

this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

If you remove the lines for making this entry a user profile, it works fine (i.e., will insert the name). However, I can not figure out how to set the entry with the IS_USER_PROFILE flag.

Can you create a user profile from an App? If so, any ideas on why this won't work?

Many thanks! Scott

like image 784
Scott Wardlaw Avatar asked Oct 09 '12 19:10

Scott Wardlaw


2 Answers

@ozmank gave a good pointer - thought of adding a more current working example from an app that targets API level 15-23

I encountered this issue mainly on Samsung devices - where the profile doesn't exist and it can be deleted when it does exist.

  1. Add to AndroidManifest.xml uses-permission for WRITE_PROFILE

    <uses-permission android:name="android.permission.WRITE_PROFILE"/>
    
  2. Create an empty raw contact that belongs to the profile - this will generate the profile entry

    ContentValues values = new ContentValues();            
    context.getContentResolver().insert(Profile.CONTENT_RAW_CONTACTS_URI, values);
    

from the Uri that is returned you can query the raw contact entry.

Note - This doesn't add info to the profile, so if you will query the profile for info using

Uri uri = Uri.withAppendedPath(Profile.CONTENT_URI, Contacts.Data.CONTENT_DIRECTORY);
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

The result cursor will be empty, and maybe if you'll decide to run the code in #2 again and go into the stock contacts app you will see multiple empty entries under the Me profile - so my recommendation is to keep the me profile as clean as possible and track the raw contact that you created and check if it is still valid - before creating a new raw contact.

Uri uri = Uri.withAppendedPath(Profile.CONTENT_RAW_CONTACTS_URI, String.valueOf(lastLocalProfileManualRawId));
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

if (cursor != null && cursor.getCount() > 0){
    // The profile exists
} else {
    // Create a new raw contact
}
like image 160
Noa Drach Avatar answered Nov 07 '22 15:11

Noa Drach


var values = new ContentValues (); values.Put (ContactsContract.Contacts.InterfaceConsts.is_user_profile,"1"); ontentResolver.Update (ContactsContract.Profile.ContentRawContactsUri,values, null, null);

like image 1
ozmank Avatar answered Nov 07 '22 15:11

ozmank