title: Contacts getting duplicated while adding raw contact based on phone number
body: <p>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.  </p>
  
  <p>What I did is, I queried to list all the contacts in the device by using below URI  <code>ContactsContract.Contacts.CONTENT_URI</code><br>
  Again queried all the phone numbers of each of the contacts returned by executing above query using the <code>URI</code></p>
  
  <pre><code>ContactsContract.CommonDataKinds.Phone.CONTENT_URI
  </code></pre>
  
  <p>Then created ClsContactList <code>class objects</code> 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.  </p>
  
  <pre><code>private static void addContact(Account account, ClsContactList ObjContact) {
      ArrayList&lt;ContentProviderOperation&gt; operationList = new ArrayList&lt;ContentProviderOperation&gt;();
      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();
          }
      }
  }  
  </code></pre>
  
  <p>Please have a look at the attached screen shot. Many thanks in advance.  </p>
  
  <p>Raw contacts in contact details activity</p>
  
  <p><a href="https://i.stack.imgur.com/Tul4m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tul4m.png" alt="enter image description here"></a></p>
  
  <p>Contacts duplicated in Android contact list</p>
  
  <p><a href="https://i.stack.imgur.com/r2gES.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r2gES.png" alt="enter image description here"></a></p>