Search in sources :

Example 11 with ContentProviderResult

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

the class AnnotatedCallLogContentProvider method applyBatch.

/**
 * {@inheritDoc}
 *
 * <p>Note: When applyBatch is used with the AnnotatedCallLog, only a single notification for the
 * content URI is generated, not individual notifications for each affected URI.
 */
@NonNull
@Override
public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    ContentProviderResult[] results = new ContentProviderResult[operations.size()];
    if (operations.isEmpty()) {
        return results;
    }
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    try {
        applyingBatch.set(true);
        database.beginTransaction();
        for (int i = 0; i < operations.size(); i++) {
            ContentProviderOperation operation = operations.get(i);
            int match = uriMatcher.match(operation.getUri());
            switch(match) {
                case ANNOTATED_CALL_LOG_TABLE_CODE:
                case ANNOTATED_CALL_LOG_TABLE_ID_CODE:
                    // These are allowed values, continue.
                    break;
                case COALESCED_ANNOTATED_CALL_LOG_TABLE_CODE:
                    throw new UnsupportedOperationException("coalesced call log does not support applyBatch");
                default:
                    throw new IllegalArgumentException("Unknown uri: " + operation.getUri());
            }
            ContentProviderResult result = operation.apply(this, results, i);
            if (operations.get(i).isInsert()) {
                if (result.uri == null) {
                    throw new OperationApplicationException("error inserting row");
                }
            } else if (result.count == 0) {
                /*
           * The batches built by MutationApplier happen to contain operations in order of:
           *
           * 1. Inserts
           * 2. Updates
           * 3. Deletes
           *
           * Let's say the last row in the table is row Z, and MutationApplier wishes to update it,
           * as well as insert row A. When row A gets inserted, row Z will be deleted via the
           * trigger if the table is full. Then later, when we try to process the update for row Z,
           * it won't exist.
           */
                LogUtil.w("AnnotatedCallLogContentProvider.applyBatch", "update or delete failed, possibly because row got cleaned up");
            }
            results[i] = result;
        }
        database.setTransactionSuccessful();
    } finally {
        applyingBatch.set(false);
        database.endTransaction();
    }
    notifyChange(AnnotatedCallLog.CONTENT_URI);
    return results;
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) OperationApplicationException(android.content.OperationApplicationException) NonNull(android.support.annotation.NonNull)

Example 12 with ContentProviderResult

use of android.content.ContentProviderResult in project fdroidclient by f-droid.

the class FDroidProvider method applyBatch.

@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;
}
Also used : ContentProviderResult(android.content.ContentProviderResult) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) NonNull(android.support.annotation.NonNull)

Example 13 with ContentProviderResult

use of android.content.ContentProviderResult in project iosched by google.

the class ScheduleProvider method applyBatch.

/**
 * 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();
    }
}
Also used : ContentProviderResult(android.content.ContentProviderResult) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 14 with ContentProviderResult

use of android.content.ContentProviderResult in project AndroidLife by CaMnter.

the class RemoteContentProvider method applyBatch.

/**
 * 反射替换 ContentProviderOperation 集合中所有的 ContentProviderOperation # Uri mUri
 *
 * 在 ContentProviderOperation 集合存在数据的情况下
 * 取出每个 ContentProviderOperation 对应的插件 ContentProvider
 * 手动调用 ContentProvider # applyBatch(...)
 *
 * @param operations operations
 * @return ContentProviderResult[]
 * @throws OperationApplicationException OperationApplicationException
 */
@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)

Example 15 with ContentProviderResult

use of android.content.ContentProviderResult in project network-monitor by caarmen.

the class NetMonProvider method applyBatch.

/**
 * Perform all operations in a single transaction and notify all relevant URIs at the end.
 *
 * @see android.content.ContentProvider#applyBatch(java.util.ArrayList)
 */
@Override
@NonNull
public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    Log.v(TAG, "applyBatch: " + operations.size());
    Set<Uri> urisToNotify = StreamSupport.stream(operations).map(ContentProviderOperation::getUri).collect(Collectors.toSet());
    Log.v(TAG, "applyBatch: will notify these uris after persisting: " + urisToNotify);
    SQLiteDatabase db = mNetworkMonitorDatabase.getWritableDatabase();
    db.beginTransaction();
    try {
        int batchSize = 100;
        int operationsProcessed = 0;
        ContentProviderResult[] result = new ContentProviderResult[operations.size()];
        while (!operations.isEmpty()) {
            ArrayList<ContentProviderOperation> batch = new ArrayList<>(batchSize);
            for (int i = 0; i < batchSize && !operations.isEmpty(); i++) batch.add(operations.remove(0));
            Log.v(TAG, "applyBatch of " + batch.size() + " operations");
            ContentProviderResult[] batchResult = super.applyBatch(batch);
            System.arraycopy(batchResult, 0, result, operationsProcessed, batchResult.length);
            operationsProcessed += batch.size();
        }
        db.setTransactionSuccessful();
        for (Uri uri : urisToNotify) mContext.getContentResolver().notifyChange(uri, null);
        return result;
    } finally {
        db.endTransaction();
    }
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Uri(android.net.Uri) 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