Search in sources :

Example 16 with ContentProviderOperation

use of android.content.ContentProviderOperation in project android by owncloud.

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, "Sending " + operations.size() + " operations to FileContentProvider");
            try {
                if (getContentResolver() != null) {
                    results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
                } else {
                    results = getContentProviderClient().applyBatch(operations);
                }
            } catch (OperationApplicationException e) {
                Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
            } catch (RemoteException e) {
                Log_OC.e(TAG, "Exception in batch of operations  " + e.getMessage());
            }
        }
    }
}
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 17 with ContentProviderOperation

use of android.content.ContentProviderOperation in project android by owncloud.

the class FileDataStorageManager method saveSharesDB.

public void saveSharesDB(ArrayList<OCShare> shares) {
    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
    // Reset flags & Remove shares for this files
    String filePath = "";
    for (OCShare share : shares) {
        if (filePath != share.getPath()) {
            filePath = share.getPath();
            resetShareFlagInAFile(filePath);
            operations = prepareRemoveSharesInFile(filePath, operations);
        }
    }
    // Add operations to insert shares
    operations = prepareInsertShares(shares, operations);
    // apply operations in batch
    if (operations.size() > 0) {
        Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
        try {
            if (getContentResolver() != null) {
                getContentResolver().applyBatch(MainApp.getAuthority(), operations);
            } else {
                getContentProviderClient().applyBatch(operations);
            }
        } catch (OperationApplicationException e) {
            Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
        } catch (RemoteException e) {
            Log_OC.e(TAG, "Exception in batch of operations  " + e.getMessage());
        }
    }
//        // TODO: review if it is needed
//        // Update shared files
//        ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
//
//        for (OCShare share : shares) {
//            // Get the path
//            String path = share.getPath();
//            if (share.isFolder()) {
//                path = path + FileUtils.PATH_SEPARATOR;
//            }
//
//            // Update OCFile with data from share: ShareByLink, publicLink and
//            OCFile file = getFileByPath(path);
//            if (file != null) {
//                if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
//                    file.setShareViaLink(true);
//                    sharedFiles.add(file);
//                }
//            }
//        }
//
//        // TODO: Review
//        updateSharedFiles(sharedFiles);
}
Also used : 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 18 with ContentProviderOperation

use of android.content.ContentProviderOperation in project android by owncloud.

the class FileContentProvider method applyBatch.

@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();
    // it's supposed that transactions can be nested
    db.beginTransaction();
    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;
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 19 with ContentProviderOperation

use of android.content.ContentProviderOperation in project robolectric by robolectric.

the class ShadowContentResolverTest method applyBatchForRegisteredProvider.

@SuppressWarnings("serial")
@Test
public void applyBatchForRegisteredProvider() throws RemoteException, OperationApplicationException {
    final ArrayList<String> operations = new ArrayList<>();
    ShadowContentResolver.registerProviderInternal("registeredProvider", new ContentProvider() {

        @Override
        public boolean onCreate() {
            return true;
        }

        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            operations.add("query");
            MatrixCursor cursor = new MatrixCursor(new String[] { "a" });
            cursor.addRow(new Object[] { "b" });
            return cursor;
        }

        @Override
        public String getType(Uri uri) {
            return null;
        }

        @Override
        public Uri insert(Uri uri, ContentValues values) {
            operations.add("insert");
            return ContentUris.withAppendedId(uri, 1);
        }

        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            operations.add("delete");
            return 0;
        }

        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            operations.add("update");
            return 0;
        }
    });
    final Uri uri = Uri.parse("content://registeredProvider/path");
    contentResolver.applyBatch("registeredProvider", new ArrayList<ContentProviderOperation>() {

        {
            add(ContentProviderOperation.newInsert(uri).withValue("a", "b").build());
            add(ContentProviderOperation.newUpdate(uri).withValue("a", "b").build());
            add(ContentProviderOperation.newDelete(uri).build());
            add(ContentProviderOperation.newAssertQuery(uri).withValue("a", "b").build());
        }
    });
    assertThat(operations).containsExactly("insert", "update", "delete", "query");
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ContentProvider(android.content.ContentProvider) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) BaseCursor(org.robolectric.fakes.BaseCursor) Uri(android.net.Uri) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Example 20 with ContentProviderOperation

use of android.content.ContentProviderOperation in project robolectric by robolectric.

the class ShadowContentResolverTest method applyBatchForUnregisteredProvider.

@Test
public void applyBatchForUnregisteredProvider() throws RemoteException, OperationApplicationException {
    List<ContentProviderOperation> resultOperations = shadowContentResolver.getContentProviderOperations(AUTHORITY);
    assertThat(resultOperations).isNotNull();
    assertThat(resultOperations.size()).isEqualTo(0);
    ContentProviderResult[] contentProviderResults = new ContentProviderResult[] { new ContentProviderResult(1), new ContentProviderResult(1) };
    shadowContentResolver.setContentProviderResult(contentProviderResults);
    Uri uri = Uri.parse("content://org.robolectric");
    ArrayList<ContentProviderOperation> operations = new ArrayList<>();
    operations.add(ContentProviderOperation.newInsert(uri).withValue("column1", "foo").withValue("column2", 5).build());
    operations.add(ContentProviderOperation.newUpdate(uri).withSelection("id_column", new String[] { "99" }).withValue("column1", "bar").build());
    operations.add(ContentProviderOperation.newDelete(uri).withSelection("id_column", new String[] { "11" }).build());
    ContentProviderResult[] result = contentResolver.applyBatch(AUTHORITY, operations);
    resultOperations = shadowContentResolver.getContentProviderOperations(AUTHORITY);
    assertThat(resultOperations).isEqualTo(operations);
    assertThat(result).isEqualTo(contentProviderResults);
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Uri(android.net.Uri) Test(org.junit.Test)

Aggregations

ContentProviderOperation (android.content.ContentProviderOperation)75 ArrayList (java.util.ArrayList)53 OperationApplicationException (android.content.OperationApplicationException)34 Uri (android.net.Uri)27 ContentValues (android.content.ContentValues)25 RemoteException (android.os.RemoteException)22 Cursor (android.database.Cursor)14 ContentProviderResult (android.content.ContentProviderResult)13 ContentResolver (android.content.ContentResolver)9 LinkedList (java.util.LinkedList)8 IOException (java.io.IOException)7 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)6 Intent (android.content.Intent)5 List (java.util.List)5 SuppressLint (android.annotation.SuppressLint)4 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 IntentFilter (android.content.IntentFilter)4