I have added app row in contacts detail screen.I am experiencing issue in few devices.For certain contacts I am seeing separate contact being created and my app row is shown both in the existing and separate contacts.That means the new contact being created with addContact() doesn't merge completely with existing contact inside joinIntoExistingContact() method.I tried debugging and found joinIntoExistingContact() method is executing fine and applyBatch() call doesn't throw any exception still the new contact doesn't merge with existing contact.My device show a separate contact added with phone number in contacts name.
private static void addContact(ContentResolver contentResolver,int name, int phoneNumber,long existingContactId) {
Log.i("BHUVNESH", "Adding contact: " + name);
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
//Create our RawContact
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, name);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.example.bhuvnesh.myapplication");
builder.withValue(ContactsContract.RawContacts.SYNC1, phoneNumber);
builder.withValue(ContactsContract.RawContacts.AGGREGATION_MODE,ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
operationList.add(builder.build());
//Create a Data record of common type 'StructuredName' for our RawContact
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
operationList.add(builder.build());
//Create a Data record of custom type "vnd.android.cursor.item/vnd.com.example.bhuvnesh.myapplication.profile" to display a link to the Last.fm profile
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.com.example.bhuvnesh.myapplication.profile");
builder.withValue(ContactsContract.Data.DATA1, phoneNumber);
builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
builder.withValue(ContactsContract.Data.DATA3, "View profile");
operationList.add(builder.build());
try {
ContentProviderResult[] results = contentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
int rawcontactId = Integer.parseInt(results[0].uri.getLastPathSegment());
joinIntoExistingContact(existingContactId,rawcontactId);
} catch (Exception e) {
Log.e("BHUVNESH", "Something went wrong during creation! " + e);
e.printStackTrace();
}
}
private void joinIntoExistingContact(long existingContactId, long newRawContactId) {
// get all existing raw-contact-ids that belong to the contact-id
List<Long> existingRawIds = new ArrayList<>();
Cursor cur = getContentResolver().query(RawContacts.CONTENT_URI, new String[] { RawContacts._ID }, RawContacts.CONTACT_ID + "=" + existingContactId, null, null);
while (cur.moveToNext()) {
existingRawIds.add(cur.getLong(0));
}
cur.close();
Log.i("Join", "Found " + existingRawIds.size() + " raw-contact-ids");
List<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// go over all existing raw ids, and join with our new one
for (Long existingRawId : existingRawIds) {
Builder builder = ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI);
builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER);
builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, newRawContactId);
builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, existingRawId);
ops.add(builder.build());
}
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
}
UPDATE
The issue seems to he happening if two contacts are saved with different name but same phone number.Synching creates a separate contact for such contacts and aggregate app icon in both existing and separate contact.
from Separate contact being created despite using AggregationExceptions.TYPE_KEEP_TOGETHER
No comments:
Post a Comment