Search in sources :

Example 41 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class DocumentsStorageProvider method queryChildDocuments.

@SuppressLint("LongLogTag")
@Override
public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) throws FileNotFoundException {
    Log.d(TAG, "queryChildDocuments(), id=" + parentDocumentId);
    Context context = getNonNullContext();
    Document parentFolder = toDocument(parentDocumentId);
    FileDataStorageManager storageManager = parentFolder.getStorageManager();
    final FileCursor resultCursor = new FileCursor(projection);
    for (OCFile file : storageManager.getFolderContent(parentFolder.getFile(), false)) {
        resultCursor.addFile(new Document(storageManager, file));
    }
    boolean isLoading = false;
    if (parentFolder.isExpired()) {
        final ReloadFolderDocumentTask task = new ReloadFolderDocumentTask(parentFolder, result -> context.getContentResolver().notifyChange(toNotifyUri(parentFolder), null, false));
        task.executeOnExecutor(executor);
        resultCursor.setLoadingTask(task);
        isLoading = true;
    }
    final Bundle extra = new Bundle();
    extra.putBoolean(DocumentsContract.EXTRA_LOADING, isLoading);
    resultCursor.setExtras(extra);
    resultCursor.setNotificationUri(context.getContentResolver(), toNotifyUri(parentFolder));
    return resultCursor;
}
Also used : Context(android.content.Context) OCFile(com.owncloud.android.datamodel.OCFile) Bundle(android.os.Bundle) FileCursor(org.nextcloud.providers.cursors.FileCursor) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) SuppressLint(android.annotation.SuppressLint)

Example 42 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class FileDownloader method downloadFile.

/**
 * Core download method: requests a file to download and stores it.
 *
 * @param downloadKey Key to access the download to perform, contained in mPendingDownloads
 */
private void downloadFile(String downloadKey) {
    mStartedDownload = true;
    mCurrentDownload = mPendingDownloads.get(downloadKey);
    if (mCurrentDownload != null) {
        // Detect if the account exists
        if (accountManager.exists(mCurrentDownload.getAccount())) {
            Log_OC.d(TAG, "Account " + mCurrentDownload.getAccount().name + " exists");
            notifyDownloadStart(mCurrentDownload);
            RemoteOperationResult downloadResult = null;
            try {
                // / prepare client object to send the request to the ownCloud server
                Account currentDownloadAccount = mCurrentDownload.getAccount();
                Optional<User> currentDownloadUser = accountManager.getUser(currentDownloadAccount.name);
                if (!currentUser.equals(currentDownloadUser)) {
                    currentUser = currentDownloadUser;
                    mStorageManager = new FileDataStorageManager(currentUser.get(), getContentResolver());
                }
                // else, reuse storage manager from previous operation
                // always get client from client manager, to get fresh credentials in case
                // of update
                OwnCloudAccount ocAccount = currentDownloadUser.get().toOwnCloudAccount();
                mDownloadClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, this);
                // / perform the download
                downloadResult = mCurrentDownload.execute(mDownloadClient);
                if (downloadResult.isSuccess()) {
                    saveDownloadedFile();
                }
            } catch (Exception e) {
                Log_OC.e(TAG, "Error downloading", e);
                downloadResult = new RemoteOperationResult(e);
            } finally {
                Pair<DownloadFileOperation, String> removeResult = mPendingDownloads.removePayload(mCurrentDownload.getUser().getAccountName(), mCurrentDownload.getRemotePath());
                if (downloadResult == null) {
                    downloadResult = new RemoteOperationResult(new RuntimeException("Error downloading…"));
                }
                // / notify result
                notifyDownloadResult(mCurrentDownload, downloadResult);
                sendBroadcastDownloadFinished(mCurrentDownload, downloadResult, removeResult.second);
            }
        } else {
            cancelPendingDownloads(mCurrentDownload.getUser().getAccountName());
        }
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) DownloadFileOperation(com.owncloud.android.operations.DownloadFileOperation) User(com.nextcloud.client.account.User) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount)

Example 43 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class FileUploader method retryUploads.

/**
 * Retries a list of uploads.
 */
private void retryUploads(Intent intent, User user, List<String> requestedUploads) {
    boolean onWifiOnly;
    boolean whileChargingOnly;
    OCUpload upload = intent.getParcelableExtra(KEY_RETRY_UPLOAD);
    onWifiOnly = upload.isUseWifiOnly();
    whileChargingOnly = upload.isWhileChargingOnly();
    UploadFileOperation newUpload = new UploadFileOperation(mUploadsStorageManager, connectivityService, powerManagementService, user, null, upload, upload.getNameCollisionPolicy(), upload.getLocalAction(), this, onWifiOnly, whileChargingOnly, true, new FileDataStorageManager(user, getContentResolver()));
    newUpload.addDataTransferProgressListener(this);
    newUpload.addDataTransferProgressListener((FileUploaderBinder) mBinder);
    newUpload.addRenameUploadListener(this);
    Pair<String, String> putResult = mPendingUploads.putIfAbsent(user.getAccountName(), upload.getRemotePath(), newUpload);
    if (putResult != null) {
        String uploadKey = putResult.first;
        requestedUploads.add(uploadKey);
        // Update upload in database
        upload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
        mUploadsStorageManager.updateUpload(upload);
    }
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation)

Example 44 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class FileUploader method startNewUpload.

/**
 * Start a new {@link UploadFileOperation}.
 */
@SuppressLint("SdCardPath")
private void startNewUpload(User user, List<String> requestedUploads, boolean onWifiOnly, boolean whileChargingOnly, NameCollisionPolicy nameCollisionPolicy, int localAction, boolean isCreateRemoteFolder, int createdBy, OCFile file, boolean disableRetries) {
    if (file.getStoragePath().startsWith("/data/data/")) {
        Log_OC.d(TAG, "Upload from sensitive path is not allowed");
        return;
    }
    OCUpload ocUpload = new OCUpload(file, user);
    ocUpload.setFileSize(file.getFileLength());
    ocUpload.setNameCollisionPolicy(nameCollisionPolicy);
    ocUpload.setCreateRemoteFolder(isCreateRemoteFolder);
    ocUpload.setCreatedBy(createdBy);
    ocUpload.setLocalAction(localAction);
    ocUpload.setUseWifiOnly(onWifiOnly);
    ocUpload.setWhileChargingOnly(whileChargingOnly);
    ocUpload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
    UploadFileOperation newUpload = new UploadFileOperation(mUploadsStorageManager, connectivityService, powerManagementService, user, file, ocUpload, nameCollisionPolicy, localAction, this, onWifiOnly, whileChargingOnly, disableRetries, new FileDataStorageManager(user, getContentResolver()));
    newUpload.setCreatedBy(createdBy);
    if (isCreateRemoteFolder) {
        newUpload.setRemoteFolderToBeCreated();
    }
    newUpload.addDataTransferProgressListener(this);
    newUpload.addDataTransferProgressListener((FileUploaderBinder) mBinder);
    newUpload.addRenameUploadListener(this);
    Pair<String, String> putResult = mPendingUploads.putIfAbsent(user.getAccountName(), file.getRemotePath(), newUpload);
    if (putResult != null) {
        requestedUploads.add(putResult.first);
        // Save upload in database
        long id = mUploadsStorageManager.storeUpload(ocUpload);
        newUpload.setOCUploadId(id);
    }
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) SuppressLint(android.annotation.SuppressLint)

Example 45 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class FileUploader method uploadFile.

/**
 * Core upload method: sends the file(s) to upload
 *
 * @param uploadKey Key to access the upload to perform, contained in mPendingUploads
 */
public void uploadFile(String uploadKey) {
    mCurrentUpload = mPendingUploads.get(uploadKey);
    if (mCurrentUpload != null) {
        // / Check account existence
        if (!accountManager.exists(mCurrentUpload.getAccount())) {
            Log_OC.w(TAG, "Account " + mCurrentUpload.getAccount().name + " does not exist anymore -> cancelling all its uploads");
            cancelPendingUploads(mCurrentUpload.getUser().getAccountName());
            return;
        }
        // / OK, let's upload
        mUploadsStorageManager.updateDatabaseUploadStart(mCurrentUpload);
        notifyUploadStart(mCurrentUpload);
        sendBroadcastUploadStarted(mCurrentUpload);
        RemoteOperationResult uploadResult = null;
        try {
            // / prepare client object to send the request to the ownCloud server
            if (mCurrentAccount == null || !mCurrentAccount.equals(mCurrentUpload.getAccount())) {
                mCurrentAccount = mCurrentUpload.getAccount();
                mStorageManager = new FileDataStorageManager(getCurrentUser().get(), getContentResolver());
            }
            // else, reuse storage manager from previous operation
            // always get client from client manager, to get fresh credentials in case of update
            OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
            mUploadClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, this);
            // // If parent folder is encrypted, upload file encrypted
            // OCFile parent = mStorageManager.getFileByPath(mCurrentUpload.getFile().getParentRemotePath());
            // if (parent.isEncrypted()) {
            // UploadEncryptedFileOperation uploadEncryptedFileOperation =
            // new UploadEncryptedFileOperation(parent, mCurrentUpload);
            // 
            // uploadResult = uploadEncryptedFileOperation.execute(mUploadClient, mStorageManager);
            // } else {
            // / perform the regular upload
            uploadResult = mCurrentUpload.execute(mUploadClient);
        // }
        } catch (Exception e) {
            Log_OC.e(TAG, "Error uploading", e);
            uploadResult = new RemoteOperationResult(e);
        } finally {
            Pair<UploadFileOperation, String> removeResult;
            if (mCurrentUpload.wasRenamed()) {
                removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getOldFile().getRemotePath());
            // TODO: grant that name is also updated for mCurrentUpload.getOCUploadId
            } else {
                removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getDecryptedRemotePath());
            }
            mUploadsStorageManager.updateDatabaseUploadResult(uploadResult, mCurrentUpload);
            // / notify result
            notifyUploadResult(mCurrentUpload, uploadResult);
            sendBroadcastUploadFinished(mCurrentUpload, uploadResult, removeResult.second);
        }
        // generate new Thumbnail
        Optional<User> user = getCurrentUser();
        if (user.isPresent()) {
            final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(mStorageManager, user.get());
            File file = new File(mCurrentUpload.getOriginalStoragePath());
            String remoteId = mCurrentUpload.getFile().getRemoteId();
            task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, remoteId));
        }
    }
}
Also used : User(com.nextcloud.client.account.User) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Aggregations

FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)91 OCFile (com.owncloud.android.datamodel.OCFile)43 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)21 Account (android.accounts.Account)19 User (com.nextcloud.client.account.User)16 Intent (android.content.Intent)12 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)12 Context (android.content.Context)9 File (java.io.File)9 ArrayList (java.util.ArrayList)9 OCCapability (com.owncloud.android.lib.resources.status.OCCapability)8 Test (org.junit.Test)8 ScreenshotTest (com.owncloud.android.utils.ScreenshotTest)7 OCUpload (com.owncloud.android.db.OCUpload)6 AccountManager (android.accounts.AccountManager)5 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)5 UploadFileOperation (com.owncloud.android.operations.UploadFileOperation)5 DialogFragment (androidx.fragment.app.DialogFragment)4 IOException (java.io.IOException)4 AccountsException (android.accounts.AccountsException)3