Search in sources :

Example 51 with OperationApplicationException

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

the class NotesClient method updateScheduledTime.

public static void updateScheduledTime(Context context, Set<Long> noteIds, OrgDateTime time) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    String noteIdsCommaSeparated = TextUtils.join(",", noteIds);
    /* Update notes. */
    ContentValues values = new ContentValues();
    if (time != null) {
        values.put(ProviderContract.Notes.UpdateParam.SCHEDULED_STRING, new OrgRange(time).toString());
    } else {
        values.putNull(ProviderContract.Notes.UpdateParam.SCHEDULED_STRING);
    }
    ops.add(ContentProviderOperation.newUpdate(ProviderContract.Notes.ContentUri.notes()).withValues(values).withSelection(ProviderContract.Notes.UpdateParam._ID + " IN (" + noteIdsCommaSeparated + ")", null).build());
    updateBooksMtimeForNotes(context, noteIdsCommaSeparated, ops);
    /*
         * Apply batch.
         */
    try {
        context.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException) OrgRange(com.orgzly.org.datetime.OrgRange) OperationApplicationException(android.content.OperationApplicationException)

Example 52 with OperationApplicationException

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

use of android.content.OperationApplicationException in project Signal-Android by WhisperSystems.

the class DirectoryHelper method updateContactsDatabase.

private static void updateContactsDatabase(@NonNull Context context, @NonNull Collection<RecipientId> activeIds, boolean removeMissing, @NonNull Map<String, String> rewrites) {
    if (!Permissions.hasAll(context, Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)) {
        Log.w(TAG, "[updateContactsDatabase] No contact permissions. Skipping.");
        return;
    }
    AccountHolder account = getOrCreateSystemAccount(context);
    if (account == null) {
        Log.w(TAG, "Failed to create an account!");
        return;
    }
    try {
        ContactsDatabase contactsDatabase = SignalDatabase.contacts();
        List<String> activeAddresses = Stream.of(activeIds).map(Recipient::resolved).filter(Recipient::hasE164).map(Recipient::requireE164).toList();
        contactsDatabase.removeDeletedRawContacts(account.getAccount());
        contactsDatabase.setRegisteredUsers(account.getAccount(), activeAddresses, removeMissing);
        syncRecipientInfoWithSystemContacts(context, rewrites);
    } catch (RemoteException | OperationApplicationException e) {
        Log.w(TAG, "Failed to update contacts.", e);
    }
}
Also used : Recipient(org.thoughtcrime.securesms.recipients.Recipient) ContactsDatabase(org.thoughtcrime.securesms.contacts.ContactsDatabase) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 54 with OperationApplicationException

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

the class FileDataStorageManager method updateSharedFiles.

public void updateSharedFiles(Collection<OCFile> sharedFiles) {
    resetShareFlagsInAllFiles();
    if (sharedFiles != null) {
        ArrayList<ContentProviderOperation> operations = new ArrayList<>(sharedFiles.size());
        // prepare operations to insert or update files to save in the given folder
        for (OCFile file : sharedFiles) {
            ContentValues cv = new ContentValues();
            cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
            cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
            cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
            cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
            cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimeType());
            cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
            cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
            cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
            if (!file.isFolder()) {
                cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
            }
            cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, user.getAccountName());
            cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
            cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
            cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
            cv.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, file.getEtagOnServer());
            cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
            cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
            cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
            cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
            cv.put(ProviderTableMeta.FILE_FAVORITE, file.isFavorite());
            cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.isUpdateThumbnailNeeded() ? 1 : 0);
            cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading() ? 1 : 0);
            cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
            boolean existsByPath = fileExists(file.getRemotePath());
            if (existsByPath || fileExists(file.getFileId())) {
                // 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());
            }
        }
        // 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) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 55 with OperationApplicationException

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

the class FileDataStorageManager method saveSharesDB.

public void saveSharesDB(List<OCShare> shares) {
    ArrayList<ContentProviderOperation> operations = new ArrayList<>();
    // Reset flags & Remove shares for this files
    String filePath = "";
    for (OCShare share : shares) {
        if (!filePath.equals(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);
        }
    }
}
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)

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