tags:

views:

131

answers:

1

Hello,

I met this problem at writing contacts by API for Android 2.0 or greater. Each time I write the same contact which already exist in my account (Google account) I got some part of contact aggregated ok but other did not. For example fields like FN, N, ORG, TITLE always are in one copy but TEL, EMAIL, ADR are added extra so after 2nd writing the same contact I have 2 copy the same TEL or EMAIL. How to force API engine to not repeate existed data ?

Code:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)                       
.build());

...
// adding phone number

ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
            builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
            builder.withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneValue);
            builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phoneType); // work/home
            builder.withValue(ContactsContract.CommonDataKinds.Phone.LABEL, phoneLabel);

            ops.add(builder.build());

...


try {
            contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (Exception e) {
            //
}

I tried add: AGGREGATION_MODE on AGGREGATION_MODE_DISABLED. but it changed nothing.

I will glad for any hint in this case.

BR, Bogus

A: 

I think for an existing contact you either do an update, or delete rows for Data that exist for that contact, before doing an insert As I understand from the code you have, you are inserting stuff rather than updating.

Pentium10
Yes I use insert action instead update but why only some fields are doubled, trippled, ... ?
Bogus
The Data table is separate from the standard contacts fields. Phones, emails, birthdays are stored in separate table. That's why only those fields gets doubled.
Pentium10
Thank you for your help.I changed newInsert() on newUpdate() and it works fine for already existed contacts otherwise nothing is written (phone book is empty).How does work aggregation? I would not like to compare each contact with a new one. I thought aggregation engine should do it itself - compare each filed and add some changes if need but not doubled their.I do not want to do it on my own.It seems very complex. I think there must be another solution.I hope.
Bogus
I have not used this builder class for working with contact providers. I always tried an update first, and checked the return count, and if that was zero, I made an insert. So probably you should do the same for your update, get the count or uri from ContentProviderResult and if zero do an insert.
Pentium10
I did as you said:(no contacts in google account)1. I did update.(none contact were written and it is right)2. I did insert.(3 contacts were wrttten ok)3. I did update again.After thas I found 1 changed contact instead 3.Some different aggregation was made. ContentProviderResult[] result has length =6and I check uri and count for each index.Every uri is null but count show difference values like 18, 24 etc but never 0.Hmm.. I realy do not know what is wrong now. You idea to do update first and then an insertis very good way but whyit now aggregate all into one contact ?
Bogus
This is out of scope for me, I don't have the experience with these classes to answer what you experience. I recommend posting a new questions explicitly telling in title the `ContentProviderOperation` class. Maybe someone with experience will spot it and answer it.
Pentium10