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.kontalk.sync.Syncer.java

private void addContactData(String username, String phone, List<ContentProviderOperation> operations,
        int index) {
    ContentProviderOperation.Builder builder;
    final int opIndex = index * 3;

    // create a Data record of common type 'StructuredName' for our RawContact
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, opIndex)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, username);
    operations.add(builder.build());/*from w  w w  . j  ava2 s.c  om*/

    // create a Data record of custom type 'org.kontalk.user' to display a link to the conversation
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, opIndex)
            .withValue(ContactsContract.Data.MIMETYPE, Users.CONTENT_ITEM_TYPE)
            .withValue(DATA_COLUMN_DISPLAY_NAME, username)
            .withValue(DATA_COLUMN_ACCOUNT_NAME, mContext.getString(R.string.app_name))
            .withValue(DATA_COLUMN_PHONE, phone).withYieldAllowed(true);
    operations.add(builder.build());
}

From source file:net.ddns.mlsoftlaberge.contactslist.ui.ContactAdminFragment.java

public void insertnote() {
    normalizememo();//from  w  ww  .  j  a  v  a2  s  .  c  o m
    newnote = notememo.toString() + notereformat.toString();
    try {
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI).withValue(Data.RAW_CONTACT_ID, mNotesRawId)
                .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
                .withValue(Data.DATA1, newnote).build());
        getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        Toast.makeText(getActivity(), "Transaction Inserted", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getActivity(), "Transaction Not Inserted", Toast.LENGTH_SHORT).show();
        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

From source file:at.bitfire.vcard4android.AndroidContact.java

protected void insertIMPP(BatchOperation batch, Impp impp) {
    int typeCode = Im.TYPE_OTHER; // default value
    String typeLabel = null;//from   w  w w. j  av a 2s  .c o  m

    for (ImppType type : impp.getTypes())
        if (type == ImppType.HOME) {
            typeCode = Im.TYPE_HOME;
            break;
        } else if (type == ImppType.WORK || type == ImppType.BUSINESS) {
            typeCode = Im.TYPE_WORK;
            break;
        }

    if (typeCode == Im.TYPE_OTHER) // still default value?
        if (!impp.getTypes().isEmpty()) {
            typeCode = Im.TYPE_CUSTOM;
            typeLabel = xNameToLabel(impp.getTypes().iterator().next().getValue());
        }

    String protocol = impp.getProtocol();
    if (protocol == null) {
        Constants.log.warn("Ignoring IMPP address without protocol");
        return;
    }

    int protocolCode = 0;
    String protocolLabel = null;

    // SIP addresses are IMPP entries in the VCard but locally stored in SipAddress rather than Im
    boolean sipAddress = false;

    if (impp.isAim())
        protocolCode = Im.PROTOCOL_AIM;
    else if (impp.isMsn())
        protocolCode = Im.PROTOCOL_MSN;
    else if (impp.isYahoo())
        protocolCode = Im.PROTOCOL_YAHOO;
    else if (impp.isSkype())
        protocolCode = Im.PROTOCOL_SKYPE;
    else if (protocol.equalsIgnoreCase("qq"))
        protocolCode = Im.PROTOCOL_QQ;
    else if (protocol.equalsIgnoreCase("google-talk"))
        protocolCode = Im.PROTOCOL_GOOGLE_TALK;
    else if (impp.isIcq())
        protocolCode = Im.PROTOCOL_ICQ;
    else if (impp.isXmpp() || protocol.equalsIgnoreCase("jabber"))
        protocolCode = Im.PROTOCOL_JABBER;
    else if (protocol.equalsIgnoreCase("netmeeting"))
        protocolCode = Im.PROTOCOL_NETMEETING;
    else if (protocol.equalsIgnoreCase("sip"))
        sipAddress = true;
    else {
        protocolCode = Im.PROTOCOL_CUSTOM;
        protocolLabel = protocol;
    }

    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(dataSyncURI());
    if (id == null)
        builder.withValueBackReference(Im.RAW_CONTACT_ID, 0);
    else
        builder.withValue(Im.RAW_CONTACT_ID, id);
    if (sipAddress)
        // save as SIP address
        builder.withValue(SipAddress.MIMETYPE, SipAddress.CONTENT_ITEM_TYPE)
                .withValue(SipAddress.DATA, impp.getHandle()).withValue(SipAddress.TYPE, typeCode)
                .withValue(SipAddress.LABEL, typeLabel);
    else {
        // save as IM address
        builder.withValue(Im.MIMETYPE, Im.CONTENT_ITEM_TYPE).withValue(Im.DATA, impp.getHandle())
                .withValue(Im.TYPE, typeCode).withValue(Im.LABEL, typeLabel)
                .withValue(Im.PROTOCOL, protocolCode).withValue(Im.CUSTOM_PROTOCOL, protocolLabel);
    }
    batch.enqueue(builder.build());
}

From source file:org.linphone.ContactsManager.java

public void createNewContact(ArrayList<ContentProviderOperation> ops, String firstName, String lastName,
        Bitmap mBitmap) {// w  w w. ja  v  a2  s  .c om
    int rawContactID = 0;

    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.AGGREGATION_MODE,
                    ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

    if (getDisplayName(lastName, firstName) != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, lastName)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, firstName)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                        getDisplayName(lastName, firstName))
                .build());
    }

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    android.util.Log.e("BITMAP", "BITMAP NULL: " + String.valueOf(mBitmap == null));
    if (mBitmap != null) {

        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        // Adding insert operation to operations list
        // to insert Photo in the table ContactsContract.Data
        //android.util.Log.e("", "ADDDING PHOTOO");
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, stream.toByteArray()).build());
        try {
            stream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:at.bitfire.vcard4android.AndroidContact.java

protected void insertNickname(BatchOperation batch) {
    ezvcard.property.Nickname nick = contact.nickName;
    if (nick != null && !nick.getValues().isEmpty()) {
        int typeCode = Nickname.TYPE_DEFAULT;
        String typeLabel = null;/*from   w  w w  . j a v  a  2s  .c o m*/

        String type = nick.getType();
        if (!TextUtils.isEmpty(type))
            switch (type) {
            case Contact.NICKNAME_TYPE_MAIDEN_NAME:
                typeCode = Nickname.TYPE_MAIDEN_NAME;
                break;
            case Contact.NICKNAME_TYPE_SHORT_NAME:
                typeCode = Nickname.TYPE_SHORT_NAME;
                break;
            case Contact.NICKNAME_TYPE_INITIALS:
                typeCode = Nickname.TYPE_INITIALS;
                break;
            case Contact.NICKNAME_TYPE_OTHER_NAME:
                typeCode = Nickname.TYPE_OTHER_NAME;
                break;
            default:
                typeCode = Nickname.TYPE_CUSTOM;
                typeLabel = xNameToLabel(type);
            }

        ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(dataSyncURI());
        if (id == null)
            builder.withValueBackReference(Nickname.RAW_CONTACT_ID, 0);
        else
            builder.withValue(Nickname.RAW_CONTACT_ID, id);
        builder.withValue(Nickname.MIMETYPE, Nickname.CONTENT_ITEM_TYPE)
                .withValue(Nickname.NAME, nick.getValues().get(0)).withValue(Nickname.TYPE, typeCode)
                .withValue(Nickname.LABEL, typeLabel);
        batch.enqueue(builder.build());
    }
}

From source file:at.bitfire.vcard4android.AndroidContact.java

protected void insertNote(BatchOperation batch) {
    if (!TextUtils.isEmpty(contact.note)) {
        ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(dataSyncURI());
        if (id == null)
            builder.withValueBackReference(Note.RAW_CONTACT_ID, 0);
        else/*w  w w.  j  a v  a 2s . c o  m*/
            builder.withValue(Note.RAW_CONTACT_ID, id);
        builder.withValue(Note.MIMETYPE, Note.CONTENT_ITEM_TYPE).withValue(Note.NOTE, contact.note);
        batch.enqueue(builder.build());
    }
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Creates a new contact and stores it in the database
 * /*from ww w  . j  a  va 2 s . c o m*/
 * @param id the raw contact id which is required for linking items to the contact
 * @param contact the contact to be saved
 * @param account the account to be saved under
 */
private boolean modifyContact(String id, JSONObject contact, Account account) {
    // Get the RAW_CONTACT_ID which is needed to insert new values in an already existing contact.
    // But not needed to update existing values.
    int rawId = (new Integer(getJsonString(contact, "rawId"))).intValue();

    // Create a list of attributes to add to the contact database
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    //Add contact type
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name).build());

    // Modify name
    JSONObject name;
    try {
        String displayName = getJsonString(contact, "displayName");
        name = contact.getJSONObject("name");
        if (displayName != null || name != null) {
            ContentProviderOperation.Builder builder = ContentProviderOperation
                    .newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
                                    + "=?",
                            new String[] { id,
                                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE });

            if (displayName != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
            }

            String familyName = getJsonString(name, "familyName");
            if (familyName != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName);
            }
            String middleName = getJsonString(name, "middleName");
            if (middleName != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName);
            }
            String givenName = getJsonString(name, "givenName");
            if (givenName != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName);
            }
            String honorificPrefix = getJsonString(name, "honorificPrefix");
            if (honorificPrefix != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, honorificPrefix);
            }
            String honorificSuffix = getJsonString(name, "honorificSuffix");
            if (honorificSuffix != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, honorificSuffix);
            }

            ops.add(builder.build());
        }
    } catch (JSONException e1) {
        Log.d(LOG_TAG, "Could not get name");
    }

    // Modify 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);
                String phoneId = getJsonString(phone, "id");
                // This is a new phone so do a DB insert
                if (phoneId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER,
                            getJsonString(phone, "value"));
                    contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE,
                            getPhoneType(getJsonString(phone, "type")));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing phone so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.Phone._ID + "=? AND "
                                    + ContactsContract.Data.MIMETYPE + "=?",
                            new String[] { phoneId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                                    getJsonString(phone, "value"))
                            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                                    getPhoneType(getJsonString(phone, "type")))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get phone numbers");
    }

    // Modify 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);
                String emailId = getJsonString(email, "id");
                // This is a new email so do a DB insert
                if (emailId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Email.DATA,
                            getJsonString(email, "value"));
                    contentValues.put(ContactsContract.CommonDataKinds.Email.TYPE,
                            getContactType(getJsonString(email, "type")));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing email so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.Email._ID + "=? AND "
                                    + ContactsContract.Data.MIMETYPE + "=?",
                            new String[] { emailId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Email.DATA,
                                    getJsonString(email, "value"))
                            .withValue(ContactsContract.CommonDataKinds.Email.TYPE,
                                    getContactType(getJsonString(email, "type")))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Modify 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);
                String addressId = getJsonString(address, "id");
                // This is a new address so do a DB insert
                if (addressId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
                            getJsonString(address, "formatted"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET,
                            getJsonString(address, "streetAddress"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY,
                            getJsonString(address, "locality"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION,
                            getJsonString(address, "region"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
                            getJsonString(address, "postalCode"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
                            getJsonString(address, "country"));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing address so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.StructuredPostal._ID + "=? AND "
                                    + ContactsContract.Data.MIMETYPE + "=?",
                            new String[] { addressId,
                                    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_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());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get addresses");
    }

    // Modify 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);
                String orgId = getJsonString(org, "id");
                ;
                // This is a new organization so do a DB insert
                if (orgId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Organization.DEPARTMENT,
                            getJsonString(org, "department"));
                    contentValues.put(ContactsContract.CommonDataKinds.Organization.COMPANY,
                            getJsonString(org, "name"));
                    contentValues.put(ContactsContract.CommonDataKinds.Organization.TITLE,
                            getJsonString(org, "title"));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing organization so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(
                                    ContactsContract.CommonDataKinds.Organization._ID + "=? AND "
                                            + ContactsContract.Data.MIMETYPE + "=?",
                                    new String[] { orgId,
                                            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT,
                                    getJsonString(org, "department"))
                            .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY,
                                    getJsonString(org, "name"))
                            .withValue(ContactsContract.CommonDataKinds.Organization.TITLE,
                                    getJsonString(org, "title"))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get organizations");
    }

    // Modify 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);
                String imId = getJsonString(im, "id");
                ;
                // This is a new IM so do a DB insert
                if (imId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"));
                    contentValues.put(ContactsContract.CommonDataKinds.Im.TYPE,
                            getContactType(getJsonString(im, "type")));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing IM so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.Im._ID + "=? AND " + ContactsContract.Data.MIMETYPE
                                    + "=?",
                            new String[] { imId, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"))
                            .withValue(ContactsContract.CommonDataKinds.Im.TYPE,
                                    getContactType(getJsonString(im, "type")))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Modify note
    String note = getJsonString(contact, "note");
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?",
                    new String[] { id, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE })
            .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build());

    // Modify nickname
    String nickname = getJsonString(contact, "nickname");
    if (nickname != null) {
        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(
                        ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?",
                        new String[] { id, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE })
                .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build());
    }

    // Modify 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);
                String websiteId = getJsonString(website, "id");
                ;
                // This is a new website so do a DB insert
                if (websiteId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Website.DATA,
                            getJsonString(website, "value"));
                    contentValues.put(ContactsContract.CommonDataKinds.Website.TYPE,
                            getContactType(getJsonString(website, "type")));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing website so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(
                                    ContactsContract.CommonDataKinds.Website._ID + "=? AND "
                                            + ContactsContract.Data.MIMETYPE + "=?",
                                    new String[] { websiteId,
                                            ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Website.DATA,
                                    getJsonString(website, "value"))
                            .withValue(ContactsContract.CommonDataKinds.Website.TYPE,
                                    getContactType(getJsonString(website, "type")))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get websites");
    }

    // Modify birthday
    String birthday = getJsonString(contact, "birthday");
    if (birthday != null) {
        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(
                        ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
                                + "=? AND " + ContactsContract.CommonDataKinds.Event.TYPE + "=?",
                        new String[] { id, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
                                new String("" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) })
                .withValue(ContactsContract.CommonDataKinds.Event.TYPE,
                        ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
                .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build());
    }

    // Modify 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);
                String photoId = getJsonString(photo, "id");
                byte[] bytes = getPhotoBytes(getJsonString(photo, "value"));
                // This is a new photo so do a DB insert
                if (photoId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
                    contentValues.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes);

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing photo so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.Photo._ID + "=? AND "
                                    + ContactsContract.Data.MIMETYPE + "=?",
                            new String[] { photoId, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                            .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes).build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get photos");
    }

    boolean retVal = true;

    //Modify contact
    try {
        mApp.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        Log.e(LOG_TAG, Log.getStackTraceString(e), e);
        retVal = false;
    } catch (OperationApplicationException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        Log.e(LOG_TAG, Log.getStackTraceString(e), e);
        retVal = false;
    }

    return retVal;
}

From source file:at.bitfire.vcard4android.AndroidContact.java

protected void insertStructuredPostal(BatchOperation batch, Address address) {
    /*   street po.box (extended)
     *   postcode city//from w w w  .j a  va2 s.c  o  m
     *   region
     *   COUNTRY
     */
    String formattedAddress = address.getLabel();
    if (!TextUtils.isEmpty(formattedAddress)) {
        String lineStreet = StringUtils.join(
                new String[] { address.getStreetAddress(), address.getPoBox(), address.getExtendedAddress() },
                " "),
                lineLocality = StringUtils.join(new String[] { address.getPostalCode(), address.getLocality() },
                        " ");

        List<String> lines = new LinkedList<>();
        if (!TextUtils.isEmpty(lineStreet))
            lines.add(lineStreet);
        if (!TextUtils.isEmpty(lineLocality))
            lines.add(lineLocality);
        if (!TextUtils.isEmpty(address.getRegion()))
            lines.add(address.getRegion());
        if (!TextUtils.isEmpty(address.getCountry()))
            lines.add(address.getCountry().toUpperCase(Locale.getDefault()));

        formattedAddress = StringUtils.join(lines, "\n");
    }

    int typeCode = StructuredPostal.TYPE_OTHER;
    String typeLabel = null;

    for (AddressType type : address.getTypes())
        if (type == AddressType.HOME) {
            typeCode = StructuredPostal.TYPE_HOME;
            break;
        } else if (type == AddressType.WORK) {
            typeCode = StructuredPostal.TYPE_WORK;
            break;
        }

    if (typeCode == StructuredPostal.TYPE_OTHER) // still default value?
        if (!address.getTypes().isEmpty()) {
            typeCode = StructuredPostal.TYPE_CUSTOM;
            typeLabel = xNameToLabel(address.getTypes().iterator().next().getValue());
        }

    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(dataSyncURI());
    if (id == null)
        builder.withValueBackReference(StructuredPostal.RAW_CONTACT_ID, 0);
    else
        builder.withValue(StructuredPostal.RAW_CONTACT_ID, id);
    builder.withValue(StructuredPostal.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE)
            .withValue(StructuredPostal.FORMATTED_ADDRESS, formattedAddress)
            .withValue(StructuredPostal.TYPE, typeCode).withValue(StructuredPostal.LABEL, typeLabel)
            .withValue(StructuredPostal.STREET, address.getStreetAddress())
            .withValue(StructuredPostal.POBOX, address.getPoBox())
            .withValue(StructuredPostal.NEIGHBORHOOD, address.getExtendedAddress())
            .withValue(StructuredPostal.CITY, address.getLocality())
            .withValue(StructuredPostal.REGION, address.getRegion())
            .withValue(StructuredPostal.POSTCODE, address.getPostalCode())
            .withValue(StructuredPostal.COUNTRY, address.getCountry());
    batch.enqueue(builder.build());
}

From source file:com.android.contacts.ContactSaveService.java

private void addMembersToGroup(ContentResolver resolver, long[] rawContactsToAdd, long groupId) {
    if (rawContactsToAdd == null) {
        return;//w  w w  . j  a  v  a2s  .  co m
    }
    for (long rawContactId : rawContactsToAdd) {
        try {
            final ArrayList<ContentProviderOperation> rawContactOperations = new ArrayList<ContentProviderOperation>();

            // Build an assert operation to ensure the contact is not already in the group
            final ContentProviderOperation.Builder assertBuilder = ContentProviderOperation
                    .newAssertQuery(Data.CONTENT_URI);
            assertBuilder.withSelection(
                    Data.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID
                            + "=?",
                    new String[] { String.valueOf(rawContactId), GroupMembership.CONTENT_ITEM_TYPE,
                            String.valueOf(groupId) });
            assertBuilder.withExpectedCount(0);
            rawContactOperations.add(assertBuilder.build());

            // Build an insert operation to add the contact to the group
            final ContentProviderOperation.Builder insertBuilder = ContentProviderOperation
                    .newInsert(Data.CONTENT_URI);
            insertBuilder.withValue(Data.RAW_CONTACT_ID, rawContactId);
            insertBuilder.withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE);
            insertBuilder.withValue(GroupMembership.GROUP_ROW_ID, groupId);
            rawContactOperations.add(insertBuilder.build());

            if (DEBUG) {
                for (ContentProviderOperation operation : rawContactOperations) {
                    Log.v(TAG, operation.toString());
                }
            }

            // Apply batch
            if (!rawContactOperations.isEmpty()) {
                resolver.applyBatch(ContactsContract.AUTHORITY, rawContactOperations);
            }
        } catch (RemoteException e) {
            // Something went wrong, bail without success
            FeedbackHelper.sendFeedback(this, TAG,
                    "Problem persisting user edits for raw contact ID " + String.valueOf(rawContactId), e);
        } catch (OperationApplicationException e) {
            // The assert could have failed because the contact is already in the group,
            // just continue to the next contact
            FeedbackHelper.sendFeedback(this, TAG, "Assert failed in adding raw contact ID "
                    + String.valueOf(rawContactId) + ". Already exists in group " + String.valueOf(groupId), e);
        }
    }
}

From source file:at.bitfire.vcard4android.AndroidContact.java

protected void insertWebsite(BatchOperation batch, Url url) {
    int typeCode = Website.TYPE_OTHER;
    String typeLabel = null;/*from  w ww. j av  a 2 s.  com*/

    String type = url.getType();
    if (!TextUtils.isEmpty(type))
        switch (type) {
        case Contact.URL_TYPE_HOMEPAGE:
            typeCode = Website.TYPE_HOMEPAGE;
            break;
        case Contact.URL_TYPE_BLOG:
            typeCode = Website.TYPE_BLOG;
            break;
        case Contact.URL_TYPE_PROFILE:
            typeCode = Website.TYPE_PROFILE;
            break;
        case "home":
            typeCode = Website.TYPE_HOME;
            break;
        case "work":
            typeCode = Website.TYPE_WORK;
            break;
        case Contact.URL_TYPE_FTP:
            typeCode = Website.TYPE_FTP;
            break;
        default:
            typeCode = Website.TYPE_CUSTOM;
            typeLabel = xNameToLabel(type);
        }

    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(dataSyncURI());
    if (id == null)
        builder.withValueBackReference(Website.RAW_CONTACT_ID, 0);
    else
        builder.withValue(Website.RAW_CONTACT_ID, id);
    builder.withValue(Website.MIMETYPE, Website.CONTENT_ITEM_TYPE).withValue(Website.URL, url.getValue())
            .withValue(Website.TYPE, typeCode).withValue(Website.LABEL, typeLabel);
    batch.enqueue(builder.build());
}