Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

Adding new connection to existing android contact

gyus.

I'm trying to add a connection to an existing contact, like viber does: enter image description here

It was pretty simple to add a contact with such connection following nemezis repo, but I haven't managed to update a contact in order to add a connection. I have tried:

ops.add(ContentProviderOperation.newUpdate(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
                    .withSelection(Data.RAW_CONTACT_ID + "= ?", new String[]{String.valueOf(id)})
                            //.withValue(Data.RAW_CONTACT_ID, id)
                    .withValue(Data.MIMETYPE, MIMETYPE_PROFILE)
                            //.withValue(Data.DATA1, 12345)
                    .withValue(Data.DATA2, "sample")
                    .withValue(Data.DATA3, "sample")
                    .build());

But it just doesn't work without errors. I tried a bunch of other options, but failed with them too, I doubt I should post all of them here. Any ideas, guys?

Thanks a lot.

Upvotes: 0

Views: 716

Answers (1)

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

Nevermind, guys. I have figured it out.

The android contact system uses matching. Moreover ACCOUNT_TYPE info is just read/write-once data. Therefore what you need to do is to create a new raw contact with the data matching with existing contact(using display name, phone number or email). Once you insert data android calls matching and aggregate the accounts. Piece of code:

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

            int backId = 0;
            ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(RawContacts.CONTENT_URI, true))
                    .withValue(RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME)
                    .withValue(RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE)
                    .build());

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

            ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
                    .withValueBackReference(Data.RAW_CONTACT_ID, backId)
                    .withValue(Data.MIMETYPE, MIMETYPE)
                            //.withValue(Data.DATA1, 12345)
                    .withValue(Data.DATA2, "data2")
                    .withValue(Data.DATA3, "data3")
                    .build());

            try {
                context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (Exception e) {
                e.printStackTrace();
            }

Upvotes: 2

Related Questions