Search in sources :

Example 46 with OperationApplicationException

use of android.content.OperationApplicationException in project Meltdown by phubbard.

the class MeltdownApp method doCPtest.

public void doCPtest() {
    ContentResolver cr = getContentResolver();
    List<Integer> items = fetchUnreadItemsIDs();
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    // Get data in max-fever-size chunks
    final int PER_FETCH = 50;
    final int num_fetches = (int) Math.ceil(items.size() / PER_FETCH) + 1;
    for (int idx = 0; idx < num_fetches; idx++) {
        int left_index = idx * PER_FETCH;
        int right_index = Math.min((idx + 1) * PER_FETCH, items.size());
        Log.d(TAG, "On run " + idx + " pulling from " + left_index + " to " + (right_index - 1) + " of " + items.size());
        List<Integer> ids = items.subList(left_index, right_index);
        String payload = xcvr.fetchListOfItems(ids);
        for (int j = 0; j < ids.size(); j++) {
            JSONArray jitems;
            RssItem this_item;
            try {
                JSONObject jdata = new JSONObject(payload);
                jitems = jdata.getJSONArray("items");
                this_item = new RssItem(jitems.getJSONObject(j));
                ops.add(ContentProviderOperation.newInsert(ItemProvider.URI).withYieldAllowed(true).withValues(this_item.getCV()).build());
            } catch (JSONException je) {
                Log.e(TAG, "JSON error in CP import logic: ", je);
            } finally {
            }
        }
    }
    try {
        cr.applyBatch(ItemProvider.AUTHORITY, ops);
    } catch (RemoteException e) {
        Log.e(TAG, "Remote exception in bulk save", e);
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        Log.e(TAG, "OAE exception in bulk save", e);
        e.printStackTrace();
    }
    cr.notifyChange(ItemProvider.URI, null, false);
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ContentResolver(android.content.ContentResolver) JSONObject(org.json.JSONObject) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 47 with OperationApplicationException

use of android.content.OperationApplicationException in project android_frameworks_base by ResurrectionRemix.

the class TvInputManagerService method registerBroadcastReceivers.

private void registerBroadcastReceivers() {
    PackageMonitor monitor = new PackageMonitor() {

        private void buildTvInputList(String[] packages) {
            synchronized (mLock) {
                if (mCurrentUserId == getChangingUserId()) {
                    buildTvInputListLocked(mCurrentUserId, packages);
                    buildTvContentRatingSystemListLocked(mCurrentUserId);
                }
            }
        }

        @Override
        public void onPackageUpdateFinished(String packageName, int uid) {
            if (DEBUG)
                Slog.d(TAG, "onPackageUpdateFinished(packageName=" + packageName + ")");
            // This callback is invoked when the TV input is reinstalled.
            // In this case, isReplacing() always returns true.
            buildTvInputList(new String[] { packageName });
        }

        @Override
        public void onPackagesAvailable(String[] packages) {
            if (DEBUG) {
                Slog.d(TAG, "onPackagesAvailable(packages=" + Arrays.toString(packages) + ")");
            }
            // available.
            if (isReplacing()) {
                buildTvInputList(packages);
            }
        }

        @Override
        public void onPackagesUnavailable(String[] packages) {
            // unavailable.
            if (DEBUG) {
                Slog.d(TAG, "onPackagesUnavailable(packages=" + Arrays.toString(packages) + ")");
            }
            if (isReplacing()) {
                buildTvInputList(packages);
            }
        }

        @Override
        public void onSomePackagesChanged() {
            // the TV inputs.
            if (DEBUG)
                Slog.d(TAG, "onSomePackagesChanged()");
            if (isReplacing()) {
                if (DEBUG)
                    Slog.d(TAG, "Skipped building TV input list due to replacing");
                // methods instead.
                return;
            }
            buildTvInputList(null);
        }

        @Override
        public boolean onPackageChanged(String packageName, int uid, String[] components) {
            // the update can be handled in {@link #onSomePackagesChanged}.
            return true;
        }

        @Override
        public void onPackageRemoved(String packageName, int uid) {
            synchronized (mLock) {
                UserState userState = getOrCreateUserStateLocked(getChangingUserId());
                if (!userState.packageSet.contains(packageName)) {
                    // Not a TV input package.
                    return;
                }
            }
            ArrayList<ContentProviderOperation> operations = new ArrayList<>();
            String selection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=?";
            String[] selectionArgs = { packageName };
            operations.add(ContentProviderOperation.newDelete(TvContract.Channels.CONTENT_URI).withSelection(selection, selectionArgs).build());
            operations.add(ContentProviderOperation.newDelete(TvContract.Programs.CONTENT_URI).withSelection(selection, selectionArgs).build());
            operations.add(ContentProviderOperation.newDelete(TvContract.WatchedPrograms.CONTENT_URI).withSelection(selection, selectionArgs).build());
            ContentProviderResult[] results = null;
            try {
                ContentResolver cr = getContentResolverForUser(getChangingUserId());
                results = cr.applyBatch(TvContract.AUTHORITY, operations);
            } catch (RemoteException | OperationApplicationException e) {
                Slog.e(TAG, "error in applyBatch", e);
            }
            if (DEBUG) {
                Slog.d(TAG, "onPackageRemoved(packageName=" + packageName + ", uid=" + uid + ")");
                Slog.d(TAG, "results=" + results);
            }
        }
    };
    monitor.register(mContext, null, UserHandle.ALL, true);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
    intentFilter.addAction(Intent.ACTION_USER_REMOVED);
    mContext.registerReceiverAsUser(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_USER_SWITCHED.equals(action)) {
                switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
                removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            }
        }
    }, UserHandle.ALL, intentFilter, null, null);
}
Also used : Context(android.content.Context) ContentProviderResult(android.content.ContentProviderResult) IntentFilter(android.content.IntentFilter) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) PackageMonitor(com.android.internal.content.PackageMonitor) ContentResolver(android.content.ContentResolver) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 48 with OperationApplicationException

use of android.content.OperationApplicationException in project android by nextcloud.

the class FileDataStorageManager method saveShares.

public void saveShares(Collection<OCShare> shares) {
    cleanShares();
    if (shares != null) {
        ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(shares.size());
        // prepare operations to insert or update files to save in the given folder
        for (OCShare share : shares) {
            ContentValues cv = new ContentValues();
            cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
            cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
            cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
            cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
            cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
            cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
            cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
            cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
            cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
            cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName());
            cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
            cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
            cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
            cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
            if (shareExistsForRemoteId(share.getRemoteId())) {
                // updating an existing file
                operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).withValues(cv).withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", new String[] { String.valueOf(share.getRemoteId()) }).build());
            } else {
                // adding a new file
                operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).withValues(cv).build());
            }
        }
        // apply operations in batch
        if (operations.size() > 0) {
            @SuppressWarnings("unused") ContentProviderResult[] results = null;
            Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
            try {
                if (getContentResolver() != null) {
                    results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
                } else {
                    results = getContentProviderClient().applyBatch(operations);
                }
            } catch (OperationApplicationException | RemoteException e) {
                Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
            }
        }
    }
}
Also used : ContentValues(android.content.ContentValues) ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) OCShare(com.owncloud.android.lib.resources.shares.OCShare) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 49 with OperationApplicationException

use of android.content.OperationApplicationException 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 50 with OperationApplicationException

use of android.content.OperationApplicationException 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)

Aggregations

OperationApplicationException (android.content.OperationApplicationException)67 ContentProviderOperation (android.content.ContentProviderOperation)60 ArrayList (java.util.ArrayList)55 RemoteException (android.os.RemoteException)46 ContentProviderResult (android.content.ContentProviderResult)23 ContentValues (android.content.ContentValues)17 ContentResolver (android.content.ContentResolver)12 Cursor (android.database.Cursor)11 Uri (android.net.Uri)11 IOException (java.io.IOException)11 Intent (android.content.Intent)9 OCShare (com.owncloud.android.lib.resources.shares.OCShare)6 LinkedList (java.util.LinkedList)6 List (java.util.List)6 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)6 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 IntentFilter (android.content.IntentFilter)4