android.content.ContentProviderResult Java Examples

The following examples show how to use android.content.ContentProviderResult. 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 check out the related API usage on the sidebar.
Example #1
Source Project: Neptune   Author: iqiyi   File: ContentProviderProxy1.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    if (operations.size() > 0) {
        ContentProvider provider = getContentProvider(operations.get(0).getUri());
        if (provider != null) {
            try {
                for (ContentProviderOperation operation : operations) {
                    Uri pluginUri = Uri.parse(operation.getUri().getQueryParameter(IntentConstant.EXTRA_TARGET_URI_KEY));
                    ReflectionUtils.on(operation).set("mUri", pluginUri);
                }
                return provider.applyBatch(operations);
            } catch (Exception e) {
                return new ContentProviderResult[0];
            }
        }
    }
    return new ContentProviderResult[0];
}
 
Example #2
Source Project: GitJourney   Author: OlgaKuklina   File: ActivityItemsProvider.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example #3
Source Project: VirtualAPK   Author: didi   File: RemoteContentProvider.java    License: Apache License 2.0 6 votes vote down vote up
@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];
}
 
Example #4
Source Project: AndroidContacts   Author: blainepwnz   File: ContactsSaver.java    License: MIT License 6 votes vote down vote up
private ContentProviderResult[] createContacts(List<ContactData> contacts) {
    ContentProviderResult[] results = null;
    ArrayList<ContentProviderOperation> op_list = new ArrayList<>();
    for (int i = 0; i < contacts.size(); i++) {
        ContactData contactData = contacts.get(i);
        op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, contactData.getAccountType())
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, contactData.getAccountName())
            .withValue(ContactsContract.RawContacts.STARRED, contactData.isFavorite() ? 1 : 0)
            .build());
    }
    try {
        results = mResolver.applyBatch(ContactsContract.AUTHORITY, op_list);
    } catch (Exception ignored) {
    }
    return results;
}
 
Example #5
Source Project: narrate-android   Author: timothymiko   File: DataProvider.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example #6
Source Project: Cirrus_depricated   Author: skymania   File: FileContentProvider.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example #7
Source Project: espresso-macchiato   Author: nenick   File: EspContactTool.java    License: MIT License 6 votes vote down vote up
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);
        }
    }
 
Example #8
Source Project: device-database   Author: android-database-best-practices   File: DevicesProvider.java    License: Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example #9
Source Project: tindroid   Author: tinode   File: BatchOperation.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("UnusedReturnValue")
List<Uri> execute() {
    List<Uri> resultUris = new ArrayList<>();
    if (mOperations.size() == 0) {
        return resultUris;
    }
    // Apply the mOperations to the content provider
    try {
        ContentProviderResult[] results = mResolver.applyBatch(ContactsContract.AUTHORITY, mOperations);
        if (results.length > 0) {
            for (ContentProviderResult result : results) {
                resultUris.add(result.uri);
            }
        }
    } catch (final OperationApplicationException | RemoteException e) {
        Log.e(TAG, "storing contact data failed", e);
    }
    mOperations.clear();
    return resultUris;
}
 
Example #10
Source Project: make-your-app-material   Author: DmitryMalkovich   File: ItemsProvider.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example #11
Source Project: RememBirthday   Author: Kunzisoft   File: EventLoader.java    License: GNU General Public License v3.0 6 votes vote down vote up
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());
        }
    }
}
 
Example #12
Source Project: RememBirthday   Author: Kunzisoft   File: EventLoader.java    License: GNU General Public License v3.0 6 votes vote down vote up
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());
    }
}
 
Example #13
Source Project: RememBirthday   Author: Kunzisoft   File: ContactProvider.java    License: GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example #14
Source Project: RememBirthday   Author: Kunzisoft   File: ContactProvider.java    License: GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example #15
Source Project: CPOrm   Author: Wackymax   File: TransactionHelper.java    License: MIT License 6 votes vote down vote up
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);
            }
        }
    }
 
Example #16
Source Project: android-atleap   Author: blandware   File: SQLiteProvider.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    ContentProviderResult[] result = null;

    SQLiteDatabase db = this.getDatabaseHelper().getWritableDatabase();

    db.beginTransaction();
    try {
        result = super.applyBatch(operations);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    return result;
}
 
Example #17
Source Project: fdroidclient   Author: f-droid   File: FDroidProvider.java    License: GNU General Public License v3.0 6 votes vote down vote up
@NonNull
@Override
public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    ContentProviderResult[] result = null;
    applyingBatch = true;
    final SQLiteDatabase db = db();
    db.beginTransaction();
    try {
        result = super.applyBatch(operations);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        applyingBatch = false;
    }
    return result;
}
 
Example #18
Source Project: v2ex   Author: taoliuh   File: V2exProvider.java    License: Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example #19
Source Project: callmeter   Author: felixb   File: DataProvider.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(
        @NonNull final ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    Log.d(TAG, "applyBatch(#", operations.size(), ")");
    ContentProviderResult[] ret = null;
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    assert db != null;
    db.beginTransaction();
    try {
        ret = super.applyBatch(operations);
        db.setTransactionSuccessful();
    } catch (SQLException e) {
        Log.e(TAG, "error applying batch");
        throw e;
    } finally {
        db.endTransaction();
    }
    return ret;
}
 
Example #20
Source Project: LaunchEnr   Author: enricocid   File: LauncherProvider.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    createDbIfNotExists();
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    db.beginTransaction();
    try {
        ContentProviderResult[] result =  super.applyBatch(operations);
        db.setTransactionSuccessful();
        reloadLauncherIfExternal();
        return result;
    } finally {
        db.endTransaction();
    }
}
 
Example #21
Source Project: AndroidContacts   Author: blainepwnz   File: ContactsSaver.java    License: MIT License 5 votes vote down vote up
public int[] insertContacts(List<ContactData> contactDataList) {
    ArrayList<ContentValues> cvList = new ArrayList<>(100);

    ContentProviderResult[] results = createContacts(contactDataList);
    int[] ids = new int[results.length];
    for (int i = 0; i < results.length; i++) {
        int id = Integer.parseInt(results[i].uri.getLastPathSegment());
        generateInsertOperations(cvList, contactDataList.get(i), id);
        ids[i] = id;
    }
    mResolver.bulkInsert(ContactsContract.Data.CONTENT_URI, cvList.toArray(new ContentValues[cvList.size()]));
    return ids;
}
 
Example #22
Source Project: play-apk-expansion   Author: google   File: APEZProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(
        ArrayList<ContentProviderOperation> operations)
                throws OperationApplicationException {
    initIfNecessary();
    return super.applyBatch(operations);
}
 
Example #23
Source Project: glide-support   Author: TWiStErRob   File: APEZProvider.java    License: The Unlicense 5 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(
		ArrayList<ContentProviderOperation> operations)
		throws OperationApplicationException {
       initIfNecessary();
	return super.applyBatch(operations);
}
 
Example #24
Source Project: Trebuchet   Author: talentlo   File: LauncherProvider.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    db.beginTransaction();
    try {
        ContentProviderResult[] result =  super.applyBatch(operations);
        db.setTransactionSuccessful();
        reloadLauncherIfExternal();
        return result;
    } finally {
        db.endTransaction();
    }
}
 
Example #25
Source Project: RememBirthday   Author: Kunzisoft   File: ReminderCalendarProviderObserver.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Apply operations
 */
private void applyBatch() {
    try {
        ContentProviderResult[] contentProviderResults =
                contentResolver.applyBatch(CalendarContract.AUTHORITY, ops);
        for(ContentProviderResult result : contentProviderResults)
            if(result.uri != null)
                Log.d(this.getClass().getSimpleName(), result.uri.toString());
    } catch (RemoteException|OperationApplicationException e) {
        Log.e(this.getClass().getSimpleName(), e.getMessage());
    } finally {
        ops.clear();
    }
}
 
Example #26
Source Project: stetho   Author: facebook   File: APODContentProvider.java    License: MIT License 5 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
    throws OperationApplicationException {
  SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  db.beginTransaction();
  try {
    ContentProviderResult[] results = super.applyBatch(operations);
    db.setTransactionSuccessful();
    return results;
  } finally {
    db.endTransaction();
    notifyChange();
  }
}
 
Example #27
Source Project: attendee-checkin   Author: googlearchive   File: GutenbergProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(
        @NonNull ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    SQLiteDatabase db = mHelper.getWritableDatabase();
    db.beginTransaction();
    try {
        ContentProviderResult[] result = super.applyBatch(operations);
        db.setTransactionSuccessful();
        return result;
    } finally {
        db.endTransaction();
    }
}
 
Example #28
Source Project: TurboLauncher   Author: Phonemetra   File: LauncherProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    db.beginTransaction();
    try {
        ContentProviderResult[] result =  super.applyBatch(operations);
        db.setTransactionSuccessful();
        return result;
    } finally {
        db.endTransaction();
    }
}
 
Example #29
Source Project: Alite   Author: CmdrStardust   File: APEZProvider.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(
		ArrayList<ContentProviderOperation> operations)
		throws OperationApplicationException {
       initIfNecessary();
	return super.applyBatch(operations);
}
 
Example #30
Source Project: CPOrm   Author: Wackymax   File: CPOrmContentProvider.java    License: MIT License 5 votes vote down vote up
@NonNull
@Override
public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {

    if (debugEnabled) {
        CPOrmLog.d("********* Apply Batch **********");
        CPOrmLog.d("Operations Count: " + operations.size());
    }

    isBatchOperation.set(true);
    changedUri.set(new LinkedHashSet<Uri>());

    boolean success = false;
    ContentProviderResult[] contentProviderResults = null;
    SQLiteDatabase db = database.getWritableDatabase();
    try {
        db.beginTransactionNonExclusive();
        contentProviderResults = super.applyBatch(operations);
        db.setTransactionSuccessful();
        success = true;
        return contentProviderResults;
    } finally {
        db.endTransaction();

        if(success && changedUri.get() != null) {
            for (Uri uri : changedUri.get()) {

                if (uri != null) {
                    TableDetails tableDetails = uriMatcherHelper.getTableDetails(uri);
                    notifyChanges(uri, tableDetails);
                }
            }
        }
        isBatchOperation.set(false);
        changedUri.remove();
    }
}