Home » Android » unit testing – Deleting (Raw) Contacts in Android 2.x

unit testing – Deleting (Raw) Contacts in Android 2.x

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am working on Unit Tests for my Android app, and am doing a lot with Contacts. I have to insert contacts into the Android Content Providers, and delete them after running my tests. Trouble is, they do not get actually deleted:

Insertion:

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

int backRefIndex = 0;

Random r = new Random();

    contactOps.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                                           .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                                           .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) 
                                           .build());      
    contactOps.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                           .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRefIndex)
                                           .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                                           .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name" + r.nextInt())
                                           .build());

    contactOps.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRefIndex)
                       .withValue(ContactsContract.CommonDataKinds.Phone.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "020" + r.nextInt())
                       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, r.nextInt(20)
                       .build());

    try {
        ContentProviderResult[] result = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, contactOps);
    } catch (Exception e) {
        e.printStackTrace();
    }

Deletion method 1 (returns number of raw contacts, but they do not actually get deleted):

int deletedRawContacts = context.getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts._ID + " >= ?", new String[]{"0"});

Deletion method 2 (same result as deletion method 1, but different approach):

private static int deleteAllRawContacts(Context context) {
    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.RawContacts.CONTENT_URI, null, null, null, null);
    int count = 0;
    while (cur.moveToNext()) {
        try {
            String contactId = cur.getString(cur.getColumnIndex(ContactsContract.RawContacts._ID));
                count += cr.delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts._ID + " = ?", new String[]{contactId});
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
    }
    return count;
}

The deletion method for Contacts works, but the deletion method for Raw Contacts will return a false value. It will “tell” me, that it deleted all contacts, but when I run my next test case, the old Raw Contacts can still be found (i.e. the count of inserted contacts vs. present contacts is wrong). Note: All testing is done on the Android emulator.

Any ideas how to solve this?

I saw a similar question here: How to delete a contact? – but the solution does not seem to solve the given problem either.

How to&Answers:

As wiseideal already mentioned the way you delete your rawcontacts will only set the “deleted”-flag to 1.

What you need to do is to set the caller_is_syncadapter-flag in your URI to true like this:

RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build()

And then use this new URI to call the delete-method:

int deletedRawContacts = context.getContentResolver().delete(RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(), ContactsContract.RawContacts._ID + " >= ?", new String[]{"0"});

The corresponding part in the documentation is here (Operations->delete).

Hope this helps and happy coding 🙂

Answer:

I am working on the same issue.I found the delete column is setted to 1 once I “delete” it.So I think contentresolver doesnt delete the rawcontact data physically,it just set a delete flag.Maybe we should avoid these tag.