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:ch.berta.fabio.popularmovies.data.repositories.MovieRepositoryImpl.java

private ArrayList<ContentProviderOperation> getInsertContentProviderOps(@NonNull Movie movie) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    ops.add(ContentProviderOperation.newInsert(MovieContract.Movie.CONTENT_URI)
            .withValues(movie.getContentValuesEntry()).build());

    List<Review> reviews = movie.getReviews();
    if (!reviews.isEmpty()) {
        for (Review review : reviews) {
            ops.add(ContentProviderOperation.newInsert(MovieContract.Review.CONTENT_URI)
                    .withValueBackReference(MovieContract.Review.COLUMN_MOVIE_ID, 0)
                    .withValues(review.getContentValuesEntry()).build());
        }/* w  w  w .j  a v a 2 s  .co m*/
    }

    List<Video> videos = movie.getVideos();
    if (!videos.isEmpty()) {
        for (Video video : videos) {
            // only add youtube videos
            if (video.siteIsYouTube()) {
                ops.add(ContentProviderOperation.newInsert(MovieContract.Video.CONTENT_URI)
                        .withValueBackReference(MovieContract.Video.COLUMN_MOVIE_ID, 0)
                        .withValues(video.getContentValuesEntry()).build());
            }
        }
    }

    return ops;
}

From source file:cz.maresmar.sfm.utils.ActionUtils.java

/**
 * Make edits in Actions for corresponding Menu entry. These action are saved as
 * {@link ProviderContract#ACTION_SYNC_STATUS_LOCAL} then.
 * <p>//from  w  w  w .  j a  v  a 2  s  . c  o m
 * Handles menu group restrictions (like one order per group), in such cases creates
 * {@link ProviderContract#ACTION_ENTRY_TYPE_VIRTUAL} actions to override the
 * {@link ProviderContract#ACTION_SYNC_STATUS_SYNCED} ones. If an action reserves nothing,
 * the action is removed.
 * </p>
 *
 * @param context    Some valid context
 * @param userUri    User Uri prefix
 * @param relativeId Relative ID of corresponding Menu entry
 * @param portalId   Portal ID of corresponding Menu entry
 * @param reserved   New amount of reserved food
 * @param offered    New amount of offered food
 */
@WorkerThread
public static void makeEdit(@NonNull Context context, @NonNull Uri userUri, long relativeId, long portalId,
        int reserved, int offered) {
    // Load the corresponding menu entry
    @ProviderContract.PortalFeatures
    int portalFeatures;
    long menuGroupId;
    int price;
    long date;
    int syncedReserved, syncedOffered, syncedTaken;
    boolean hasLocal;
    int localReserved, localOffered;
    long portalGroupId;

    Uri menuUri = Uri.withAppendedPath(userUri, ProviderContract.MENU_ENTRY_PATH);
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();

    try (Cursor menuCursor = context.getContentResolver().query(menuUri, new String[] {
            ProviderContract.MenuEntry.PORTAL_FEATURES, ProviderContract.MenuEntry.GROUP_ID,
            ProviderContract.MenuEntry.PRICE, ProviderContract.MenuEntry.DATE,
            ProviderContract.MenuEntry.SYNCED_RESERVED_AMOUNT, ProviderContract.MenuEntry.SYNCED_OFFERED_AMOUNT,
            ProviderContract.MenuEntry.SYNCED_TAKEN_AMOUNT, ProviderContract.MenuEntry.LOCAL_RESERVED_AMOUNT,
            ProviderContract.MenuEntry.LOCAL_OFFERED_AMOUNT, ProviderContract.MenuEntry.PORTAL_GROUP_ID },
            ProviderContract.MenuEntry.ME_RELATIVE_ID + " = " + relativeId + " AND "
                    + ProviderContract.MenuEntry.PORTAL_ID + " = " + portalId,
            null, null)) {

        if (BuildConfig.DEBUG) {
            Assert.isOne(menuCursor.getCount());
        }

        menuCursor.moveToFirst();

        // Info
        portalFeatures = menuCursor.getInt(0);
        menuGroupId = menuCursor.getLong(1);
        price = menuCursor.getInt(2);
        date = menuCursor.getLong(3);

        // Synced
        syncedReserved = menuCursor.getInt(4);
        syncedOffered = menuCursor.getInt(5);
        syncedTaken = menuCursor.getInt(6);

        // Local
        hasLocal = !menuCursor.isNull(7);
        localReserved = menuCursor.getInt(7);
        localOffered = menuCursor.getInt(8);

        // Portal group
        portalGroupId = menuCursor.getLong(9);

        // Insert changes
        Uri actionUri = Uri.withAppendedPath(userUri, ProviderContract.ACTION_PATH);
        // Insert virtual group changes
        boolean restrictToOneOrderPerGroup = (portalFeatures
                & ProviderContract.FEATURE_RESTRICT_TO_ONE_ORDER_PER_GROUP) == ProviderContract.FEATURE_RESTRICT_TO_ONE_ORDER_PER_GROUP;

        // Delete old edits as I want something new
        if (!restrictToOneOrderPerGroup) {
            // Delete action for this menu entry
            ops.add((ContentProviderOperation.newDelete(actionUri)
                    .withSelection(ProviderContract.Action.ME_RELATIVE_ID + " = " + relativeId + " AND "
                            + ProviderContract.Action.ME_PORTAL_ID + " = " + portalId + " AND "
                            + ProviderContract.Action.SYNC_STATUS + " = "
                            + ProviderContract.ACTION_SYNC_STATUS_EDIT, null)
                    .build()));
        } else {
            // Delete actions for whole menu entry group
            ops.add(ContentProviderOperation.newDelete(actionUri)
                    .withSelection(ProviderContract.Action.ME_PORTAL_ID + " IN " + "(SELECT "
                            + DbContract.Portal._ID + " FROM " + DbContract.Portal.TABLE_NAME + " WHERE "
                            + DbContract.Portal.COLUMN_NAME_PGID + " == " + portalGroupId + " ) AND "
                            + ProviderContract.Action.SYNC_STATUS + " = "
                            + ProviderContract.ACTION_SYNC_STATUS_EDIT + " AND " + "EXISTS ( SELECT * FROM "
                            + DbContract.MenuEntry.TABLE_NAME + " WHERE " + DbContract.MenuEntry.COLUMN_NAME_PID
                            + " == " + ProviderContract.Action.ME_PORTAL_ID + " AND "
                            + DbContract.MenuEntry.COLUMN_NAME_RELATIVE_ID + " == "
                            + ProviderContract.Action.ME_RELATIVE_ID + " AND "
                            + DbContract.MenuEntry.COLUMN_NAME_DATE + " == " + date + " AND "
                            + DbContract.MenuEntry.COLUMN_NAME_MGID + " == " + menuGroupId + " )", null)
                    .build());
        }

        // Insert new edits
        if ((hasLocal && !(reserved == localReserved && offered == localOffered))
                || (!hasLocal && !(reserved == syncedReserved && offered == syncedOffered))) {
            if (restrictToOneOrderPerGroup) {
                // Sets other actions in group to zeros
                try (Cursor groupCursor = context.getContentResolver().query(menuUri, new String[] {
                        ProviderContract.MenuEntry.ME_RELATIVE_ID, ProviderContract.MenuEntry.PRICE,
                        ProviderContract.MenuEntry.STATUS, ProviderContract.MenuEntry.SYNCED_RESERVED_AMOUNT,
                        ProviderContract.MenuEntry.SYNCED_TAKEN_AMOUNT, ProviderContract.MenuEntry.PORTAL_ID },
                        ProviderContract.MenuEntry.PORTAL_ID + " IN " + "(SELECT " + DbContract.Portal._ID
                                + " FROM " + DbContract.Portal.TABLE_NAME + " WHERE "
                                + DbContract.Portal.COLUMN_NAME_PGID + " == " + portalGroupId + " ) AND "
                                + ProviderContract.MenuEntry.DATE + " = " + date + " AND "
                                + ProviderContract.MenuEntry.GROUP_ID + " = " + menuGroupId + " AND ("
                                + "(IFNULL(" + ProviderContract.MenuEntry.SYNCED_RESERVED_AMOUNT + ", 0)"
                                + " - IFNULL(" + ProviderContract.MenuEntry.SYNCED_TAKEN_AMOUNT
                                + ", 0)) > 0 OR " + "(IFNULL("
                                + ProviderContract.MenuEntry.LOCAL_RESERVED_AMOUNT + ", 0)" + " - IFNULL("
                                + ProviderContract.MenuEntry.SYNCED_TAKEN_AMOUNT + ", 0)) > 0)",
                        null, null)) {
                    if (groupCursor != null) {
                        while (groupCursor.moveToNext()) {
                            // Skip main changed row
                            if (groupCursor.getLong(0) == relativeId) {
                                continue;
                            }

                            @ProviderContract.MenuStatus
                            int status = groupCursor.getInt(2);
                            boolean canCancel = (status
                                    & ProviderContract.MENU_STATUS_CANCELABLE) == ProviderContract.MENU_STATUS_CANCELABLE;
                            boolean canUseStock = (status
                                    & ProviderContract.FEATURE_FOOD_STOCK) == ProviderContract.FEATURE_FOOD_STOCK;

                            // Insert virtual actions
                            ContentValues newAction = new ContentValues();
                            newAction.put(ProviderContract.Action.ME_RELATIVE_ID, groupCursor.getLong(0));
                            newAction.put(ProviderContract.Action.ME_PORTAL_ID, groupCursor.getLong(5));
                            newAction.put(ProviderContract.Action.SYNC_STATUS,
                                    ProviderContract.ACTION_SYNC_STATUS_EDIT);
                            newAction.put(ProviderContract.Action.ENTRY_TYPE,
                                    ProviderContract.ACTION_ENTRY_TYPE_VIRTUAL);
                            newAction.put(ProviderContract.Action.PRICE, groupCursor.getInt(1));
                            if (canCancel) {
                                newAction.put(ProviderContract.Action.RESERVED_AMOUNT, 0);
                                newAction.put(ProviderContract.Action.OFFERED_AMOUNT, 0);
                            } else {
                                newAction.put(ProviderContract.Action.RESERVED_AMOUNT, groupCursor.getInt(3));
                                newAction.put(ProviderContract.Action.OFFERED_AMOUNT, groupCursor.getInt(3));

                                Toast.makeText(context, R.string.actions_food_stock_on_restricted_to_one,
                                        Toast.LENGTH_LONG).show();
                            }
                            newAction.put(ProviderContract.Action.TAKEN_AMOUNT, groupCursor.getInt(4));

                            ops.add(ContentProviderOperation.newInsert(actionUri).withValues(newAction)
                                    .build());
                        }
                    }
                }
            }

            // Insert main edit
            ContentValues newAction = new ContentValues();
            newAction.put(ProviderContract.Action.ME_RELATIVE_ID, relativeId);
            newAction.put(ProviderContract.Action.ME_PORTAL_ID, portalId);
            newAction.put(ProviderContract.Action.SYNC_STATUS, ProviderContract.ACTION_SYNC_STATUS_EDIT);
            newAction.put(ProviderContract.Action.ENTRY_TYPE, ProviderContract.ACTION_ENTRY_TYPE_STANDARD);
            newAction.put(ProviderContract.Action.PRICE, price);
            newAction.put(ProviderContract.Action.RESERVED_AMOUNT, reserved);
            newAction.put(ProviderContract.Action.OFFERED_AMOUNT, offered);
            newAction.put(ProviderContract.Action.TAKEN_AMOUNT, syncedTaken);

            ops.add(ContentProviderOperation.newInsert(actionUri).withValues(newAction).build());
        }

        // Apply changes at once (it boost the performance)
        try {
            context.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.voidsink.anewjkuapp.update.ImportExamTask.java

@Override
protected Void doInBackground(Void... params) {
    Log.d(TAG, "Start importing exams");

    synchronized (sync_lock) {
        final DateFormat df = DateFormat.getDateInstance();

        try {/*from   w  w w  . j  a v  a  2 s  . c om*/
            Log.d(TAG, "setup connection");

            updateNotify(mContext.getString(R.string.notification_sync_connect));

            if (KusssHandler.getInstance().isAvailable(mContext,
                    AppUtils.getAccountAuthToken(mContext, mAccount),
                    AppUtils.getAccountName(mContext, mAccount),
                    AppUtils.getAccountPassword(mContext, mAccount))) {

                updateNotify(mContext.getString(R.string.notification_sync_exam_loading));

                List<Exam> exams;
                if (PreferenceWrapper.getNewExamsByCourseId(mContext)) {
                    CourseMap courseMap = new CourseMap(mContext);
                    List<Term> terms = KusssContentProvider.getTerms(mContext);

                    Log.d(TAG, "load exams by courseId");
                    exams = KusssHandler.getInstance().getNewExamsByCourseId(mContext, courseMap.getCourses(),
                            terms);
                } else {
                    Log.d(TAG, "load exams");
                    exams = KusssHandler.getInstance().getNewExams(mContext);
                }
                if (exams == null) {
                    mSyncResult.stats.numParseExceptions++;
                } else {
                    Map<String, Exam> examMap = new HashMap<>();
                    for (Exam exam : exams) {
                        Exam old = examMap.put(
                                KusssHelper.getExamKey(exam.getCourseId(),
                                        AppUtils.termToString(exam.getTerm()), exam.getDtStart().getTime()),
                                exam);
                        if (old != null) {
                            Log.w(TAG, "exam alread loaded: " + KusssHelper.getExamKey(old.getCourseId(),
                                    AppUtils.termToString(old.getTerm()), old.getDtStart().getTime()));
                        }
                    }

                    Log.d(TAG, String.format("got %s exams", exams.size()));

                    updateNotify(mContext.getString(R.string.notification_sync_exam_updating));

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

                    Uri examUri = KusssContentContract.Exam.CONTENT_URI;
                    Cursor c = mProvider.query(examUri, EXAM_PROJECTION, null, null, null);

                    if (c == null) {
                        Log.w(TAG, "selection failed");
                    } else {
                        Log.d(TAG, "Found " + c.getCount() + " local entries. Computing merge solution...");
                        int examId;
                        String examTerm;
                        String examCourseId;
                        long examDtStart;
                        long examDtEnd;
                        String examLocation;

                        while (c.moveToNext()) {
                            examId = c.getInt(COLUMN_EXAM_ID);
                            examTerm = c.getString(COLUMN_EXAM_TERM);
                            examCourseId = c.getString(COLUMN_EXAM_COURSEID);
                            examDtStart = c.getLong(COLUMN_EXAM_DTSTART);
                            examDtEnd = c.getLong(COLUMN_EXAM_DTEND);
                            examLocation = c.getString(COLUMN_EXAM_LOCATION);

                            Exam exam = examMap
                                    .remove(KusssHelper.getExamKey(examCourseId, examTerm, examDtStart));
                            if (exam != null) {
                                // Check to see if the entry needs to be
                                // updated
                                Uri existingUri = examUri.buildUpon().appendPath(Integer.toString(examId))
                                        .build();
                                Log.d(TAG, "Scheduling update: " + existingUri);

                                if (!DateUtils.isSameDay(new Date(examDtStart), exam.getDtStart())
                                        || !new Date(examDtEnd).equals(exam.getDtEnd())
                                        || !examLocation.equals(exam.getLocation())) {
                                    mNewExamNotification.addUpdate(getEventString(exam));
                                }

                                batch.add(ContentProviderOperation
                                        .newUpdate(KusssContentContract.asEventSyncAdapter(existingUri,
                                                mAccount.name, mAccount.type))
                                        .withValue(KusssContentContract.Exam.COL_ID, Integer.toString(examId))
                                        .withValues(KusssHelper.getExamContentValues(exam)).build());
                                mSyncResult.stats.numUpdates++;
                            } else if (examDtStart > mSyncFromNow - DateUtils.MILLIS_PER_DAY) {
                                // Entry doesn't exist. Remove only newer
                                // events from the database.
                                Uri deleteUri = examUri.buildUpon().appendPath(Integer.toString(examId))
                                        .build();
                                Log.d(TAG, "Scheduling delete: " + deleteUri);

                                batch.add(ContentProviderOperation.newDelete(KusssContentContract
                                        .asEventSyncAdapter(deleteUri, mAccount.name, mAccount.type)).build());
                                mSyncResult.stats.numDeletes++;
                            }
                        }
                        c.close();

                        for (Exam exam : examMap.values()) {
                            batch.add(ContentProviderOperation
                                    .newInsert(KusssContentContract.asEventSyncAdapter(examUri, mAccount.name,
                                            mAccount.type))
                                    .withValues(KusssHelper.getExamContentValues(exam)).build());
                            Log.d(TAG, "Scheduling insert: " + exam.getTerm() + " " + exam.getCourseId());

                            mNewExamNotification.addInsert(getEventString(exam));

                            mSyncResult.stats.numInserts++;
                        }

                        if (batch.size() > 0) {
                            updateNotify(mContext.getString(R.string.notification_sync_exam_saving));

                            Log.d(TAG, "Applying batch update");
                            mProvider.applyBatch(batch);
                            Log.d(TAG, "Notify resolver");
                            mResolver.notifyChange(KusssContentContract.Exam.CONTENT_CHANGED_URI, null, // No
                                    // local
                                    // observer
                                    false); // IMPORTANT: Do not
                            // sync to
                            // network
                        } else {
                            Log.w(TAG, "No batch operations found! Do nothing");
                        }
                    }
                }
                KusssHandler.getInstance().logout(mContext);
            } else {
                mSyncResult.stats.numAuthExceptions++;
            }
        } catch (Exception e) {
            Analytics.sendException(mContext, e, true);
            Log.e(TAG, "import failed", e);
        }
    }

    setImportDone();

    return null;
}

From source file:org.mythtv.service.dvr.v25.RecordingRuleHelperV25.java

private void processRecordingRule(final Context context, final LocationProfile locationProfile,
        ArrayList<ContentProviderOperation> ops, org.mythtv.services.api.v025.beans.RecRule recRule) {
    Log.d(TAG, "processRecordingRule : enter");

    String recRuleSelection = RecordingRuleConstants.FIELD_REC_RULE_ID + " = ?";

    recRuleSelection = appendLocationHostname(context, locationProfile, recRuleSelection,
            RecordingRuleConstants.TABLE_NAME);

    ContentValues recRuleValues = convertRecRuleToContentValues(locationProfile, recRule);
    Cursor recRuleCursor = context.getContentResolver().query(RecordingRuleConstants.CONTENT_URI,
            recRuleProjection, recRuleSelection, new String[] { String.valueOf(recRule.getId()) }, null);
    if (recRuleCursor.moveToFirst()) {
        Long id = recRuleCursor.getLong(recRuleCursor.getColumnIndexOrThrow(RecordingRuleConstants._ID));
        Log.v(TAG, "processRecordingRule : updating recRule " + id + ":" + recRule.getId() + ":"
                + recRule.getTitle());/* w  w w .j  a  va2s  . c o m*/

        ops.add(ContentProviderOperation
                .newUpdate(ContentUris.withAppendedId(RecordingRuleConstants.CONTENT_URI, id))
                .withValues(recRuleValues).build());

    } else {
        Log.v(TAG, "processRecordingRule : adding recRule " + recRule.getId() + ":" + recRule.getTitle());

        ops.add(ContentProviderOperation.newInsert(RecordingRuleConstants.CONTENT_URI).withValues(recRuleValues)
                .build());

    }
    recRuleCursor.close();

    Log.d(TAG, "processRecordingRule : exit");
}

From source file:org.mythtv.service.dvr.v26.RecordingRuleHelperV26.java

private void processRecordingRule(final Context context, final LocationProfile locationProfile,
        ArrayList<ContentProviderOperation> ops, org.mythtv.services.api.v026.beans.RecRule recRule) {
    Log.d(TAG, "processRecordingRule : enter");

    String recRuleSelection = RecordingRuleConstants.FIELD_REC_RULE_ID + " = ?";

    recRuleSelection = appendLocationHostname(context, locationProfile, recRuleSelection,
            RecordingRuleConstants.TABLE_NAME);

    ContentValues recRuleValues = convertRecRuleToContentValues(locationProfile, recRule);
    Cursor recRuleCursor = context.getContentResolver().query(RecordingRuleConstants.CONTENT_URI,
            recRuleProjection, recRuleSelection, new String[] { String.valueOf(recRule.getId()) }, null);
    if (recRuleCursor.moveToFirst()) {
        Long id = recRuleCursor.getLong(recRuleCursor.getColumnIndexOrThrow(RecordingRuleConstants._ID));
        Log.v(TAG, "processRecordingRule : updating recRule " + id + ":" + recRule.getId() + ":"
                + recRule.getTitle());//from ww  w . ja v  a 2  s  .  c  om

        ops.add(ContentProviderOperation
                .newUpdate(ContentUris.withAppendedId(RecordingRuleConstants.CONTENT_URI, id))
                .withValues(recRuleValues).build());

    } else {
        Log.v(TAG, "processRecordingRule : adding recRule " + recRule.getId() + ":" + recRule.getTitle());

        ops.add(ContentProviderOperation.newInsert(RecordingRuleConstants.CONTENT_URI).withValues(recRuleValues)
                .build());

    }
    recRuleCursor.close();

    Log.d(TAG, "processRecordingRule : exit");
}

From source file:com.nineash.hutsync.client.NetworkUtilities.java

private static ContentProviderOperation updateEvent(long calendar_id, Account account, Event event,
        long raw_id) {
    ContentProviderOperation.Builder builder;
    if (raw_id != -1) {
        builder = ContentProviderOperation.newUpdate(Events.CONTENT_URI.buildUpon()
                .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
                .appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type).build());
        builder.withSelection(Events._ID + " = '" + raw_id + "'", null);
    } else {// www.j a  va 2s.  c o  m
        builder = ContentProviderOperation.newInsert(Events.CONTENT_URI.buildUpon()
                .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
                .appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type).build());
    }
    long dtstart = event.getStartDate().getTime();
    long dtend = dtstart + (1000 * 60 * 60);
    if (event.getEndDate() != null)
        dtend = event.getEndDate().getTime();
    builder.withValue(Events.CALENDAR_ID, calendar_id);
    builder.withValue(Events.DTSTART, dtstart);
    builder.withValue(Events.DTEND, dtend);
    builder.withValue(Events.TITLE, event.getTitle());

    String location = "Pizza Hut";

    builder.withValue(Events.EVENT_LOCATION, location);

    String description = event.getDescription();

    builder.withValue(Events.DESCRIPTION, description);

    builder.withValue(Events._SYNC_ID, Long.valueOf(event.getId()));
    return builder.build();
}

From source file:com.granita.icloudcalsync.resource.LocalCollection.java

/** Enqueues adding the resource (including all data) to the local collection. Requires commit(). */
public void add(Resource resource) {
    int idx = pendingOperations.size();
    pendingOperations.add(buildEntry(ContentProviderOperation.newInsert(entriesURI()), resource, false)
            .withYieldAllowed(true).build());

    addDataRows(resource, -1, idx);//w  ww  .jav  a 2s  .  c o  m
}

From source file:org.mythtv.service.dvr.v27.RecordingRuleHelperV27.java

private void processRecordingRule(final Context context, final LocationProfile locationProfile,
        ArrayList<ContentProviderOperation> ops, org.mythtv.services.api.v027.beans.RecRule recRule) {
    Log.d(TAG, "processRecordingRule : enter");

    String recRuleSelection = RecordingRuleConstants.FIELD_REC_RULE_ID + " = ?";

    recRuleSelection = appendLocationHostname(context, locationProfile, recRuleSelection,
            RecordingRuleConstants.TABLE_NAME);

    ContentValues recRuleValues = convertRecRuleToContentValues(locationProfile, recRule);
    Cursor recRuleCursor = context.getContentResolver().query(RecordingRuleConstants.CONTENT_URI,
            recRuleProjection, recRuleSelection, new String[] { String.valueOf(recRule.getId()) }, null);
    if (recRuleCursor.moveToFirst()) {
        Long id = recRuleCursor.getLong(recRuleCursor.getColumnIndexOrThrow(RecordingRuleConstants._ID));
        Log.v(TAG, "processRecordingRule : updating recRule " + id + ":" + recRule.getId() + ":"
                + recRule.getTitle());/*from   www  .  j a v  a 2s.co  m*/

        ops.add(ContentProviderOperation
                .newUpdate(ContentUris.withAppendedId(RecordingRuleConstants.CONTENT_URI, id))
                .withValues(recRuleValues).build());

    } else {
        Log.v(TAG, "processRecordingRule : adding recRule " + recRule.getId() + ":" + recRule.getTitle());

        ops.add(ContentProviderOperation.newInsert(RecordingRuleConstants.CONTENT_URI).withValues(recRuleValues)
                .build());

    }
    recRuleCursor.close();

    Log.d(TAG, "processRecordingRule : exit");
}

From source file:org.linphone.compatibility.ApiFivePlus.java

public static void addSipAddressToContact(Context context, ArrayList<ContentProviderOperation> ops,
        String sipAddress) {/*from  w w  w.  j a v  a 2s . co m*/
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Im.DATA, sipAddress)
            .withValue(ContactsContract.CommonDataKinds.Im.TYPE,
                    ContactsContract.CommonDataKinds.Im.TYPE_CUSTOM)
            .withValue(ContactsContract.CommonDataKinds.Im.LABEL, context.getString(R.string.addressbook_label))
            .build());
}

From source file:at.bitfire.davdroid.resource.LocalCollection.java

/** Adds the resource (including all data) to the local collection.
 * @param resource   Resource to be added
 *//* w  ww  . j  a v  a2  s.  c  o  m*/
public void add(Resource resource) throws LocalStorageException {
    int idx = pendingOperations.size();
    pendingOperations.add(buildEntry(ContentProviderOperation.newInsert(entriesURI()), resource, false)
            .withYieldAllowed(true).build());

    addDataRows(resource, -1, idx);
    commit();
}