Search in sources :

Example 26 with ContentProviderResult

use of android.content.ContentProviderResult in project GankTouTiao by heqiangflytosky.

the class SQLiteContentProvider method applyBatch.

@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    int ypCount = 0;
    int opCount = 0;
    boolean callerIsSyncAdapter = false;
    mDb = mOpenHelper.getWritableDatabase();
    mDb.beginTransaction();
    try {
        mApplyingBatch.set(true);
        final int numOperations = operations.size();
        final ContentProviderResult[] results = new ContentProviderResult[numOperations];
        for (int i = 0; i < numOperations; i++) {
            if (++opCount >= MAX_OPERATIONS_PER_YIELD_POINT) {
                throw new OperationApplicationException("Too many content provider operations between yield points. " + "The maximum number of operations per yield point is " + MAX_OPERATIONS_PER_YIELD_POINT, ypCount);
            }
            final ContentProviderOperation operation = operations.get(i);
            if (!callerIsSyncAdapter && isCallerSyncAdapter(operation.getUri())) {
                callerIsSyncAdapter = true;
            }
            if (i > 0 && operation.isYieldAllowed()) {
                opCount = 0;
                if (mDb.yieldIfContendedSafely(SLEEP_AFTER_YIELD_DELAY)) {
                    ypCount++;
                }
            }
            results[i] = operation.apply(this, results, i);
        }
        mDb.setTransactionSuccessful();
        return results;
    } finally {
        mApplyingBatch.set(false);
        mDb.endTransaction();
        onEndTransaction(callerIsSyncAdapter);
    }
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) OperationApplicationException(android.content.OperationApplicationException)

Example 27 with ContentProviderResult

use of android.content.ContentProviderResult in project android_packages_apps_Gallery2 by LineageOS.

the class SQLiteContentProvider method applyBatch.

@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    int ypCount = 0;
    int opCount = 0;
    boolean callerIsSyncAdapter = false;
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    db.beginTransaction();
    try {
        mApplyingBatch.set(true);
        final int numOperations = operations.size();
        final ContentProviderResult[] results = new ContentProviderResult[numOperations];
        for (int i = 0; i < numOperations; i++) {
            if (++opCount >= MAX_OPERATIONS_PER_YIELD_POINT) {
                throw new OperationApplicationException("Too many content provider operations between yield points. " + "The maximum number of operations per yield point is " + MAX_OPERATIONS_PER_YIELD_POINT, ypCount);
            }
            final ContentProviderOperation operation = operations.get(i);
            if (!callerIsSyncAdapter && isCallerSyncAdapter(operation.getUri())) {
                callerIsSyncAdapter = true;
            }
            if (i > 0 && operation.isYieldAllowed()) {
                opCount = 0;
                if (db.yieldIfContendedSafely(SLEEP_AFTER_YIELD_DELAY)) {
                    ypCount++;
                }
            }
            results[i] = operation.apply(this, results, i);
        }
        db.setTransactionSuccessful();
        return results;
    } finally {
        mApplyingBatch.set(false);
        db.endTransaction();
        onEndTransaction(callerIsSyncAdapter);
    }
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) OperationApplicationException(android.content.OperationApplicationException)

Example 28 with ContentProviderResult

use of android.content.ContentProviderResult in project orgzly-android by orgzly.

the class NotesClient method create.

/**
 * Insert as last note if position is not specified.
 */
public static Note create(Context context, Note note, NotePlace target, long time) {
    ContentValues values = new ContentValues();
    toContentValues(values, note);
    Uri insertUri;
    if (target != null) {
        /* Create note relative to an existing note. */
        insertUri = ProviderContract.Notes.ContentUri.notesIdTarget(target);
    } else {
        /* Create as last note. */
        insertUri = ProviderContract.Notes.ContentUri.notes();
    }
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    /* Insert note. */
    ops.add(ContentProviderOperation.newInsert(insertUri).withValues(values).build());
    /* Add each of the note's property. */
    int i = 0;
    OrgProperties properties = note.getHead().getProperties();
    for (String name : properties.keySet()) {
        String value = properties.get(name);
        values = new ContentValues();
        values.put(ProviderContract.NoteProperties.Param.NAME, name);
        values.put(ProviderContract.NoteProperties.Param.VALUE, value);
        values.put(ProviderContract.NoteProperties.Param.POSITION, i++);
        ops.add(ContentProviderOperation.newInsert(ProviderContract.NoteProperties.ContentUri.notesProperties()).withValues(values).withValueBackReference(ProviderContract.NoteProperties.Param.NOTE_ID, 0).build());
    }
    // Update book's modification time
    ops.add(ContentProviderOperation.newUpdate(ProviderContract.Books.ContentUri.books()).withValue(DbBook.MTIME, time).withSelection(DbBook._ID + " = " + note.getPosition().getBookId(), null).build());
    ContentProviderResult[] result;
    try {
        result = context.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    long noteId = ContentUris.parseId(result[0].uri);
    /* Update ID of newly inserted note. */
    note.setId(noteId);
    return note;
}
Also used : ContentValues(android.content.ContentValues) ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Uri(android.net.Uri) OrgProperties(com.orgzly.org.OrgProperties) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 29 with ContentProviderResult

use of android.content.ContentProviderResult in project orgzly-android by orgzly.

the class Provider method applyBatch.

@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    if (BuildConfig.LOG_DEBUG)
        LogUtils.d(TAG, operations.size());
    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;
}
Also used : ContentProviderResult(android.content.ContentProviderResult) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 30 with ContentProviderResult

use of android.content.ContentProviderResult in project VirtualAPK by didi.

the class RemoteContentProvider method applyBatch.

@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];
}
Also used : Field(java.lang.reflect.Field) ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) ContentProvider(android.content.ContentProvider) Uri(android.net.Uri) OperationApplicationException(android.content.OperationApplicationException) NonNull(android.support.annotation.NonNull)

Aggregations

ContentProviderResult (android.content.ContentProviderResult)39 ContentProviderOperation (android.content.ContentProviderOperation)30 OperationApplicationException (android.content.OperationApplicationException)22 ArrayList (java.util.ArrayList)20 RemoteException (android.os.RemoteException)17 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)12 ContentValues (android.content.ContentValues)9 Uri (android.net.Uri)9 ContentResolver (android.content.ContentResolver)6 Intent (android.content.Intent)6 NonNull (android.support.annotation.NonNull)6 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 IntentFilter (android.content.IntentFilter)4 PackageMonitor (com.android.internal.content.PackageMonitor)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 Test (org.junit.Test)3 ContentProvider (android.content.ContentProvider)2 OperationInfo (com.android.calendar.AsyncQueryServiceHelper.OperationInfo)2 OrgProperties (com.orgzly.org.OrgProperties)2