Code example for ContentValues

Methods: clearputsize, withValues

0
		mIsNewContact = false;
		mRawContactId = rawContactId;
	} 
	 
	public ContactOperations addName(String firstName, String lastName) {
		mValues.clear();
		 
		if (!TextUtils.isEmpty(firstName)) {
			mValues.put(StructuredName.GIVEN_NAME, firstName);
			mValues.put(StructuredName.MIMETYPE,
					StructuredName.CONTENT_ITEM_TYPE);
		} 
		if (!TextUtils.isEmpty(lastName)) {
			mValues.put(StructuredName.FAMILY_NAME, lastName);
			mValues.put(StructuredName.MIMETYPE,
					StructuredName.CONTENT_ITEM_TYPE);
		} 
		if (mValues.size() > 0) {
			addInsertOp(); 
		} 
		return this;
	} 
	 
	public ContactOperations addEmail(String email) {
		mValues.clear();
		if (!TextUtils.isEmpty(email)) {
			mValues.put(Email.DATA, email);
			mValues.put(Email.TYPE, Email.TYPE_OTHER);
			mValues.put(Email.MIMETYPE, Email.CONTENT_ITEM_TYPE);
			addInsertOp(); 
		} 
		return this;
	} 
	 
	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;
	} 
	 
	public ContactOperations addBirthday(String birthday) {
		mValues.clear();
		if (!TextUtils.isEmpty(birthday)) {
			mValues.put(Event.START_DATE, birthday);
			mValues.put(Event.TYPE, Event.TYPE_BIRTHDAY);
			mValues.put(Event.MIMETYPE, Event.CONTENT_ITEM_TYPE);
			addInsertOp(); 
		} 
		return this;
	} 
	 
	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;
	} 
	 
	public ContactOperations addProfileAction(String userId) {
		mValues.clear();
		if (userId != null) {
			mValues.put(SyncAdapterColumns.DATA_PID, userId);
			mValues.put(SyncAdapterColumns.DATA_SUMMARY, mContext.getString(R.string.profile_action));
			mValues.put(SyncAdapterColumns.DATA_DETAIL, mContext.getString(R.string.view_profile));
			mValues.put(Data.MIMETYPE, SyncAdapterColumns.MIME_PROFILE);
			addInsertOp(); 
		} 
		return this;
	} 
	 
	public ContactOperations updateServerId(String serverId, Uri uri) {
		mValues.clear();
		mValues.put(RawContacts.SOURCE_ID, serverId);
		addUpdateOp(uri);
		return this;
	} 
	 
	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;
	} 
	 
	public ContactOperations updateBirthday(String birthday, String existingBirthday,
			Uri uri) {
		if (!TextUtils.equals(existingBirthday, birthday)) {
			mValues.clear();
			mValues.put(Event.START_DATE, birthday);
			addUpdateOp(uri);
		} 
		return this;
	} 
	 
	public ContactOperations updateName(Uri uri, String existingFirstName,
			String existingLastName, String firstName, String lastName) {
		 
		mValues.clear();
		if (!TextUtils.equals(existingFirstName, firstName)) {
			mValues.put(StructuredName.GIVEN_NAME, firstName);
		} 
		if (!TextUtils.equals(existingLastName, lastName)) {
			mValues.put(StructuredName.FAMILY_NAME, lastName);
		} 
		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;
	} 
	 
	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(Uri uri, String existingAvatarUrl, String avatarUrl) {
		if (avatarUrl != null && !TextUtils.equals(existingAvatarUrl, avatarUrl)) {
			byte[] avatarBuffer = NetworkUtilities.downloadAvatar(avatarUrl);
			if (avatarBuffer != null) {
				mValues.clear();
				mValues.put(Photo.DATA1, avatarUrl);
				mValues.put(Photo.PHOTO, avatarBuffer);
				mValues.put(Photo.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
				addUpdateOp(uri);
			} 
		} 
		return this;
	} 
	 
	public ContactOperations updateProfileAction(Integer userId, Uri uri) {
		mValues.clear();
		mValues.put(SyncAdapterColumns.DATA_PID, userId);
		addUpdateOp(uri);
		return this;
	} 
	 
	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());