Java Code Examples for android.content.ContentProviderOperation#newInsert()

The following examples show how to use android.content.ContentProviderOperation#newInsert() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
public static ContentProviderOperation insert(Context context, Reminder reminder, int backref) {
    ContentProviderOperation.Builder builder = ContentProviderOperation
            .newInsert(CalendarLoader.getBirthdayAdapterUri(context, CalendarContract.Reminders.CONTENT_URI));
    /*
     * add reminder to last added event identified by backRef
     * see http://stackoverflow.com/questions/4655291/semantics-of-
     * withvaluebackreference
     */
    builder.withValueBackReference(CalendarContract.Reminders.EVENT_ID, backref);
    return insert(builder, reminder);
}
 
Example 2
/**
 * Get a new ContentProviderOperation to insert an event
 */
public static ContentProviderOperation insert(Context context, long calendarId,
                                              CalendarEvent event, @Nullable Contact contact) {
    ContentProviderOperation.Builder builder;

    builder = ContentProviderOperation.newInsert(CalendarLoader.getBirthdayAdapterUri(context, CalendarContract.Events.CONTENT_URI));
    builder.withValue(CalendarContract.Events.CALENDAR_ID, calendarId);
    assignValuesInBuilder(builder, event);

    builder.withValue(CalendarContract.Events.STATUS, CalendarContract.Events.STATUS_CONFIRMED);

    /*
     * Enable reminders for this event
     * Note: Needs to be explicitly set on Android < 4 to enable reminders
     */
    builder.withValue(CalendarContract.Events.HAS_ALARM, 1);

    /*
     * Set availability to free.
     * Note: HTC calendar (4.0.3 Android + HTC Sense 4.0) will show a conflict with other events
     * if availability is not set to free!
     */
    if (Build.VERSION.SDK_INT >= 14) {
        builder.withValue(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_FREE);
    }

    // add button to open contact
    if (Build.VERSION.SDK_INT >= 16 && contact != null && contact.getLookUpKey() != null) {
        builder.withValue(CalendarContract.Events.CUSTOM_APP_PACKAGE, context.getPackageName());
        Uri contactLookupUri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_LOOKUP_URI, contact.getLookUpKey());
        builder.withValue(CalendarContract.Events.CUSTOM_APP_URI, contactLookupUri.toString());
    }
    Log.d(TAG, "Build insert event : " + event);
    return builder.build();
}
 
Example 3
public static void updateContactPhoto(ContentResolver c, long rawContactId, Photo pic, boolean primary){
	ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
	

	
	//insert new picture
	try {
		if(pic.data != null) {
               //delete old picture
               String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId
                       + "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
               Log.i(TAG, "Deleting picture: "+where);

               ContentProviderOperation.Builder builder = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI);
               builder.withSelection(where, null);
               operationList.add(builder.build());
			builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
			builder.withValue(ContactsContract.CommonDataKinds.Photo.RAW_CONTACT_ID, rawContactId);
			builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
			builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, pic.data);
			builder.withValue(ContactsContract.Data.SYNC2, String.valueOf(pic.timestamp));
			builder.withValue(ContactsContract.Data.SYNC3, pic.url);
               if (primary)
			    builder.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
			operationList.add(builder.build());

			
		}
		c.applyBatch(ContactsContract.AUTHORITY,	operationList);

	} catch (Exception e) {
		// TODO Auto-generated catch block
		Log.e("ERROR:" , e.toString());
	}


}
 
Example 4
/**
 * Creates new logger session. Must be created before appending log entries.
 * If the nRF Logger application is not installed the method will return <code>null</code>.
 *
 * @param context the context (activity, service or application).
 * @param profile application profile which will be concatenated to the application name.
 * @param key     the session key, which is used to group sessions.
 * @param name    the human readable session name.
 * @return The {@link LogSession} that can be used to append log entries or <code>null</code>
 * if nRF Logger is not installed. The <code>null</code> value can be next passed to logging methods.
 */
@Nullable
public static LogSession newSession(@NonNull final Context context,
									@Nullable final String profile,
									@NonNull final String key,
									@Nullable final String name) {
	final ArrayList<ContentProviderOperation> ops = new ArrayList<>();
	ContentProviderOperation.Builder builder =
			ContentProviderOperation.newInsert(LogContract.Application.CONTENT_URI);
	final String appName = context.getApplicationInfo()
			.loadLabel(context.getPackageManager()).toString();
	if (profile != null)
		builder.withValue(LogContract.Application.APPLICATION, appName + " " + profile);
	else
		builder.withValue(LogContract.Application.APPLICATION, appName);
	ops.add(builder.build());

	final Uri uri = LogContract.Session.CONTENT_URI.buildUpon()
			.appendEncodedPath(LogContract.Session.KEY_CONTENT_DIRECTORY)
			.appendEncodedPath(key)
			.build();
	builder = ContentProviderOperation.newInsert(uri)
			.withValueBackReference(LogContract.Session.APPLICATION_ID, 0)
			.withValue(LogContract.Session.NAME, name);
	ops.add(builder.build());

	try {
		final ContentProviderResult[] results = context.getContentResolver()
				.applyBatch(LogContract.AUTHORITY, ops);
		final Uri sessionUri = results[1].uri;
		return new LogSession(context, sessionUri);
	} catch (final Exception e) {
		// the nRF Logger application is not installed, do nothing
		return null;
	}
}
 
Example 5
Source Project: ContactMerger   File: ContactDataMapper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Append all operations needed to store the current contact to a set of
 * operations.
 * @param contact The current contact with metadata.
 * @param operations A set of operations to be extended.
 */
public void persist(RawContact contact, ArrayList<ContentProviderOperation> operations) {
    int operationsStart = operations.size();
    Builder operation;
    if (contact.getID() == -1) {
        operation = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
    } else {
        operation = ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI);
        operation.withSelection(RawContacts._ID + "=?", new String[]{Long.toString(contact.getID())});
    }
    ContentValues values = new ContentValues();
    put(values, contact);
    operation.withValues(values);
    operations.add(operation.build());
    for (Metadata data: contact.getMetadata().values()) {
        values.clear();
        put(values, data);
        if (data instanceof DeletedMetadata) {
            operation = ContentProviderOperation.newDelete(Data.CONTENT_URI);
            operation.withValues(values);
            operation.withSelection(Data._ID + "=?", new String[]{Long.toString(contact.getID())});
            operations.add(operation.build());
            continue;
        }
        if (data.getID() == -1) {
            operation = ContentProviderOperation.newInsert(Data.CONTENT_URI);
        } else {
            operation = ContentProviderOperation.newUpdate(Data.CONTENT_URI);
            operation.withSelection(Data._ID + "=?", new String[]{Long.toString(data.getID())});
        }
        if (contact.getID() == -1) {
            operation.withValueBackReference(Data.RAW_CONTACT_ID, operationsStart);
            values.remove(Data.RAW_CONTACT_ID);
        } else {
            values.put(Data.RAW_CONTACT_ID, contact.getID());
        }
        operation.withValues(values);
        operations.add(operation.build());
    }
}
 
Example 6
Source Project: v2ex   File: FeedsHandler.java    License: Apache License 2.0 5 votes vote down vote up
private void buildFeed(boolean isInsert, Feed feed,
                        ArrayList<ContentProviderOperation> list) {
    Uri allVideosUri = V2exContract.addCallerIsSyncAdapterParameter(
            V2exContract.Feeds.CONTENT_URI);
    Uri thisVideoUri = V2exContract.addCallerIsSyncAdapterParameter(
            V2exContract.Feeds.buildFeedUri(String.valueOf(feed.id)));

    ContentProviderOperation.Builder builder;
    if (isInsert) {
        builder = ContentProviderOperation.newInsert(allVideosUri);
    } else {
        builder = ContentProviderOperation.newUpdate(thisVideoUri);
    }

    if (TextUtils.isEmpty(String.valueOf(feed.id))) {
        LOGW(TAG, "Ignoring feed with missing feed ID.");
        return;
    }

    list.add(builder.withValue(V2exContract.Feeds.FEED_ID, feed.id)
            .withValue(V2exContract.Feeds.FEED_TITLE, feed.title)
            .withValue(V2exContract.Feeds.FEED_URL, feed.url)
            .withValue(V2exContract.Feeds.FEED_CONTENT, feed.content)
            .withValue(V2exContract.Feeds.FEED_CONTENT_RENDERED, feed.content_rendered)
            .withValue(V2exContract.Feeds.FEED_REPLIES, feed.replies)
            .withValue(V2exContract.Feeds.FEED_MEMBER, ModelUtils.serializeMember(feed.member))
            .withValue(V2exContract.Feeds.FEED_NODE, ModelUtils.serializeNode(feed.node))
            .withValue(V2exContract.Feeds.FEED_CREATED, feed.created)
            .withValue(V2exContract.Feeds.FEED_LAST_MODIFIED, feed.last_modified)
            .withValue(V2exContract.Feeds.FEED_LAST_TOUCHED, feed.last_touched)
            .withValue(V2exContract.Feeds.FEED_IMPORT_HASHCODE, feed.getImportHashcode())
            .build());
}
 
Example 7
Source Project: v2ex   File: NodesHandler.java    License: Apache License 2.0 5 votes vote down vote up
private void buildNode(boolean isInsert, Node node,
                       ArrayList<ContentProviderOperation> list) {
    Uri allVideosUri = V2exContract.addCallerIsSyncAdapterParameter(
            V2exContract.Nodes.CONTENT_URI);
    Uri thisVideoUri = V2exContract.addCallerIsSyncAdapterParameter(
            V2exContract.Nodes.buildNodeUri(String.valueOf(node.id)));

    ContentProviderOperation.Builder builder;
    if (isInsert) {
        builder = ContentProviderOperation.newInsert(allVideosUri);
    } else {
        builder = ContentProviderOperation.newUpdate(thisVideoUri);
    }

    if (TextUtils.isEmpty(String.valueOf(node.id))) {
        LOGW(TAG, "Ignoring feed with missing feed ID.");
        return;
    }

    list.add(builder.withValue(V2exContract.Nodes.NODE_ID, node.id)
            .withValue(V2exContract.Nodes.NODE_NAME, node.name)
            .withValue(V2exContract.Nodes.NODE_URL, node.url)
            .withValue(V2exContract.Nodes.NODE_TITLE, node.title)
            .withValue(V2exContract.Nodes.NODE_TITLE_ALTERNATIVE, node.title_alternative)
            .withValue(V2exContract.Nodes.NODE_TOPICS, node.topics)
            .withValue(V2exContract.Nodes.NODE_HEADER, node.header)
            .withValue(V2exContract.Nodes.NODE_FOOTER, node.footer)
            .withValue(V2exContract.Nodes.NODE_CREATED, node.created)
            .withValue(V2exContract.Nodes.NODE_AVATAR_MINI, node.avatar_mini)
            .withValue(V2exContract.Nodes.NODE_AVATAR_NORMAL, node.avatar_normal)
            .withValue(V2exContract.Nodes.NODE_AVATAR_LARGE, node.avatar_large)
            .withValue(V2exContract.Nodes.NODE_IMPORT_HASHCODE, node.getImportHashcode())
            .build());
}
 
Example 8
Source Project: v2ex   File: ReviewsHandler.java    License: Apache License 2.0 5 votes vote down vote up
private void buildReview(boolean isInsert, Review review,
                         ArrayList<ContentProviderOperation> list) {
    Uri allReviewsUri = V2exContract.Reviews.CONTENT_URI;
    Uri thisReviewUri = V2exContract.Reviews.buildReviewTopicUri(String.valueOf(review.id));

    ContentProviderOperation.Builder builder;
    if (isInsert) {
        builder = ContentProviderOperation.newInsert(allReviewsUri);
    } else {
        builder = ContentProviderOperation.newUpdate(thisReviewUri);
    }

    if (TextUtils.isEmpty(String.valueOf(review.id))) {
        LOGW(TAG, "Ignoring feed with missing feed ID.");
        return;
    }

    list.add(builder.withValue(V2exContract.Reviews.REVIEW_ID, review.id)
            .withValue(V2exContract.Reviews.REVIEW_TOPIC_ID, topicId)
            .withValue(V2exContract.Reviews.REVIEW_THANKS, review.thanks)
            .withValue(V2exContract.Reviews.REVIEW_CONTENT, review.content)
            .withValue(V2exContract.Reviews.REVIEW_CONTENT_RENDERED, review.content_rendered)
            .withValue(V2exContract.Reviews.REVIEW_MEMBER, ModelUtils.serializeMember(review.member))
            .withValue(V2exContract.Reviews.REVIEW_CREATED, review.created)
            .withValue(V2exContract.Reviews.REVIEW_LAST_MODIFIED, review.last_modified)
            .withValue(V2exContract.Reviews.REVIEW_IMPORT_HASHCODE, review.getImportHashcode())
            .build());
}
 
Example 9
Source Project: v2ex   File: MembersHandler.java    License: Apache License 2.0 5 votes vote down vote up
private void buildMember(boolean isInsert, Member member,
                        ArrayList<ContentProviderOperation> list) {
    Uri uri = V2exContract.Members.buildMemberUsernameUri(member.username);

    ContentProviderOperation.Builder builder;
    if (isInsert) {
        builder = ContentProviderOperation.newInsert(uri);
    } else {
        builder = ContentProviderOperation.newUpdate(uri);
    }

    list.add(builder.withValue(V2exContract.Members.MEMBER_ID, member.id)
            .withValue(V2exContract.Members.MEMBER_URL, member.url)
            .withValue(V2exContract.Members.MEMBER_USERNAME, member.username)
            .withValue(V2exContract.Members.MEMBER_WEBSITE, member.website)
            .withValue(V2exContract.Members.MEMBER_TWITTER, member.twitter)
            .withValue(V2exContract.Members.MEMBER_PSN, member.psn)
            .withValue(V2exContract.Members.MEMBER_GITHUB, member.github)
            .withValue(V2exContract.Members.MEMBER_BTC, member.btc)
            .withValue(V2exContract.Members.MEMBER_LOCATION, member.location)
            .withValue(V2exContract.Members.MEMBER_TAGLINE, member.tagline)
            .withValue(V2exContract.Members.MEMBER_BIO, member.bio)
            .withValue(V2exContract.Members.MEMBER_AVATAR_MINI, member.avatar_mini)
            .withValue(V2exContract.Members.MEMBER_AVATAR_NORMAL, member.avatar_normal)
            .withValue(V2exContract.Members.MEMBER_AVATAR_LARGE, member.avatar_large)
            .withValue(V2exContract.Members.MEMBER_CREATED, member.created)
            .withValue(V2exContract.Members.MEMBER_IMPORT_HASHCODE, member.getImportHashcode())
            .build());
}
 
Example 10
private static long getCalendar(Context context) {

        ContentResolver contentResolver = context.getContentResolver();

        // Find the calendar if we've got one
        Uri calenderUri = CalendarContract.Calendars.CONTENT_URI.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, ACCOUNT_NAME)
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, ACCOUNT_TYPE).build();


        Cursor cursor = contentResolver.query(calenderUri, new String[]{BaseColumns._ID},
                CalendarContract.Calendars.ACCOUNT_NAME + " = ? AND " + CalendarContract.Calendars.ACCOUNT_TYPE + " = ?",
                new String[]{ACCOUNT_NAME, ACCOUNT_TYPE}, null);

        try {
            if (cursor != null && cursor.moveToNext()) {
                return cursor.getLong(0);
            } else {
                ArrayList<ContentProviderOperation> operationList = new ArrayList<>();

                ContentProviderOperation.Builder builder = ContentProviderOperation
                        .newInsert(calenderUri);
                builder.withValue(CalendarContract.Calendars.ACCOUNT_NAME, ACCOUNT_NAME);
                builder.withValue(CalendarContract.Calendars.ACCOUNT_TYPE, ACCOUNT_TYPE);
                builder.withValue(CalendarContract.Calendars.NAME, CALENDAR_COLUMN_NAME);
                builder.withValue(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
                        context.getString(R.string.appName));
                builder.withValue(CalendarContract.Calendars.CALENDAR_COLOR, context.getResources().getColor(R.color.colorPrimary));
                builder.withValue(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
                builder.withValue(CalendarContract.Calendars.SYNC_EVENTS, 0);
                builder.withValue(CalendarContract.Calendars.VISIBLE, 1);

                operationList.add(builder.build());
                try {
                    contentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);
                } catch (Exception e) {
                    e.printStackTrace();
                    return -1;
                }
                return getCalendar(context);
            }
        } finally {
            if (cursor != null && !cursor.isClosed())
                cursor.close();
        }
    }
 
Example 11
/**
 * Gets calendar id, when no calendar is present, create one!
 */
public static long getCalendar(Context context) {
    ContentResolver contentResolver = context.getContentResolver();

    // Find the calendar if we've got one
    Uri calenderUri = getBirthdayAdapterUri(context, CalendarContract.Calendars.CONTENT_URI);

    // be sure to select the birthday calendar only (additionally to appendQueries in
    // getBirthdayAdapterUri for Android < 4)
    Cursor cursor = contentResolver.query(calenderUri, new String[]{BaseColumns._ID},
            CalendarContract.Calendars.ACCOUNT_NAME + " = ? AND " + CalendarContract.Calendars.ACCOUNT_TYPE + " = ?",
            new String[]{CalendarAccount.getAccountName(context), CalendarAccount.getAccountType(context)}, null);

    try {
        if (cursor != null && cursor.moveToNext()) {
            return cursor.getLong(0);
        } else {
            ArrayList<ContentProviderOperation> operationList = new ArrayList<>();

            ContentProviderOperation.Builder builder = ContentProviderOperation
                    .newInsert(calenderUri);
            builder.withValue(CalendarContract.Calendars.ACCOUNT_NAME, CalendarAccount.getAccountName(context));
            builder.withValue(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarAccount.getAccountType(context));
            builder.withValue(CalendarContract.Calendars.NAME, CALENDAR_COLUMN_NAME);
            builder.withValue(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
                    context.getString(R.string.calendar_display_name));
            builder.withValue(CalendarContract.Calendars.CALENDAR_COLOR, PreferencesManager.getCustomCalendarColor(context));
            //if (BuildConfig.DEBUG) {
            //    builder.withValue(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_EDITOR);
            //} else {
            builder.withValue(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
            //}
            builder.withValue(CalendarContract.Calendars.OWNER_ACCOUNT, CalendarAccount.getAccountName(context));
            builder.withValue(CalendarContract.Calendars.SYNC_EVENTS, 1);
            builder.withValue(CalendarContract.Calendars.VISIBLE, 1);
            operationList.add(builder.build());
            try {
                contentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);
            } catch (Exception e) {
                Log.e(TAG, "getCalendar() failed", e);
                return -1;
            }
            return getCalendar(context);
        }
    } finally {
        if (cursor != null && !cursor.isClosed())
            cursor.close();
    }
}
 
Example 12
public static ContentProviderOperation insert(Context context, long eventId, Reminder reminder) {
    ContentProviderOperation.Builder builder = ContentProviderOperation
            .newInsert(CalendarLoader.getBirthdayAdapterUri(context, CalendarContract.Reminders.CONTENT_URI));
    builder.withValue(CalendarContract.Reminders.EVENT_ID, eventId);
    return insert(builder, reminder);
}
 
Example 13
private static long getCalendar(Context context) {

        ContentResolver contentResolver = context.getContentResolver();

        // Find the calendar if we've got one
        Uri calenderUri = CalendarContract.Calendars.CONTENT_URI.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, ACCOUNT_NAME)
                .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, ACCOUNT_TYPE).build();


        Cursor cursor = contentResolver.query(calenderUri, new String[]{BaseColumns._ID},
                CalendarContract.Calendars.ACCOUNT_NAME + " = ? AND " + CalendarContract.Calendars.ACCOUNT_TYPE + " = ?",
                new String[]{ACCOUNT_NAME, ACCOUNT_TYPE}, null);

        try {
            if (cursor != null && cursor.moveToNext()) {
                return cursor.getLong(0);
            } else {
                ArrayList<ContentProviderOperation> operationList = new ArrayList<>();

                ContentProviderOperation.Builder builder = ContentProviderOperation
                        .newInsert(calenderUri);
                builder.withValue(CalendarContract.Calendars.ACCOUNT_NAME, ACCOUNT_NAME);
                builder.withValue(CalendarContract.Calendars.ACCOUNT_TYPE, ACCOUNT_TYPE);
                builder.withValue(CalendarContract.Calendars.NAME, CALENDAR_COLUMN_NAME);
                builder.withValue(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
                        context.getString(R.string.appName));
                builder.withValue(CalendarContract.Calendars.CALENDAR_COLOR, context.getResources().getColor(R.color.colorPrimary));
                builder.withValue(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
                builder.withValue(CalendarContract.Calendars.SYNC_EVENTS, 0);
                builder.withValue(CalendarContract.Calendars.VISIBLE, 1);

                operationList.add(builder.build());
                try {
                    contentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);
                } catch (Exception e) {
                    e.printStackTrace();
                    return -1;
                }
                return getCalendar(context);
            }
        } finally {
            if (cursor != null && !cursor.isClosed())
                cursor.close();
        }
    }
 
Example 14
private static void addContact(Account account, String name, String username) {
	ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

	ContentProviderOperation.Builder builder = ContentProviderOperation
			.newInsert(RawContacts.CONTENT_URI);
	builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
	builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
	builder.withValue(RawContacts.SYNC1, username);
	operationList.add(builder.build());

	builder = ContentProviderOperation
			.newInsert(ContactsContract.Data.CONTENT_URI);
	builder.withValueBackReference(
			ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID,
			0);
	builder.withValue(
			ContactsContract.Data.MIMETYPE,
			ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
	builder.withValue(
			ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
			name);
	operationList.add(builder.build());

	builder = ContentProviderOperation
			.newInsert(ContactsContract.Data.CONTENT_URI);
	builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
	builder.withValue(ContactsContract.Data.MIMETYPE,
			"vnd.android.cursor.item/vnd.org.mots.haxsync.profile");
	builder.withValue(ContactsContract.Data.DATA1, username);
	builder.withValue(ContactsContract.Data.DATA2, "Facebook Profile");
	builder.withValue(ContactsContract.Data.DATA3, "View profile");
	operationList.add(builder.build());

	try {
		mContentResolver.applyBatch(ContactsContract.AUTHORITY,
				operationList);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 15
private static void addSelfContact(Account account, int maxSize, boolean square, boolean faceDetect, boolean force, boolean root, int rootsize, File cacheDir, boolean google) {
	
	Uri rawContactUri = ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI.buildUpon()
			.appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
			.appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type)
			.build();
	
	long ID = -2;
	String username = null;
	String email = null;
       FacebookGraphFriend user = FacebookUtil.getSelfInfo();
       if (user == null)
           return;
       Cursor cursor = mContentResolver.query(rawContactUri, new String[] {BaseColumns._ID, UsernameColumn}, null, null, null);
       if (cursor.getCount() > 0){
       	cursor.moveToFirst();
       	ID = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
       	username = cursor.getString(cursor.getColumnIndex(UsernameColumn));
       	cursor.close();
       } else {
       	cursor.close();
           username = user.getUserName();
           email = user.getEmail();

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

       	ContentProviderOperation.Builder builder = ContentProviderOperation
       			.newInsert(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI);
       	builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
       	builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
       	builder.withValue(RawContacts.SYNC1, username);
       	operationList.add(builder.build());

       	builder = ContentProviderOperation
       			.newInsert(ContactsContract.Data.CONTENT_URI);
       	builder.withValueBackReference(
       			ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID,
       			0);
       	builder.withValue(
       			ContactsContract.Data.MIMETYPE,
       			ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
       	builder.withValue(
       			ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
       			account.name);
       	operationList.add(builder.build());

       	builder = ContentProviderOperation
       			.newInsert(ContactsContract.Data.CONTENT_URI);
       	builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
       	builder.withValue(ContactsContract.Data.MIMETYPE,
       			"vnd.android.cursor.item/vnd.org.mots.haxsync.profile");
       	builder.withValue(ContactsContract.Data.DATA1, username);
       	builder.withValue(ContactsContract.Data.DATA2, "Facebook Profile");
       	builder.withValue(ContactsContract.Data.DATA3, "View profile");
       	operationList.add(builder.build());
       	
       	if (email != null){
        	builder = ContentProviderOperation
        			.newInsert(ContactsContract.Data.CONTENT_URI);
        	builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
        	builder.withValue(ContactsContract.Data.MIMETYPE,
        			ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
        	builder.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
        	operationList.add(builder.build());
       	}

       	try {
       		mContentResolver.applyBatch(ContactsContract.AUTHORITY,
       				operationList);
       	} catch (Exception e) {
       		// TODO Auto-generated catch block
       		e.printStackTrace();
       		return;
       	}
           cursor = mContentResolver.query(rawContactUri, new String[] {BaseColumns._ID}, null, null, null);
           if (cursor.getCount() > 0){
           	cursor.moveToFirst();
           	ID = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
           	cursor.close();
           } else{
           	Log.i(TAG, "NO SELF CONTACT FOUND");
           	return;
           }
       }
       Log.i("self contact", "id: "+ID+" uid: "+ username);
       if (ID != -2 && username != null){

       updateContactPhoto(ID, 0, maxSize, square, user.getPicURL(), faceDetect, true, root, rootsize, cacheDir, google, true);


       }
}