Example usage for android.content ContentProviderOperation newInsert

List of usage examples for android.content ContentProviderOperation newInsert

Introduction

In this page you can find the example usage for android.content ContentProviderOperation newInsert.

Prototype

public static Builder newInsert(Uri uri) 

Source Link

Document

Create a Builder suitable for building an insert ContentProviderOperation .

Usage

From source file:org.skt.runtime.html5apis.contacts.ContactAccessorSdk5.java

/**
 * Add an email to a list of database actions to be performed
 * //from  w w w . j av a2s. c o m
 * @param ops the list of database actions
 * @param email the item to be inserted
 */
private void insertEmail(ArrayList<ContentProviderOperation> ops, JSONObject email) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value"))
            .withValue(ContactsContract.CommonDataKinds.Email.TYPE, getEmailType(getJsonString(email, "type")))
            .build());
}

From source file:com.karura.framework.plugins.utils.ContactAccessorSdk5.java

/**
 * Add an address to a list of database actions to be performed
 * //from   w w  w.  ja  v a  2s .c o m
 * @param ops
 *            the list of database actions
 * @param address
 *            the item to be inserted
 */
private void insertAddress(ArrayList<ContentProviderOperation> ops, JSONObject address) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
                    getAddressType(getJsonString(address, "type")))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
                    getJsonString(address, "formatted"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET,
                    getJsonString(address, "streetAddress"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY,
                    getJsonString(address, "locality"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION,
                    getJsonString(address, "region"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
                    getJsonString(address, "postalCode"))
            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
                    getJsonString(address, "country"))
            .build());
}

From source file:com.karura.framework.plugins.utils.ContactAccessorSdk5.java

/**
 * Add an email to a list of database actions to be performed
 * //from  w  w  w . ja  va2  s.co  m
 * @param ops
 *            the list of database actions
 * @param email
 *            the item to be inserted
 */
private void insertEmail(ArrayList<ContentProviderOperation> ops, JSONObject email) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value"))
            .withValue(ContactsContract.CommonDataKinds.Email.TYPE,
                    getContactType(getJsonString(email, "type")))
            .build());
}

From source file:com.karura.framework.plugins.utils.ContactAccessorSdk5.java

/**
 * Add a phone to a list of database actions to be performed
 * //from   www. ja v  a2s .  com
 * @param ops
 *            the list of database actions
 * @param phone
 *            the item to be inserted
 */
private void insertPhone(ArrayList<ContentProviderOperation> ops, JSONObject phone) {
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, getJsonString(phone, "value"))
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, getPhoneType(getJsonString(phone, "type")))
            .build());
}

From source file:com.karura.framework.plugins.utils.ContactAccessorSdk5.java

/**
 * Add a phone to a list of database actions to be performed
 * /*from  w  ww . j a  v  a  2s.c om*/
 * @param ops
 *            the list of database actions
 * @param phone
 *            the item to be inserted
 */
private void insertPhoto(ArrayList<ContentProviderOperation> ops, JSONObject photo) {
    byte[] bytes = getPhotoBytes(getJsonString(photo, "value"));
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes).build());
}

From source file:org.skt.runtime.html5apis.contacts.ContactAccessorSdk5.java

/**
 * Creates a new contact and stores it in the database
 * //from   w w w. j a v  a2s. com
 * @param contact the contact to be saved
 * @param account the account to be saved under
 */
private String createNewContact(JSONObject contact, String accountType, String accountName) {
    // Create a list of attributes to add to the contact database
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    //Add contact type
    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName).build());

    // Add name
    try {
        JSONObject name = contact.optJSONObject("name");
        String displayName = contact.getString("displayName");
        if (displayName != null || name != null) {
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                            getJsonString(name, "familyName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
                            getJsonString(name, "middleName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                            getJsonString(name, "givenName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX,
                            getJsonString(name, "honorificPrefix"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
                            getJsonString(name, "honorificSuffix"))
                    .build());
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get name object");
    }

    //Add phone numbers
    JSONArray phones = null;
    try {
        phones = contact.getJSONArray("phoneNumbers");
        if (phones != null) {
            for (int i = 0; i < phones.length(); i++) {
                JSONObject phone = (JSONObject) phones.get(i);
                insertPhone(ops, phone);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get phone numbers");
    }

    // Add emails
    JSONArray emails = null;
    try {
        emails = contact.getJSONArray("emails");
        if (emails != null) {
            for (int i = 0; i < emails.length(); i++) {
                JSONObject email = (JSONObject) emails.get(i);
                insertEmail(ops, email);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Add addresses
    JSONArray addresses = null;
    try {
        addresses = contact.getJSONArray("addresses");
        if (addresses != null) {
            for (int i = 0; i < addresses.length(); i++) {
                JSONObject address = (JSONObject) addresses.get(i);
                insertAddress(ops, address);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get addresses");
    }

    // Add organizations
    JSONArray organizations = null;
    try {
        organizations = contact.getJSONArray("organizations");
        if (organizations != null) {
            for (int i = 0; i < organizations.length(); i++) {
                JSONObject org = (JSONObject) organizations.get(i);
                insertOrganization(ops, org);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get organizations");
    }

    // Add IMs
    JSONArray ims = null;
    try {
        ims = contact.getJSONArray("ims");
        if (ims != null) {
            for (int i = 0; i < ims.length(); i++) {
                JSONObject im = (JSONObject) ims.get(i);
                insertIm(ops, im);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Add note
    String note = getJsonString(contact, "note");
    if (note != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build());
    }

    // Add nickname
    String nickname = getJsonString(contact, "nickname");
    if (nickname != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build());
    }

    // Add urls 
    JSONArray websites = null;
    try {
        websites = contact.getJSONArray("websites");
        if (websites != null) {
            for (int i = 0; i < websites.length(); i++) {
                JSONObject website = (JSONObject) websites.get(i);
                insertWebsite(ops, website);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get websites");
    }

    // Add birthday
    String birthday = getJsonString(contact, "birthday");
    if (birthday != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Event.TYPE,
                        ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
                .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build());
    }

    // Add photos
    JSONArray photos = null;
    try {
        photos = contact.getJSONArray("photos");
        if (photos != null) {
            for (int i = 0; i < photos.length(); i++) {
                JSONObject photo = (JSONObject) photos.get(i);
                insertPhoto(ops, photo);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get photos");
    }

    String newId = null;
    //Add contact
    try {
        ContentProviderResult[] cpResults = mApp.getContentResolver().applyBatch(ContactsContract.AUTHORITY,
                ops);
        if (cpResults.length >= 0) {
            newId = cpResults[0].uri.getLastPathSegment();
        }
    } catch (RemoteException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    } catch (OperationApplicationException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return newId;
}

From source file:com.karura.framework.plugins.utils.ContactAccessorSdk5.java

/**
 * Creates a new contact and stores it in the database
 * /*from   ww  w  . j  ava  2s  .  c  o  m*/
 * @param contact
 *            the contact to be saved
 * @param account
 *            the account to be saved under
 */
private String createNewContact(JSONObject contact, String accountType, String accountName) {
    // Create a list of attributes to add to the contact database
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    // Add contact type
    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName).build());

    // Add name
    try {
        JSONObject name = contact.optJSONObject("name");
        String displayName = contact.getString("displayName");
        if (displayName != null || name != null) {
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                            getJsonString(name, "familyName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
                            getJsonString(name, "middleName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                            getJsonString(name, "givenName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX,
                            getJsonString(name, "honorificPrefix"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
                            getJsonString(name, "honorificSuffix"))
                    .build());
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get name object");
    }

    // Add phone numbers
    JSONArray phones = null;
    try {
        phones = contact.getJSONArray("phoneNumbers");
        if (phones != null) {
            for (int i = 0; i < phones.length(); i++) {
                JSONObject phone = (JSONObject) phones.get(i);
                insertPhone(ops, phone);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get phone numbers");
    }

    // Add emails
    JSONArray emails = null;
    try {
        emails = contact.getJSONArray("emails");
        if (emails != null) {
            for (int i = 0; i < emails.length(); i++) {
                JSONObject email = (JSONObject) emails.get(i);
                insertEmail(ops, email);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Add addresses
    JSONArray addresses = null;
    try {
        addresses = contact.getJSONArray("addresses");
        if (addresses != null) {
            for (int i = 0; i < addresses.length(); i++) {
                JSONObject address = (JSONObject) addresses.get(i);
                insertAddress(ops, address);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get addresses");
    }

    // Add organizations
    JSONArray organizations = null;
    try {
        organizations = contact.getJSONArray("organizations");
        if (organizations != null) {
            for (int i = 0; i < organizations.length(); i++) {
                JSONObject org = (JSONObject) organizations.get(i);
                insertOrganization(ops, org);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get organizations");
    }

    // Add IMs
    JSONArray ims = null;
    try {
        ims = contact.getJSONArray("ims");
        if (ims != null) {
            for (int i = 0; i < ims.length(); i++) {
                JSONObject im = (JSONObject) ims.get(i);
                insertIm(ops, im);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Add note
    String note = getJsonString(contact, "note");
    if (note != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build());
    }

    // Add nickname
    String nickname = getJsonString(contact, "nickname");
    if (nickname != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build());
    }

    // Add urls
    JSONArray websites = null;
    try {
        websites = contact.getJSONArray("urls");
        if (websites != null) {
            for (int i = 0; i < websites.length(); i++) {
                JSONObject website = (JSONObject) websites.get(i);
                insertWebsite(ops, website);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get websites");
    }

    // Add birthday
    String birthday = getJsonString(contact, "birthday");
    if (birthday != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Event.TYPE,
                        ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
                .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build());
    }

    // Add photos
    JSONArray photos = null;
    try {
        photos = contact.getJSONArray("photos");
        if (photos != null) {
            for (int i = 0; i < photos.length(); i++) {
                JSONObject photo = (JSONObject) photos.get(i);
                insertPhoto(ops, photo);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get photos");
    }

    String newId = null;
    // Add contact
    try {
        ContentProviderResult[] cpResults = getContext().getContentResolver()
                .applyBatch(ContactsContract.AUTHORITY, ops);
        if (cpResults.length >= 0) {
            newId = cpResults[0].uri.getLastPathSegment();
        }
    } catch (RemoteException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    } catch (OperationApplicationException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return newId;
}

From source file:com.remobile.contacts.ContactAccessorSdk5.java

/**
 * Creates a new contact and stores it in the database
 *
 * @param contact the contact to be saved
 * @param account the account to be saved under
 *//*ww w.j av  a  2s.com*/
private String createNewContact(JSONObject contact, String accountType, String accountName) {
    // Create a list of attributes to add to the contact database
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    //Add contact type
    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName).build());

    // Add name
    try {
        JSONObject name = contact.optJSONObject("name");
        String displayName = contact.getString("displayName");
        if (displayName != null || name != null) {
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                            getJsonString(name, "familyName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
                            getJsonString(name, "middleName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                            getJsonString(name, "givenName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX,
                            getJsonString(name, "honorificPrefix"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
                            getJsonString(name, "honorificSuffix"))
                    .build());
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get name object");
    }

    //Add phone numbers
    JSONArray phones = null;
    try {
        phones = contact.getJSONArray("phoneNumbers");
        if (phones != null) {
            for (int i = 0; i < phones.length(); i++) {
                JSONObject phone = (JSONObject) phones.get(i);
                insertPhone(ops, phone);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get phone numbers");
    }

    // Add emails
    JSONArray emails = null;
    try {
        emails = contact.getJSONArray("emails");
        if (emails != null) {
            for (int i = 0; i < emails.length(); i++) {
                JSONObject email = (JSONObject) emails.get(i);
                insertEmail(ops, email);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Add addresses
    JSONArray addresses = null;
    try {
        addresses = contact.getJSONArray("addresses");
        if (addresses != null) {
            for (int i = 0; i < addresses.length(); i++) {
                JSONObject address = (JSONObject) addresses.get(i);
                insertAddress(ops, address);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get addresses");
    }

    // Add organizations
    JSONArray organizations = null;
    try {
        organizations = contact.getJSONArray("organizations");
        if (organizations != null) {
            for (int i = 0; i < organizations.length(); i++) {
                JSONObject org = (JSONObject) organizations.get(i);
                insertOrganization(ops, org);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get organizations");
    }

    // Add IMs
    JSONArray ims = null;
    try {
        ims = contact.getJSONArray("ims");
        if (ims != null) {
            for (int i = 0; i < ims.length(); i++) {
                JSONObject im = (JSONObject) ims.get(i);
                insertIm(ops, im);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Add note
    String note = getJsonString(contact, "note");
    if (note != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build());
    }

    // Add nickname
    String nickname = getJsonString(contact, "nickname");
    if (nickname != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build());
    }

    // Add urls
    JSONArray websites = null;
    try {
        websites = contact.getJSONArray("urls");
        if (websites != null) {
            for (int i = 0; i < websites.length(); i++) {
                JSONObject website = (JSONObject) websites.get(i);
                insertWebsite(ops, website);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get websites");
    }

    // Add birthday
    String birthday = getJsonString(contact, "birthday");
    if (birthday != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Event.TYPE,
                        ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
                .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build());
    }

    // Add photos
    JSONArray photos = null;
    try {
        photos = contact.getJSONArray("photos");
        if (photos != null) {
            for (int i = 0; i < photos.length(); i++) {
                JSONObject photo = (JSONObject) photos.get(i);
                insertPhoto(ops, photo);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get photos");
    }

    String newId = null;
    //Add contact
    try {
        ContentProviderResult[] cpResults = mApp.getActivity().getContentResolver()
                .applyBatch(ContactsContract.AUTHORITY, ops);
        if (cpResults.length >= 0) {
            newId = cpResults[0].uri.getLastPathSegment();
        }
    } catch (RemoteException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    } catch (OperationApplicationException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return newId;
}

From source file:com.silentcircle.vcard.VCardEntry.java

/**
 * Constructs the list of insert operation for this object. When the operationList argument is null, this method creates a new
 * ArrayList and return it. The returned object is filled with new insert operations for this object. When operationList
 * argument is not null, this method appends those new operations into the object instead of creating a new ArrayList.
 *
 * @param resolver/*  w w w.ja  v  a  2s .  co m*/
 *            {@link android.content.ContentResolver} object to be used in this method.
 * @param operationList
 *            object to be filled. You can use this argument to concatinate operation lists. If null, this method creates a
 *            new array object.
 * @return If operationList argument is null, new object with new insert operations. If it is not null, the operationList
 *         object with operations inserted by this method.
 */
public ArrayList<ContentProviderOperation> constructInsertOperations(ContentResolver resolver,
        ArrayList<ContentProviderOperation> operationList) {
    if (operationList == null) {
        operationList = new ArrayList<ContentProviderOperation>();
    }

    if (isIgnorable()) {
        return operationList;
    }

    final int backReferenceIndex = operationList.size();

    // After applying the batch the first result's Uri is returned so it is important that
    // the RawContact is the first operation that gets inserted into the list.
    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
    // if (mAccount != null) {
    // builder.withValue(RawContacts.ACCOUNT_NAME, mAccount.name);
    // builder.withValue(RawContacts.ACCOUNT_TYPE, mAccount.type);
    // } else {
    // builder.withValue(RawContacts.ACCOUNT_NAME, null);
    // builder.withValue(RawContacts.ACCOUNT_TYPE, null);
    // }
    builder.withValue(RawContacts.DISPLAY_NAME_PRIMARY, null);
    operationList.add(builder.build());

    int start = operationList.size();
    iterateAllData(new InsertOperationConstrutor(operationList, backReferenceIndex));
    int end = operationList.size();

    return operationList;
}