Contacts getting duplicated while adding raw contact based on phone number

382 views Asked by At

I am trying to add raw contacts for each of the phone numbers associated with all contacts in the Android contact list. When certain contacts have multiple phone numbers associated with it, android contact list shows the same contact multiple times. I want to aggregate those newly added raw contacts(based on the phone number) and shows under that contact.

What I did is, I queried to list all the contacts in the device by using below URI ContactsContract.Contacts.CONTENT_URI
Again queried all the phone numbers of each of the contacts returned by executing above query using the URI

ContactsContract.CommonDataKinds.Phone.CONTENT_URI

Then created ClsContactList class objects for each of the contact, which contains contact's display name and an array of phone numbers associated with that contact. Finally, these objects added to a list and for each object in the list, called the attached function to create raw contact.

private static void addContact(Account account, ClsContactList ObjContact) {
    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
    int rawContactInsertIndex = operationList.size();
    for (String phoneNumber : ObjContact.PhoneNumbers) {
        operationList.clear();
        //Creating raw contact
        ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
        builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
        builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
        builder.withValue(ContactsContract.RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DEFAULT);
        //builder.withValue(RawContacts.SYNC1, ObjContact.getDisplay_name());
        operationList.add(builder.build());
        //INSERT NAME
        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
        builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
        builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, ObjContact.getDisplay_name());
        operationList.add(builder.build());

        //INSERT MOBILE
        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
        builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
        builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber); // Number of the person
        //builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
        operationList.add(builder.build());

        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
        builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.org.c99.SyncProviderDemo.profile");
        builder.withValue(ContactsContract.Data.DATA1, "VOIP Call ("+phoneNumber+")");
        builder.withValue(ContactsContract.Data.DATA2, "Dialer App");
        builder.withYieldAllowed(true);
        operationList.add(builder.build());
        try {
            mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}  

Please have a look at the attached screen shot. Many thanks in advance.

Raw contacts in contact details activity

enter image description here

Contacts duplicated in Android contact list

enter image description here

1

There are 1 answers

1
vikas kumar On

You need to query like this to get the filtered data by comparing the mime type.

ContactsContract.Data.MIMETYPE = StructuredPostal.CONTENT_ITEM_TYPE

Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
     null, null, ContacsContract.Data.MIMETYPE +  "='" + 
ContactsContract.StructuredPostal.CONTENT_ITEM_TYPE + "'", null);