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:fr.mixit.android.io.JsonHandlerApplyMembers.java

public void parseLinkedInterests(String itemId, JSONArray interests, ContentResolver resolver)
        throws JSONException {
    final Uri itemInterestsUri = MixItContract.Members.buildInterestsDirUri(itemId);
    final HashSet<String> interestsIds = Sets.newHashSet();

    for (int j = 0; j < interests.length(); j++) {
        final int id = interests.getInt(j);
        final String interestId = String.valueOf(id);
        interestsIds.add(interestId);/*from  w ww.j  a  v  a  2  s  .  c  o  m*/

        final ContentProviderOperation ope = ContentProviderOperation.newInsert(itemInterestsUri) //
                .withValue(MixItDatabase.MembersInterests.INTEREST_ID, interestId) //
                .withValue(MixItDatabase.MembersInterests.MEMBER_ID, itemId) //
                .build();
        ProviderParsingUtils.addOpeAndApplyBatch(mAuthority, resolver, mBatch, false, ope);
    }

    mItemInterestsIds.put(itemId, interestsIds);
}

From source file:ch.berta.fabio.popularmovies.data.repositories.MovieRepositoryImpl.java

private void addMovieVideosOps(@NonNull MovieDetails movieDetails, long movieRowId,
        @NonNull ArrayList<ContentProviderOperation> ops) {
    ops.add(ContentProviderOperation.newDelete(MovieContract.Video.buildVideosFromMovieUri(movieRowId))
            .build());// w  ww .j  a  v a  2  s  . co  m

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

From source file:com.example.jumpnote.android.SyncAdapter.java

public void reconcileSyncedNotes(ContentProviderClient provider, Account account,
        List<ModelJava.Note> changedNotes, SyncStats syncStats)
        throws RemoteException, OperationApplicationException {
    Cursor noteCursor;//from w ww  . j av a2 s.co m
    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
    for (ModelJava.Note changedNote : changedNotes) {
        Uri noteUri = null;

        if (changedNote.getId() != null) {
            noteUri = addCallerIsSyncAdapterParameter(
                    JumpNoteContract.buildNoteUri(account.name, Long.parseLong(changedNote.getId())));
        } else {
            noteCursor = provider.query(JumpNoteContract.buildNoteListUri(account.name), PROJECTION,
                    JumpNoteContract.Notes.SERVER_ID + " = ?", new String[] { changedNote.getServerId() },
                    null);

            if (noteCursor.moveToNext()) {
                noteUri = addCallerIsSyncAdapterParameter(
                        JumpNoteContract.buildNoteUri(account.name, noteCursor.getLong(0)));
            }

            noteCursor.close();
        }

        if (changedNote.isPendingDelete()) {
            // Handle server-side delete.
            if (noteUri != null) {
                operations.add(ContentProviderOperation.newDelete(noteUri).build());
                syncStats.numDeletes++;
            }
        } else {
            ContentValues values = changedNote.toContentValues();
            if (noteUri != null) {
                // Handle server-side update.
                operations.add(ContentProviderOperation.newUpdate(noteUri).withValues(values).build());
                syncStats.numUpdates++;
            } else {
                // Handle server-side insert.
                operations
                        .add(ContentProviderOperation
                                .newInsert(addCallerIsSyncAdapterParameter(
                                        JumpNoteContract.buildNoteListUri(account.name)))
                                .withValues(values).build());
                syncStats.numInserts++;
            }
        }
    }

    provider.applyBatch(operations);
}

From source file:com.samsung.android.remindme.SyncAdapter.java

public void reconcileSyncedAlerts(ContentProviderClient provider, Account account,
        List<ModelJava.Alert> changedAlerts, SyncStats syncStats)
        throws RemoteException, OperationApplicationException {
    Cursor alertCursor;/*from w ww  . j  ava 2 s  .co m*/
    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
    for (ModelJava.Alert changedAlert : changedAlerts) {
        Uri alertUri = null;

        if (changedAlert.getId() != null) {
            alertUri = addCallerIsSyncAdapterParameter(
                    RemindMeContract.buildAlertUri(account.name, Long.parseLong(changedAlert.getId())));
        } else {
            alertCursor = provider.query(RemindMeContract.buildAlertListUri(account.name), PROJECTION,
                    RemindMeContract.Alerts.SERVER_ID + " = ?", new String[] { changedAlert.getServerId() },
                    null);

            if (alertCursor.moveToNext()) {
                alertUri = addCallerIsSyncAdapterParameter(
                        RemindMeContract.buildAlertUri(account.name, alertCursor.getLong(0)));
            }

            alertCursor.close();
        }

        if (changedAlert.isPendingDelete()) {
            // Handle server-side delete.
            if (alertUri != null) {
                operations.add(ContentProviderOperation.newDelete(alertUri).build());
                syncStats.numDeletes++;
            }
        } else {
            ContentValues values = changedAlert.toContentValues();
            if (alertUri != null) {
                // Handle server-side update.
                operations.add(ContentProviderOperation.newUpdate(alertUri).withValues(values).build());
                syncStats.numUpdates++;
            } else {
                // Handle server-side insert.
                operations
                        .add(ContentProviderOperation
                                .newInsert(addCallerIsSyncAdapterParameter(
                                        RemindMeContract.buildAlertListUri(account.name)))
                                .withValues(values).build());
                syncStats.numInserts++;
            }
        }
    }

    provider.applyBatch(operations);
}

From source file:fr.mixit.android.io.JsonHandlerApplyMembers.java

public void parseLinks(String itemId, JSONArray links, ContentResolver resolver) throws JSONException {
    final Uri itemLinksUri = MixItContract.Members.buildLinksDirUri(itemId);
    final HashSet<String> linksIds = Sets.newHashSet();

    for (int j = 0; j < links.length(); j++) {
        final int id = links.getInt(j);
        final String linkId = String.valueOf(id);
        linksIds.add(linkId);//from w ww .  j a va  2s.c  o  m

        final ContentProviderOperation ope = ContentProviderOperation.newInsert(itemLinksUri) //
                .withValue(MixItDatabase.MembersLinks.LINK_ID, linkId) //
                .withValue(MixItDatabase.MembersLinks.MEMBER_ID, itemId) //
                .build();
        ProviderParsingUtils.addOpeAndApplyBatch(mAuthority, resolver, mBatch, false, ope);
    }

    mItemLinksIds.put(itemId, linksIds);
}

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

protected Builder newDataInsertBuilder(Uri dataUri, String refFieldName, long raw_ref_id, int backrefIdx) {
    Builder builder = ContentProviderOperation.newInsert(syncAdapterURI(dataUri));
    if (backrefIdx != -1)
        return builder.withValueBackReference(refFieldName, backrefIdx);
    else/* w  w w.  j av a2 s. c  o  m*/
        return builder.withValue(refFieldName, raw_ref_id);
}

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

private void createRawContact(Intent intent) {
    String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME);
    String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE);
    String dataSet = intent.getStringExtra(EXTRA_DATA_SET);
    List<ContentValues> valueList = intent.getParcelableArrayListExtra(EXTRA_CONTENT_VALUES);
    Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT);

    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
    operations.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(RawContacts.ACCOUNT_NAME, accountName).withValue(RawContacts.ACCOUNT_TYPE, accountType)
            .withValue(RawContacts.DATA_SET, dataSet).build());

    int size = valueList.size();
    for (int i = 0; i < size; i++) {
        ContentValues values = valueList.get(i);
        values.keySet().retainAll(ALLOWED_DATA_COLUMNS);
        operations.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, 0).withValues(values).build());
    }//from  www  .  j  a va 2 s  . co  m

    ContentResolver resolver = getContentResolver();
    ContentProviderResult[] results;
    try {
        results = resolver.applyBatch(ContactsContract.AUTHORITY, operations);
    } catch (Exception e) {
        throw new RuntimeException("Failed to store new contact", e);
    }

    Uri rawContactUri = results[0].uri;
    callbackIntent.setData(RawContacts.getContactLookupUri(resolver, rawContactUri));

    deliverCallback(callbackIntent);
}

From source file:fr.mixit.android.io.JsonHandlerApplyMembers.java

public void parseLinkers(String itemId, JSONArray linkers, ContentResolver resolver) throws JSONException {
    final Uri itemLinkersUri = MixItContract.Members.buildLinkersDirUri(itemId);
    final HashSet<String> linkersIds = Sets.newHashSet();

    for (int j = 0; j < linkers.length(); j++) {
        final int id = linkers.getInt(j);
        final String linkerId = String.valueOf(id);
        linkersIds.add(linkerId);/*from w  ww . j  a v  a 2  s  .c  o m*/

        final ContentProviderOperation ope = ContentProviderOperation.newInsert(itemLinkersUri) //
                .withValue(MixItDatabase.MembersLinks.LINK_ID, itemId) //
                .withValue(MixItDatabase.MembersLinks.MEMBER_ID, linkerId) //
                .build();
        ProviderParsingUtils.addOpeAndApplyBatch(mAuthority, resolver, mBatch, false, ope);
    }

    mItemLinkersIds.put(itemId, linkersIds);
}

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

private void processProgramGroups(final Context context, final LocationProfile locationProfile,
        Program[] programs) throws RemoteException, OperationApplicationException {
    Log.v(TAG, "processProgramGroups : enter");

    if (null == context)
        throw new RuntimeException("RecordedHelperV26 is not initialized");

    Map<String, ProgramGroup> programGroups = new TreeMap<String, ProgramGroup>();
    for (Program program : programs) {

        if (null != program.getRecording()) {

            if (null != program.getRecording().getRecGroup()
                    && !"livetv".equalsIgnoreCase(program.getRecording().getRecGroup())
                    && !"deleted".equalsIgnoreCase(program.getRecording().getRecGroup())) {
                String cleaned = ArticleCleaner.clean(program.getTitle());
                if (!programGroups.containsKey(cleaned)) {

                    ProgramGroup programGroup = new ProgramGroup();
                    programGroup.setTitle(program.getTitle());
                    programGroup.setCategory(program.getCategory());
                    programGroup.setInetref(program.getInetref());
                    programGroup.setSort(0);

                    programGroups.put(cleaned, programGroup);
                }/*from w  w  w. ja  va  2  s .  com*/

            }

        }

    }

    int processed = -1;
    int count = 0;

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

    Log.v(TAG, "processProgramGroups : adding 'All' program group in programGroups");
    ProgramGroup all = new ProgramGroup(null, "All", "All", "All", "", 1);
    programGroups.put(all.getProgramGroup(), all);

    String[] programGroupProjection = new String[] { ProgramGroupConstants._ID };
    String programGroupSelection = ProgramGroupConstants.FIELD_PROGRAM_GROUP + " = ?";

    programGroupSelection = appendLocationHostname(context, locationProfile, programGroupSelection, null);

    for (String key : programGroups.keySet()) {
        Log.v(TAG, "processProgramGroups : processing programGroup '" + key + "'");

        ProgramGroup programGroup = programGroups.get(key);

        ContentValues programValues = convertProgramGroupToContentValues(locationProfile, programGroup);
        Cursor programGroupCursor = context.getContentResolver().query(ProgramGroupConstants.CONTENT_URI,
                programGroupProjection, programGroupSelection, new String[] { key }, null);
        if (programGroupCursor.moveToFirst()) {

            Long id = programGroupCursor
                    .getLong(programGroupCursor.getColumnIndexOrThrow(ProgramGroupConstants._ID));
            ops.add(ContentProviderOperation
                    .newUpdate(ContentUris.withAppendedId(ProgramGroupConstants.CONTENT_URI, id))
                    .withValues(programValues).withYieldAllowed(true).build());

        } else {

            ops.add(ContentProviderOperation.newInsert(ProgramGroupConstants.CONTENT_URI)
                    .withValues(programValues).withYieldAllowed(true).build());
        }
        programGroupCursor.close();
        count++;

        if (count > 100) {
            Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

            processBatch(context, ops, processed, count);
        }

    }

    if (!ops.isEmpty()) {
        Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

        processBatch(context, ops, processed, count);
    }

    Log.v(TAG, "processProgramGroups : remove deleted program groups");
    ops = new ArrayList<ContentProviderOperation>();

    DateTime lastModified = new DateTime();
    lastModified = lastModified.minusHours(1);

    String deleteProgramGroupSelection = ProgramGroupConstants.FIELD_LAST_MODIFIED_DATE + " < ?";
    String[] deleteProgramGroupArgs = new String[] { String.valueOf(lastModified.getMillis()) };

    deleteProgramGroupSelection = appendLocationHostname(context, locationProfile, deleteProgramGroupSelection,
            ProgramGroupConstants.TABLE_NAME);

    ops.add(ContentProviderOperation.newDelete(ProgramGroupConstants.CONTENT_URI)
            .withSelection(deleteProgramGroupSelection, deleteProgramGroupArgs).withYieldAllowed(true).build());

    if (!ops.isEmpty()) {
        Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

        processBatch(context, ops, processed, count);
    }

    Log.v(TAG, "processProgramGroups : exit");
}

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

private void processProgramGroups(final Context context, final LocationProfile locationProfile,
        Program[] programs) throws RemoteException, OperationApplicationException {
    Log.v(TAG, "processProgramGroups : enter");

    if (null == context)
        throw new RuntimeException("RecordedHelperV25 is not initialized");

    Map<String, ProgramGroup> programGroups = new TreeMap<String, ProgramGroup>();
    for (Program program : programs) {

        if (null != program.getRecording()) {

            if (null != program.getRecording().getRecGroup()
                    && !"livetv".equalsIgnoreCase(program.getRecording().getRecGroup())
                    && !"deleted".equalsIgnoreCase(program.getRecording().getRecGroup())) {
                String cleaned = ArticleCleaner.clean(program.getTitle());
                if (!programGroups.containsKey(cleaned)) {

                    ProgramGroup programGroup = new ProgramGroup();
                    programGroup.setTitle(program.getTitle());
                    programGroup.setCategory(program.getCategory());
                    programGroup.setInetref(program.getInetref());
                    programGroup.setSort(0);

                    programGroups.put(cleaned, programGroup);
                }//from   ww w  . j  a va 2 s. co  m

            }

        }

    }

    int processed = -1;
    int count = 0;

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

    Log.v(TAG, "processProgramGroups : adding 'All' program group in programGroups");
    ProgramGroup all = new ProgramGroup(null, "All", "All", "All", "", 1);
    programGroups.put(all.getProgramGroup(), all);

    String[] programGroupProjection = new String[] { ProgramGroupConstants._ID };
    String programGroupSelection = ProgramGroupConstants.FIELD_PROGRAM_GROUP + " = ?";

    programGroupSelection = appendLocationHostname(context, locationProfile, programGroupSelection, null);

    for (String key : programGroups.keySet()) {
        Log.v(TAG, "processProgramGroups : processing programGroup '" + key + "'");

        ProgramGroup programGroup = programGroups.get(key);

        ContentValues programValues = convertProgramGroupToContentValues(locationProfile, programGroup);
        Cursor programGroupCursor = context.getContentResolver().query(ProgramGroupConstants.CONTENT_URI,
                programGroupProjection, programGroupSelection, new String[] { key }, null);
        if (programGroupCursor.moveToFirst()) {

            Long id = programGroupCursor
                    .getLong(programGroupCursor.getColumnIndexOrThrow(ProgramGroupConstants._ID));
            ops.add(ContentProviderOperation
                    .newUpdate(ContentUris.withAppendedId(ProgramGroupConstants.CONTENT_URI, id))
                    .withValues(programValues).withYieldAllowed(true).build());

        } else {

            ops.add(ContentProviderOperation.newInsert(ProgramGroupConstants.CONTENT_URI)
                    .withValues(programValues).withYieldAllowed(true).build());
        }
        programGroupCursor.close();
        count++;

        if (count > 100) {
            Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

            processBatch(context, ops, processed, count);
        }

    }

    if (!ops.isEmpty()) {
        Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

        processBatch(context, ops, processed, count);
    }

    Log.v(TAG, "processProgramGroups : remove deleted program groups");
    ops = new ArrayList<ContentProviderOperation>();

    DateTime lastModified = new DateTime();
    lastModified = lastModified.minusHours(1);

    String deleteProgramGroupSelection = ProgramGroupConstants.FIELD_LAST_MODIFIED_DATE + " < ?";
    String[] deleteProgramGroupArgs = new String[] { String.valueOf(lastModified.getMillis()) };

    deleteProgramGroupSelection = appendLocationHostname(context, locationProfile, deleteProgramGroupSelection,
            ProgramGroupConstants.TABLE_NAME);

    ops.add(ContentProviderOperation.newDelete(ProgramGroupConstants.CONTENT_URI)
            .withSelection(deleteProgramGroupSelection, deleteProgramGroupArgs).withYieldAllowed(true).build());

    if (!ops.isEmpty()) {
        Log.v(TAG, "processProgramGroups : applying batch for '" + count + "' transactions");

        processBatch(context, ops, processed, count);
    }

    Log.v(TAG, "processProgramGroups : exit");
}