Search in sources :

Example 16 with OperationApplicationException

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

the class FileDataStorageManager method saveFolder.

/**
 * Inserts or updates the list of files contained in a given folder.
 *
 * CALLER IS RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
 *
 * @param folder
 * @param updatedFiles
 * @param filesToRemove
 */
public void saveFolder(OCFile folder, Collection<OCFile> updatedFiles, Collection<OCFile> filesToRemove) {
    Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size() + " children and " + filesToRemove.size() + " files to remove");
    ArrayList<ContentProviderOperation> operations = new ArrayList<>(updatedFiles.size());
    // prepare operations to insert or update files to save in the given folder
    for (OCFile file : updatedFiles) {
        ContentValues cv = createContentValueForFile(file, folder);
        if (fileExists(file.getFileId()) || fileExists(file.getRemotePath())) {
            // updating an existing file
            operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).withValues(cv).withSelection(ProviderTableMeta._ID + "=?", new String[] { String.valueOf(file.getFileId()) }).build());
        } else {
            // adding a new file
            operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build());
        }
    }
    // prepare operations to remove files in the given folder
    String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?";
    String[] whereArgs;
    for (OCFile file : filesToRemove) {
        if (file.getParentId() == folder.getFileId()) {
            whereArgs = new String[] { mAccount.name, file.getRemotePath() };
            if (file.isFolder()) {
                operations.add(ContentProviderOperation.newDelete(ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, file.getFileId())).withSelection(where, whereArgs).build());
                File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
                if (localFolder.exists()) {
                    removeLocalFolder(localFolder);
                }
            } else {
                operations.add(ContentProviderOperation.newDelete(ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId())).withSelection(where, whereArgs).build());
                if (file.isDown()) {
                    String path = file.getStoragePath();
                    new File(path).delete();
                    // notify MediaScanner about removed file
                    triggerMediaScan(path);
                }
            }
        }
    }
    // update metadata of folder
    ContentValues cv = createContentValueForFile(folder);
    operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).withValues(cv).withSelection(ProviderTableMeta._ID + "=?", new String[] { String.valueOf(folder.getFileId()) }).build());
    // apply operations in batch
    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);
    }
    // update new id in file objects for insertions
    if (results != null) {
        long newId;
        Iterator<OCFile> filesIt = updatedFiles.iterator();
        OCFile file = null;
        for (ContentProviderResult result : results) {
            if (filesIt.hasNext()) {
                file = filesIt.next();
            } else {
                file = null;
            }
            if (result.uri != null) {
                newId = Long.parseLong(result.uri.getPathSegments().get(1));
                // updatedFiles.get(i).setFileId(newId);
                if (file != null) {
                    file.setFileId(newId);
                }
            }
        }
    }
}
Also used : ContentValues(android.content.ContentValues) ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile) File(java.io.File) OperationApplicationException(android.content.OperationApplicationException)

Example 17 with OperationApplicationException

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

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, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
        try {
            if (getContentResolver() != null) {
                getContentResolver().applyBatch(MainApp.getAuthority(), operations);
            } else {
                getContentProviderClient().applyBatch(operations);
            }
        } catch (OperationApplicationException | RemoteException e) {
            Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
        }
    }
// // 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 OperationApplicationException

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

the class CurrentRooksClient method set.

public static void set(Context context, List<VersionedRook> books) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    /* Delete all previous. */
    ops.add(ContentProviderOperation.newDelete(ProviderContract.CurrentRooks.ContentUri.currentRooks()).build());
    /* Insert each one. */
    for (VersionedRook book : books) {
        ContentValues values = new ContentValues();
        CurrentRooksClient.toContentValues(values, book);
        ops.add(ContentProviderOperation.newInsert(ProviderContract.CurrentRooks.ContentUri.currentRooks()).withValues(values).build());
    }
    try {
        context.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
    }
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) VersionedRook(com.orgzly.android.repos.VersionedRook) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 19 with OperationApplicationException

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

the class NotesClient method update.

/**
 * Updates note by its ID.
 */
public static int update(Context context, Note note) {
    ContentValues values = new ContentValues();
    toContentValues(values, note);
    Uri noteUri = ProviderContract.Notes.ContentUri.notesId(note.getId());
    Uri uri = noteUri.buildUpon().appendQueryParameter("bookId", String.valueOf(note.getPosition().getBookId())).build();
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    /* Update note. */
    ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
    /* Delete all note's properties. */
    ops.add(ContentProviderOperation.newDelete(ProviderContract.NoteProperties.ContentUri.notesIdProperties(note.getId())).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.NOTE_ID, note.getId());
        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).build());
    }
    ContentProviderResult[] result;
    try {
        result = context.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    return result[0].count;
}
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 20 with OperationApplicationException

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

the class Shelf method reParseNotesStateAndTitles.

/**
 * Using current states configuration, update states and titles for all notes.
 * Keywords that were part of the title can become states and vice versa.
 */
public void reParseNotesStateAndTitles() throws IOException {
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    /* Get all notes. */
    Cursor cursor = mContext.getContentResolver().query(ProviderContract.Notes.ContentUri.notes(), null, null, null, null);
    try {
        OrgParser.Builder parserBuilder = new OrgParser.Builder().setTodoKeywords(AppPreferences.todoKeywordsSet(mContext)).setDoneKeywords(AppPreferences.doneKeywordsSet(mContext));
        OrgParserWriter parserWriter = new OrgParserWriter();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            /* Get current heading string. */
            Note note = NotesClient.fromCursor(cursor);
            /* Skip root node. */
            if (note.getPosition().getLevel() == 0) {
                continue;
            }
            OrgHead head = note.getHead();
            String headString = parserWriter.whiteSpacedHead(head, note.getPosition().getLevel(), false);
            /* Re-parse heading using current setting of keywords. */
            OrgParsedFile file = parserBuilder.setInput(headString).build().parse();
            if (BuildConfig.LOG_DEBUG)
                LogUtils.d(TAG, "Note " + note + " parsed has " + file.getHeadsInList().size() + " notes");
            if (file.getHeadsInList().size() != 1) {
                throw new IOException("Got " + file.getHeadsInList().size() + " notes after parsing \"" + headString + "\"");
            }
            OrgHead newHead = file.getHeadsInList().get(0).getHead();
            ContentValues values = new ContentValues();
            /* Update if state, title or priority are different. */
            if (!TextUtils.equals(newHead.getState(), head.getState()) || !TextUtils.equals(newHead.getTitle(), head.getTitle()) || !TextUtils.equals(newHead.getPriority(), head.getPriority())) {
                values.put(ProviderContract.Notes.UpdateParam.TITLE, newHead.getTitle());
                values.put(ProviderContract.Notes.UpdateParam.STATE, newHead.getState());
                values.put(ProviderContract.Notes.UpdateParam.PRIORITY, newHead.getPriority());
            }
            if (values.size() > 0) {
                ops.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(ProviderContract.Notes.ContentUri.notes(), cursor.getLong(0))).withValues(values).build());
            }
        }
    } finally {
        cursor.close();
    }
    /*
         * Apply batch.
         */
    try {
        mContext.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    notifyDataChanged(mContext);
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) OrgHead(com.orgzly.org.OrgHead) CircularArrayList(com.orgzly.android.util.CircularArrayList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Cursor(android.database.Cursor) OrgParserWriter(com.orgzly.org.parser.OrgParserWriter) OrgParsedFile(com.orgzly.org.parser.OrgParsedFile) OrgParser(com.orgzly.org.parser.OrgParser) DbNote(com.orgzly.android.provider.models.DbNote) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Aggregations

OperationApplicationException (android.content.OperationApplicationException)63 ContentProviderOperation (android.content.ContentProviderOperation)57 ArrayList (java.util.ArrayList)52 RemoteException (android.os.RemoteException)42 ContentProviderResult (android.content.ContentProviderResult)21 ContentValues (android.content.ContentValues)15 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 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 PackageMonitor (com.android.internal.content.PackageMonitor)4