diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt index 178761ef8475..3efb81a35d7b 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt @@ -66,6 +66,7 @@ class FileDownloadWorker( private val TAG = FileDownloadWorker::class.java.simpleName private var currentDownload: DownloadFileOperation? = null + private val lock = Any() const val FILES_SEPARATOR = "," const val FOLDER_REMOTE_PATH = "FOLDER_REMOTE_PATH" @@ -84,12 +85,16 @@ class FileDownloadWorker( const val EXTRA_ACCOUNT_NAME = "EXTRA_ACCOUNT_NAME" fun isDownloading(user: User, file: OCFile): Boolean { - return currentDownload?.isActive(user, file) ?: false + synchronized(lock) { + return currentDownload?.isActive(user, file) ?: false + } } fun cancelCurrentDownload(user: User, file: OCFile) { - if (currentDownload?.isActive(user, file) == true) { - currentDownload?.cancel() + synchronized(lock) { + if (currentDownload?.isActive(user, file) == true) { + currentDownload?.cancel() + } } } diff --git a/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java b/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java index 1aaca764934c..e0e28af5a40d 100644 --- a/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java @@ -170,6 +170,11 @@ protected RemoteOperationResult run(OwnCloudClient client) { } } + Context operationContext = context.get(); + if (operationContext == null) { + return new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR); + } + RemoteOperationResult result; File newFile = null; boolean moved; @@ -190,6 +195,8 @@ protected RemoteOperationResult run(OwnCloudClient client) { result = downloadOperation.execute(client); + + if (result.isSuccess()) { modificationTimestamp = downloadOperation.getModificationTimestamp(); etag = downloadOperation.getEtag(); @@ -204,13 +211,13 @@ protected RemoteOperationResult run(OwnCloudClient client) { // decrypt file if (file.isEncrypted()) { - FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, context.get().getContentResolver()); + FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, operationContext.getContentResolver()); - OCFile parent = fileDataStorageManager.getFileByPath(file.getParentRemotePath()); + OCFile parent = fileDataStorageManager.getFileByEncryptedRemotePath(file.getParentRemotePath()); DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, - context.get(), + operationContext, user); if (metadata == null) { @@ -228,7 +235,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { key, iv, authenticationTag, - new ArbitraryDataProviderImpl(context.get()), + new ArbitraryDataProviderImpl(operationContext), user); try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) { @@ -248,7 +255,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { } else if (downloadType == DownloadType.EXPORT) { new FileExportUtils().exportFile(file.getFileName(), file.getMimeType(), - context.get().getContentResolver(), + operationContext.getContentResolver(), null, tmpFile); if (!tmpFile.delete()) { @@ -256,6 +263,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { } } } + Log_OC.i(TAG, "Download of " + file.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());