Code example for ContentValues

Methods: clearput, withValues

0
     * @param phone new phone number for the contact 
     * @param phoneType the type: cell, home, etc. 
     * @return instance of ContactOperations 
     */ 
    public ContactOperations addPhone(String phone, int phoneType) {
        mValues.clear();
        if (!TextUtils.isEmpty(phone)) {
            mValues.put(Phone.NUMBER, phone);
            mValues.put(Phone.TYPE, phoneType);
            mValues.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
            addInsertOp(); 
        } 
        return this;
    } 
 
    /** 
     * Adds a group membership 
     * 
     * @param id The id of the group to assign 
     * @return instance of ContactOperations 
     */ 
    public ContactOperations addGroupMembership(long groupId) {
        mValues.clear();
        mValues.put(GroupMembership.GROUP_ROW_ID, groupId);
        mValues.put(GroupMembership.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE);
        addInsertOp(); 
        return this;
    } 
 
    public ContactOperations addAvatar(String avatarUrl) {
        if (avatarUrl != null) {
            byte[] avatarBuffer = NetworkUtilities.downloadAvatar(avatarUrl);
            if (avatarBuffer != null) {
                mValues.clear();
                mValues.put(Photo.PHOTO, avatarBuffer);
                mValues.put(Photo.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
                addInsertOp(); 
            } 
        } 
        return this;
    } 
 
    /** 
     * Adds a profile action 
     * 
     * @param userId the userId of the sample SyncAdapter user object 
     * @return instance of ContactOperations 
     */ 
    public ContactOperations addProfileAction(long userId) {
        mValues.clear();
        if (userId != 0) {
            mValues.put(SampleSyncAdapterColumns.DATA_PID, userId);
            mValues.put(SampleSyncAdapterColumns.DATA_SUMMARY, mContext
                .getString(R.string.profile_action));
            mValues.put(SampleSyncAdapterColumns.DATA_DETAIL, mContext
                .getString(R.string.view_profile));
            mValues.put(Data.MIMETYPE, SampleSyncAdapterColumns.MIME_PROFILE);
            addInsertOp(); 
        } 
        return this;
    } 
 
    /** 
     * Updates contact's serverId 
     * 
     * @param serverId the serverId for this contact 
     * @param uri Uri for the existing raw contact to be updated 
     * @return instance of ContactOperations 
     */ 
    public ContactOperations updateServerId(long serverId, Uri uri) {
        mValues.clear();
        mValues.put(RawContacts.SOURCE_ID, serverId);
        addUpdateOp(uri);
        return this;
    } 
 
    /** 
     * Updates contact's email 
     * 
     * @param email email id of the sample SyncAdapter user 
     * @param uri Uri for the existing raw contact to be updated 
     * @return instance of ContactOperations 
     */ 
    public ContactOperations updateEmail(String email, String existingEmail, Uri uri) {
        if (!TextUtils.equals(existingEmail, email)) {
            mValues.clear();
            mValues.put(Email.DATA, email);
            addUpdateOp(uri);
        } 
        return this;
    } 
 
    /** 
     * Updates contact's name. The caller can either provide first-name 
     * and last-name fields or a full-name field. 
     * 
     * @param uri Uri for the existing raw contact to be updated 
     * @param existingFirstName the first name stored in provider 
     * @param existingLastName the last name stored in provider 
     * @param existingFullName the full name stored in provider 
     * @param firstName the new first name to store 
     * @param lastName the new last name to store 
     * @param fullName the new full name to store 
     * @return instance of ContactOperations 
     */ 
    public ContactOperations updateName(Uri uri,
        String existingFirstName,
        String existingLastName,
        String existingFullName,
        String firstName,
        String lastName,
        String fullName) {
 
        mValues.clear();
        if (TextUtils.isEmpty(fullName)) {
            if (!TextUtils.equals(existingFirstName, firstName)) {
                mValues.put(StructuredName.GIVEN_NAME, firstName);
            } 
            if (!TextUtils.equals(existingLastName, lastName)) {
                mValues.put(StructuredName.FAMILY_NAME, lastName);
            } 
        } else { 
            if (!TextUtils.equals(existingFullName, fullName)) {
                mValues.put(StructuredName.DISPLAY_NAME, fullName);
            } 
        } 
        if (mValues.size() > 0) {
            addUpdateOp(uri);
        } 
        return this;
    } 
 
    public ContactOperations updateDirtyFlag(boolean isDirty, Uri uri) {
        int isDirtyValue = isDirty ? 1 : 0;
        mValues.clear();
        mValues.put(RawContacts.DIRTY, isDirtyValue);
        addUpdateOp(uri);
        return this;
    } 
 
    /** 
     * Updates contact's phone 
     * 
     * @param existingNumber phone number stored in contacts provider 
     * @param phone new phone number for the contact 
     * @param uri Uri for the existing raw contact to be updated 
     * @return instance of ContactOperations 
     */ 
    public ContactOperations updatePhone(String existingNumber, String phone, Uri uri) {
        if (!TextUtils.equals(phone, existingNumber)) {
            mValues.clear();
            mValues.put(Phone.NUMBER, phone);
            addUpdateOp(uri);
        } 
        return this;
    } 
 
    public ContactOperations updateAvatar(String avatarUrl, Uri uri) {
        if (avatarUrl != null) {
            byte[] avatarBuffer = NetworkUtilities.downloadAvatar(avatarUrl);
            if (avatarBuffer != null) {
                mValues.clear();
                mValues.put(Photo.PHOTO, avatarBuffer);
                mValues.put(Photo.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
                addUpdateOp(uri);
            } 
        } 
        return this;
    } 
 
    /** 
     * Updates contact's profile action 
     * 
     * @param userId sample SyncAdapter user id 
     * @param uri Uri for the existing raw contact to be updated 
     * @return instance of ContactOperations 
     */ 
    public ContactOperations updateProfileAction(Integer userId, Uri uri) {
        mValues.clear();
        mValues.put(SampleSyncAdapterColumns.DATA_PID, userId);
        addUpdateOp(uri);
        return this;
    } 
 
    /** 
     * Adds an insert operation into the batch 
     */ 
    private void addInsertOp() { 
 
        if (!mIsNewContact) {
            mValues.put(Phone.RAW_CONTACT_ID, mRawContactId);
        } 
        ContentProviderOperation.Builder builder =
                newInsertCpo(Data.CONTENT_URI, mIsSyncOperation, mIsYieldAllowed);
        builder.withValues(mValues);
        if (mIsNewContact) {
            builder.withValueBackReference(Data.RAW_CONTACT_ID, mBackReference);
        } 
        mIsYieldAllowed = false;
        mBatchOperation.add(builder.build());