/** * Apply the given set of {@link ContentProviderOperation}, executing inside * a {@link SQLiteDatabase} transaction. All changes will be rolled back if * any single one fails. */ public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); db.beginTransaction(); try { final int numOperations = operations.size(); final ContentProviderResult[] results = new ContentProviderResult[numOperations]; for (int i = 0; i < numOperations; i++) { results[i] = operations.get(i).apply(this, results, i); } db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } }
@NonNull @Override public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { ContentProviderResult[] result = null; isApplyingBatch = true; final SQLiteDatabase db = db(); db.beginTransaction(); try { result = super.applyBatch(operations); db.setTransactionSuccessful(); } finally { db.endTransaction(); isApplyingBatch = false; } return result; }
@Nullable public static DatabaseIdRange createFromContentProviderResults(ContentProviderResult[] results){ Integer f = null; Integer l = null; for(ContentProviderResult result : results){ if(result.uri != null && !result.uri.toString().isEmpty()){ int dbid = Integer.parseInt(result.uri.getPathSegments().get(1)); if(f == null || dbid < f){ f = dbid; } if(l == null || dbid > l){ l = dbid; } } } return nonNull(f) && nonNull(l) ? new DatabaseIdRange(f, l) : null; }
@Nullable public static DatabaseIdRange createFromContentProviderResults(ContentProviderResult[] results, String path){ UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); sUriMatcher.addURI(MessengerContentProvider.AUTHORITY, path, MATCH_CODE); Integer f = null; Integer l = null; for(ContentProviderResult result : results){ if(result.uri != null && !result.uri.toString().isEmpty()){ if(sUriMatcher.match(result.uri) != MATCH_CODE){ continue; } int dbid = Integer.parseInt(result.uri.getPathSegments().get(1)); if(f == null || dbid < f){ f = dbid; } if(l == null || dbid > l){ l = dbid; } } } return nonNull(f) && nonNull(l) ? new DatabaseIdRange(f, l) : null; }
@Override public Single<int[]> attachDbos(int accountId, int attachToType, int attachToDbid, @NonNull List<Entity> entities) { return Single.create(emitter -> { ArrayList<ContentProviderOperation> operations = new ArrayList<>(entities.size()); int[] indexes = new int[entities.size()]; for (int i = 0; i < entities.size(); i++) { Entity entity = entities.get(i); indexes[i] = appendAttachOperationWithStableAttachToId(operations, accountId, attachToType, attachToDbid, entity); } ContentProviderResult[] results = getContentResolver().applyBatch(MessengerContentProvider.AUTHORITY, operations); int [] ids = new int[entities.size()]; for (int i = 0; i < indexes.length; i++) { ContentProviderResult result = results[indexes[i]]; int dbid = Integer.parseInt(result.uri.getPathSegments().get(1)); ids[i] = dbid; } emitter.onSuccess(ids); }); }
@Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { ContentProviderResult[] results; SQLiteDatabase db = mOpenHelper.getWritableDatabase(); db.beginTransaction(); try { inBatch.set(true); results = super.applyBatch(operations); inBatch.set(false); db.setTransactionSuccessful(); } finally { db.endTransaction(); } notifyChange(); return results; }
@NonNull @Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { try { Field uriField = ContentProviderOperation.class.getDeclaredField("mUri"); uriField.setAccessible(true); for (ContentProviderOperation operation : operations) { Uri pluginUri = Uri.parse(operation.getUri().getQueryParameter(KEY_URI)); uriField.set(operation, pluginUri); } } catch (Exception e) { return new ContentProviderResult[0]; } if (operations.size() > 0) { ContentProvider provider = getContentProvider(operations.get(0).getUri()); if (provider != null) { return provider.applyBatch(operations); } } return new ContentProviderResult[0]; }
@NonNull @Override public ContentProviderResult[] applyBatch( @NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final Context context = getContext(); if (context == null) { return new ContentProviderResult[0]; } final SampleDatabase database = SampleDatabase.getInstance(context); database.beginTransaction(); try { final ContentProviderResult[] result = super.applyBatch(operations); database.setTransactionSuccessful(); return result; } finally { database.endTransaction(); } }
@Test public void cheese_applyBatch() throws RemoteException, OperationApplicationException { final ArrayList<ContentProviderOperation> operations = new ArrayList<>(); operations.add(ContentProviderOperation .newInsert(SampleContentProvider.URI_CHEESE) .withValue(Cheese.COLUMN_NAME, "Peynir") .build()); operations.add(ContentProviderOperation .newInsert(SampleContentProvider.URI_CHEESE) .withValue(Cheese.COLUMN_NAME, "Queso") .build()); final ContentProviderResult[] results = mContentResolver.applyBatch( SampleContentProvider.AUTHORITY, operations); assertThat(results.length, is(2)); final Cursor cursor = mContentResolver.query(SampleContentProvider.URI_CHEESE, new String[]{Cheese.COLUMN_NAME}, null, null, null); assertThat(cursor, notNullValue()); assertThat(cursor.getCount(), is(2)); assertThat(cursor.moveToFirst(), is(true)); cursor.close(); }
/** * Apply the given set of {@link ContentProviderOperation}, executing inside * a {@link SQLiteDatabase} transaction. All changes will be rolled back if * any single one fails. */ @Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); db.beginTransaction(); try { final int numOperations = operations.size(); final ContentProviderResult[] results = new ContentProviderResult[numOperations]; for (int i = 0; i < numOperations; i++) { results[i] = operations.get(i).apply(this, results, i); } db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } }
@Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { if (DBG) Log.d(TAG, "applyBatch"); ContentProviderResult[] result = null; SQLiteDatabase db = mDbHolder.get(); db.beginTransaction(); try { result = super.applyBatch(operations); db.setTransactionSuccessful(); } finally { db.endTransaction(); } if (result != null) { mCr.notifyChange(MusicStore.ALL_CONTENT_URI, null); } return result; }
@Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { SQLiteDatabase db = mDbHolder.get(); db.beginTransaction(); ContentProviderResult[] result = null; try { result = super.applyBatch(operations); db.setTransactionSuccessful(); ContentResolver res = mCr; res.notifyChange(ScraperStore.ALL_CONTENT_URI, null); return result; } finally { db.endTransaction(); } }
@Override public void transactionSuccess() { try { ContentProviderResult[] cpr = context.getContentResolver().applyBatch( dsUri.getAuthority(), trans); if(cpr == null || cpr.length != trans.size()){ throw new DaoException(); } for (int i = 0; i < cpr.length; i++) { if (cpr[i] == null || ( cpr[i].count == null && cpr[i].uri == null)) { throw new DaoException(); } } } catch (RemoteException | OperationApplicationException e) { throw new DaoException(); } finally { trans = null; } }
@Override public ContentProviderResult[] applyBatch( ArrayList<ContentProviderOperation> operations ) throws OperationApplicationException { ContentProviderResult[] contentProviderResults; try { getWritableDatabase().beginTransaction(); contentProviderResults = new ContentProviderResult[operations .size()]; int i = 0; for (ContentProviderOperation cpo : operations) { contentProviderResults[i] = cpo.apply(this, contentProviderResults, i); if(contentProviderResults[i] == null || (contentProviderResults[i].count == null && contentProviderResults[i].uri == null)){ throw new DaoException(); } i++; } getWritableDatabase().setTransactionSuccessful(); } finally{ if (getWritableDatabase().inTransaction()) { getWritableDatabase().endTransaction(); } } return contentProviderResults; }
/** * Apply the given set of {@link ContentProviderOperation}, executing inside * a {@link SQLiteDatabase} transaction. All changes will be rolled back if * any single one fails. */ @Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final SQLiteDatabase db = mDatabaseHelper.getWritableDatabase(); db.beginTransaction(); try { final int numOperations = operations.size(); final ContentProviderResult[] results = new ContentProviderResult[numOperations]; for (int i = 0; i < numOperations; i++) { results[i] = operations.get(i).apply(this, results, i); } db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } }
/** * Apply the given set of {@link ContentProviderOperation}, executing inside * a {@link SQLiteDatabase} transaction. All changes will be rolled back if * any single one fails. */ @NonNull @Override public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); db.beginTransaction(); try { final int numOperations = operations.size(); final ContentProviderResult[] results = new ContentProviderResult[numOperations]; for (int i = 0; i < numOperations; i++) { results[i] = operations.get(i).apply(this, results, i); } db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } }
@Override public ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { Log_OC.d("FileContentProvider", "applying batch in provider " + this + " (temporary: " + isTemporary() + ")" ); ContentProviderResult[] results = new ContentProviderResult[operations.size()]; int i=0; SQLiteDatabase db = mDbHelper.getWritableDatabase(); db.beginTransaction(); // it's supposed that transactions can be nested try { for (ContentProviderOperation operation : operations) { results[i] = operation.apply(this, results, i); i++; } db.setTransactionSuccessful(); } finally { db.endTransaction(); } Log_OC.d("FileContentProvider", "applied batch in provider " + this); return results; }
public static Uri add(ContactSpec spec) { // original code http://stackoverflow.com/questions/4744187/how-to-add-new-contacts-in-android // good blog http://androiddevelopement.blogspot.de/2011/07/insert-update-delete-view-contacts-in.html ArrayList<ContentProviderOperation> ops = new ArrayList<>(); addContactBase(ops); addContactDisplayName(spec, ops); addContactAddress(spec, ops); try { ContentProviderResult[] results = InstrumentationRegistry.getTargetContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); return results[0].uri; } catch (RemoteException | OperationApplicationException e) { throw new IllegalStateException("Could not add contact", e); } }
@Override public @NonNull ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final SQLiteDatabase db = helper.getWritableDatabase(); db.beginTransaction(); try { final ContentProviderResult[] results = super.applyBatch(operations); db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } }
@Override public ContentProviderResult[] applyBatch( ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { /*SQLiteDatabase db = sqlOpenHelper.getWritableDatabase(); isInBatchMode.set(true); db.beginTransaction(); try { final ContentProviderResult[] retResult = super.applyBatch(operations); db.setTransactionSuccessful(); getContext().getContentResolver().notifyChange(ApparelContract.CONTENT_URI, null); return retResult; } finally { isInBatchMode.remove(); db.endTransaction(); }*/ return null; }
/** * Apply the given set of {@link ContentProviderOperation}, executing inside * a {@link SQLiteDatabase} transaction. All changes will be rolled back if * any single one fails. */ @Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final SQLiteDatabase db = mDbHelper.getWritableDatabase(); db.beginTransaction(); try { final int numOperations = operations.size(); final ContentProviderResult[] results = new ContentProviderResult[numOperations]; for (int i = 0; i < numOperations; i++) { results[i] = operations.get(i).apply(this, results, i); } db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } }
/** * Apply the given set of {@link ContentProviderOperation}, executing inside * a {@link SQLiteDatabase} transaction. All changes will be rolled back if * any single one fails. */ @NonNull public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); db.beginTransaction(); try { final int numOperations = operations.size(); final ContentProviderResult[] results = new ContentProviderResult[numOperations]; for (int i = 0; i < numOperations; i++) { results[i] = operations.get(i).apply(this, results, i); } db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } }
public synchronized static void updateEvent(Context context, Contact contact, DateUnknownYear newBirthday) throws EventException { // TODO UNIFORMISE for (CalendarEvent event : getEventsSavedOrCreateNewsForEachYear(context, contact)) { // Construct each anniversary of new birthday int year = new DateTime(event.getDate()).getYear(); Date newBirthdayDate = DateUnknownYear.getDateWithYear(newBirthday.getDate(), year); event.setDateStart(newBirthdayDate); event.setAllDay(true); ArrayList<ContentProviderOperation> operations = new ArrayList<>(); ContentProviderOperation contentProviderOperation = EventProvider.update(event); operations.add(contentProviderOperation); try { ContentProviderResult[] contentProviderResults = context.getContentResolver().applyBatch(CalendarContract.AUTHORITY, operations); for(ContentProviderResult contentProviderResult : contentProviderResults) { if (contentProviderResult.count != 0) Log.d(TAG, "Update event : " + event.toString()); } } catch (RemoteException|OperationApplicationException e) { Log.e(TAG, "Unable to update event : " + e.getMessage()); } } }
public synchronized static void deleteEventsFromContact(Context context, Contact contact) { ArrayList<ContentProviderOperation> operations = new ArrayList<>(); try { for (CalendarEvent event : getEventsSavedForEachYear(context, contact)) { operations.add(ReminderProvider.deleteAll(context, event.getId())); operations.add(EventProvider.delete(event)); } ContentProviderResult[] contentProviderResults = context.getContentResolver().applyBatch(CalendarContract.AUTHORITY, operations); for(ContentProviderResult contentProviderResult : contentProviderResults) { Log.d(TAG, contentProviderResult.toString()); if (contentProviderResult.uri != null) Log.d(TAG, contentProviderResult.uri.toString()); } } catch (RemoteException |OperationApplicationException |EventException e) { Log.e(TAG, "Unable to deleteById events : " + e.getMessage()); } }
@Override protected Exception doInBackground(Void... params) { try { ArrayList<ContentProviderOperation> ops = new ArrayList<>(); ContentProviderOperation.Builder contentBuilder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday.toBackupString()) .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY); Log.d(getClass().getSimpleName(), "Add birthday " + birthday); ops.add(contentBuilder.build()); ContentProviderResult[] results = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); if(results[0] == null) throw new Exception("Unable to add new birthday"); } catch(Exception e) { return e; } return null; }
@Override protected Exception doInBackground(Void... voids) { try { ArrayList<ContentProviderOperation> ops = new ArrayList<>(); ContentProviderOperation.Builder contentBuilder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(ContactsContract.Data._ID + " =? AND " + ContactsContract.Data.MIMETYPE + " =? AND " + ContactsContract.CommonDataKinds.Event.START_DATE + " =? AND " + ContactsContract.CommonDataKinds.Event.TYPE + " =?" , new String[]{String.valueOf(dataId), ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, oldBirthday.toBackupString(), String.valueOf(ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)}) .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday.toBackupString()); Log.d(getClass().getSimpleName(), "Update birthday " + oldBirthday + " to " + birthday); ops.add(contentBuilder.build()); ContentProviderResult[] results = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); if(results[0].count == 0) return new Exception("Unable to update birthday"); } catch(Exception e) { return e; } return null; }
@Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { writableDatabase.beginTransaction(); try { final int operationsCount = operations.size(); final ContentProviderResult[] results = new ContentProviderResult[operationsCount]; for (int i = 0; i < operationsCount; i++) { results[i] = operations.get(i).apply(this, results, i); } writableDatabase.setTransactionSuccessful(); return results; } finally { writableDatabase.endTransaction(); } }
@Override @NonNull public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { final SQLiteDatabase db = getOpenHelper().getWritableDatabase(); db.beginTransaction(); try { final int numberOfOperations = operations.size(); final ContentProviderResult[] results = new ContentProviderResult[numberOfOperations]; for (int i = 0; i < numberOfOperations; i++) { results[i] = operations.get(i).apply(this, results, i); } db.setTransactionSuccessful(); return results; } finally { db.endTransaction(); } }
public static void saveInTransaction(Context context, List<? extends CPDefaultRecord> records) throws RemoteException, OperationApplicationException { List<ContentProviderOperation> operations = prepareTransaction(context, records); ContentProviderResult[] contentProviderResults = CPOrm.applyPreparedOperations(operations); Map<Class, Long> referenceIds = new HashMap<>(); for (int i = 0; i < contentProviderResults.length; i++) { ContentProviderResult result = contentProviderResults[i]; CPDefaultRecord source = records.get(i); referenceIds.remove(source.getClass()); if(result.uri != null && source.getId() == null && ContentUris.parseId(result.uri) != -1){ source.setId(ContentUris.parseId(result.uri)); referenceIds.put(source.getClass(), source.getId()); } try { applyReferenceResults(source.getClass(), source, referenceIds); } catch (IllegalAccessException e) { CPOrmLog.e("Failed to apply back reference id's for uri " + result.uri); } } }
@NonNull @Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { SQLiteDatabase db = mQuizDBHelper.getWritableDatabase(); mIsBatchMode = true; db.beginTransaction(); try { final ContentProviderResult[] res = super.applyBatch(operations); db.setTransactionSuccessful(); getContext().getContentResolver().notifyChange(QuizProvider.BASE_URI, null); return res; } finally { mIsBatchMode = false; db.endTransaction(); } }
public List<Uri> execute() { List<Uri> resultUris = new ArrayList<Uri>(); if (mOperations.size() == 0) { return resultUris; } // Apply the mOperations to the content provider try { ContentProviderResult[] results = mResolver.applyBatch(ContactsContract.AUTHORITY, mOperations); if ((results != null) && (results.length > 0)) { for (int i = 0; i < results.length; i++) { resultUris.add(results[i].uri); } } } catch (final OperationApplicationException e1) { Log.e(TAG, "storing contact data failed", e1); } catch (final RemoteException e2) { Log.e(TAG, "storing contact data failed", e2); } mOperations.clear(); return resultUris; }