Search in sources :

Example 31 with ContentProviderOperation

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

the class FileDataStorageManager method updateSharedFiles.

public void updateSharedFiles(Collection<OCFile> sharedFiles) {
    resetShareFlagsInAllFiles();
    if (sharedFiles != null) {
        ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(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, mAccount.name);
            cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
            cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
            cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.getAvailableOfflineStatus().getValue());
            cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
            cv.put(ProviderTableMeta.FILE_TREE_ETAG, file.getTreeEtag());
            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_PUBLIC_LINK, file.getPublicLink());
            cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
            cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
            cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail() ? 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
                setInitialAvailableOfflineStatus(file, cv);
                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, "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) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 32 with ContentProviderOperation

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

the class FileDataStorageManager method moveLocalFile.

/**
     * Updates database and file system for a file or folder that was moved to a different location.
     *
     * TODO explore better (faster) implementations
     * TODO throw exceptions up !
     */
public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) {
    if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
        OCFile targetParent = getFileByPath(targetParentPath);
        if (targetParent == null) {
            throw new IllegalStateException("Parent folder of the target path does not exist!!");
        }
        /// 1. get all the descendants of the moved element in a single QUERY
        Cursor c = null;
        if (getContentProviderClient() != null) {
            try {
                c = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI, null, ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ", new String[] { mAccount.name, file.getRemotePath() + "%" }, ProviderTableMeta.FILE_PATH + " ASC ");
            } catch (RemoteException e) {
                Log_OC.e(TAG, e.getMessage());
            }
        } else {
            c = getContentResolver().query(ProviderTableMeta.CONTENT_URI, null, ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ", new String[] { mAccount.name, file.getRemotePath() + "%" }, ProviderTableMeta.FILE_PATH + " ASC ");
        }
        List<String> originalPathsToTriggerMediaScan = new ArrayList<>();
        List<String> newPathsToTriggerMediaScan = new ArrayList<>();
        String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
        /// 2. prepare a batch of update operations to change all the descendants
        if (c != null) {
            ArrayList<ContentProviderOperation> operations = new ArrayList<>(c.getCount());
            if (c.moveToFirst()) {
                int lengthOfOldPath = file.getRemotePath().length();
                int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
                do {
                    // keep construction in the loop
                    ContentValues cv = new ContentValues();
                    OCFile child = createFileInstance(c);
                    cv.put(ProviderTableMeta.FILE_PATH, targetPath + child.getRemotePath().substring(lengthOfOldPath));
                    if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) {
                        // update link to downloaded content - but local move is not done here!
                        String targetLocalPath = defaultSavePath + targetPath + child.getStoragePath().substring(lengthOfOldStoragePath);
                        cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath);
                        originalPathsToTriggerMediaScan.add(child.getStoragePath());
                        newPathsToTriggerMediaScan.add(targetLocalPath);
                    }
                    if (targetParent.getAvailableOfflineStatus() != OCFile.AvailableOfflineStatus.NOT_AVAILABLE_OFFLINE) {
                        // moving to an available offline subfolder
                        cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE_PARENT.getValue());
                    } else {
                        // moving to a not available offline subfolder - with care
                        if (file.getAvailableOfflineStatus() == OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE_PARENT) {
                            cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, OCFile.AvailableOfflineStatus.NOT_AVAILABLE_OFFLINE.getValue());
                        }
                    }
                    if (child.getRemotePath().equals(file.getRemotePath())) {
                        cv.put(ProviderTableMeta.FILE_PARENT, targetParent.getFileId());
                    }
                    operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).withValues(cv).withSelection(ProviderTableMeta._ID + "=?", new String[] { String.valueOf(child.getFileId()) }).build());
                } while (c.moveToNext());
            }
            c.close();
            /// 3. apply updates in batch
            try {
                if (getContentResolver() != null) {
                    getContentResolver().applyBatch(MainApp.getAuthority(), operations);
                } else {
                    getContentProviderClient().applyBatch(operations);
                }
            } catch (Exception e) {
                Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", e);
            }
        }
        /// 4. move in local file system
        String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
        String targetLocalPath = defaultSavePath + targetPath;
        File localFile = new File(originalLocalPath);
        boolean renamed = false;
        if (localFile.exists()) {
            File targetFile = new File(targetLocalPath);
            File targetFolder = targetFile.getParentFile();
            if (!targetFolder.exists()) {
                targetFolder.mkdirs();
            }
            renamed = localFile.renameTo(targetFile);
        }
        if (renamed) {
            Iterator<String> it = originalPathsToTriggerMediaScan.iterator();
            while (it.hasNext()) {
                // Notify MediaScanner about removed file
                deleteFileInMediaScan(it.next());
            }
            it = newPathsToTriggerMediaScan.iterator();
            while (it.hasNext()) {
                // Notify MediaScanner about new file/folder
                triggerMediaScan(it.next());
            }
        }
    }
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) RemoteException(android.os.RemoteException) IOException(java.io.IOException) OperationApplicationException(android.content.OperationApplicationException) RemoteException(android.os.RemoteException) File(java.io.File)

Example 33 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 34 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 35 with ContentProviderOperation

use of android.content.ContentProviderOperation in project Etar-Calendar by Etar-Group.

the class EventInfoFragment method saveReminders.

private boolean saveReminders() {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(3);
    // Read reminders from UI
    mReminders = EventViewUtils.reminderItemsToReminders(mReminderViews, mReminderMinuteValues, mReminderMethodValues);
    mOriginalReminders.addAll(mUnsupportedReminders);
    Collections.sort(mOriginalReminders);
    mReminders.addAll(mUnsupportedReminders);
    Collections.sort(mReminders);
    // Check if there are any changes in the reminder
    boolean changed = EditEventHelper.saveReminders(ops, mEventId, mReminders, mOriginalReminders, false);
    if (!changed) {
        return false;
    }
    // save new reminders
    AsyncQueryService service = new AsyncQueryService(getActivity());
    service.startBatch(0, null, Calendars.CONTENT_URI.getAuthority(), ops, 0);
    mOriginalReminders = mReminders;
    // Update the "hasAlarm" field for the event
    Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mEventId);
    int len = mReminders.size();
    boolean hasAlarm = len > 0;
    if (hasAlarm != mHasAlarm) {
        ContentValues values = new ContentValues();
        values.put(Events.HAS_ALARM, hasAlarm ? 1 : 0);
        service.startUpdate(0, null, uri, values, null, null, 0);
    }
    return true;
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Uri(android.net.Uri)

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