From 1c2c56fdd427b3a2b988daa210dd2cef7de2b529 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 27 Dec 2023 15:47:48 +0100 Subject: [PATCH] Fix sync Signed-off-by: alperozturk --- .../files/downloader/FileDownloadHelper.kt | 24 +++-- .../client/jobs/BackgroundJobManager.kt | 13 +++ .../client/jobs/BackgroundJobManagerImpl.kt | 90 ++++++++++++++++--- .../SynchronizeFolderOperation.java | 16 +--- 4 files changed, 111 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt index bd8b8417d9..ecbe944a9a 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadHelper.kt @@ -31,6 +31,7 @@ import com.owncloud.android.operations.DownloadFileOperation import com.owncloud.android.operations.DownloadType import com.owncloud.android.utils.MimeTypeUtil import java.io.File +import java.util.concurrent.atomic.AtomicBoolean import javax.inject.Inject class FileDownloadHelper { @@ -79,8 +80,21 @@ class FileDownloadHelper { storageManager?.saveConflict(file, null) } - fun downloadFile(user: User, ocFile: OCFile) { + fun downloadFiles(user: User, ocFile: List, cancelRequest: AtomicBoolean) { backgroundJobManager.startFilesDownloadJob( + user, + ocFile, + "", + DownloadType.DOWNLOAD, + "", + "", + null, + cancelRequest + ) + } + + fun downloadFile(user: User, ocFile: OCFile) { + backgroundJobManager.startFileDownloadJob( user, ocFile, "", @@ -92,7 +106,7 @@ class FileDownloadHelper { } fun downloadFile(user: User, ocFile: OCFile, behaviour: String) { - backgroundJobManager.startFilesDownloadJob( + backgroundJobManager.startFileDownloadJob( user, ocFile, behaviour, @@ -104,7 +118,7 @@ class FileDownloadHelper { } fun downloadFile(user: User, ocFile: OCFile, downloadType: DownloadType) { - backgroundJobManager.startFilesDownloadJob( + backgroundJobManager.startFileDownloadJob( user, ocFile, "", @@ -116,7 +130,7 @@ class FileDownloadHelper { } fun downloadFile(user: User, ocFile: OCFile, conflictUploadId: Long) { - backgroundJobManager.startFilesDownloadJob( + backgroundJobManager.startFileDownloadJob( user, ocFile, "", @@ -137,7 +151,7 @@ class FileDownloadHelper { packageName: String, conflictUploadId: Long? ) { - backgroundJobManager.startFilesDownloadJob( + backgroundJobManager.startFileDownloadJob( user, ocFile, behaviour, diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt index 30b90d1fbe..4b2f505c8d 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt @@ -24,6 +24,7 @@ import androidx.work.ListenableWorker import com.nextcloud.client.account.User import com.owncloud.android.datamodel.OCFile import com.owncloud.android.operations.DownloadType +import java.util.concurrent.atomic.AtomicBoolean /** * This interface allows to control, schedule and monitor all application @@ -147,6 +148,18 @@ interface BackgroundJobManager { @Suppress("LongParameterList") fun startFilesDownloadJob( + user: User, + files: List, + behaviour: String, + downloadType: DownloadType?, + activityName: String, + packageName: String, + conflictUploadId: Long?, + cancelRequest: AtomicBoolean + ) + + @Suppress("LongParameterList") + fun startFileDownloadJob( user: User, ocFile: OCFile, behaviour: String, diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt index 8e815432c9..98d67a1c04 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt @@ -42,10 +42,12 @@ import com.nextcloud.client.documentscan.GeneratePdfFromImagesWork import com.nextcloud.client.files.downloader.FileDownloadWorker import com.nextcloud.client.preferences.AppPreferences import com.owncloud.android.datamodel.OCFile +import com.owncloud.android.lib.common.operations.OperationCancelledException import com.owncloud.android.operations.DownloadType import java.util.Date import java.util.UUID import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean import kotlin.reflect.KClass /** @@ -509,7 +511,73 @@ internal class BackgroundJobManagerImpl( workManager.enqueueUniqueWork(JOB_FILES_UPLOAD + user.accountName, ExistingWorkPolicy.KEEP, request) } + private fun getOneTimeDownloadRequest( + user: User, + file: OCFile, + behaviour: String, + downloadType: DownloadType?, + activityName: String, + packageName: String, + conflictUploadId: Long? + ): OneTimeWorkRequest { + val gson = Gson() + + val data = workDataOf( + FileDownloadWorker.USER_NAME to user.accountName, + FileDownloadWorker.FILE to gson.toJson(file), + FileDownloadWorker.BEHAVIOUR to behaviour, + FileDownloadWorker.DOWNLOAD_TYPE to downloadType.toString(), + FileDownloadWorker.ACTIVITY_NAME to activityName, + FileDownloadWorker.PACKAGE_NAME to packageName, + FileDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId + ) + + return oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user) + .setInputData(data) + .build() + } + + @Throws(OperationCancelledException::class) override fun startFilesDownloadJob( + user: User, + files: List, + behaviour: String, + downloadType: DownloadType?, + activityName: String, + packageName: String, + conflictUploadId: Long?, + cancelRequest: AtomicBoolean + ) { + val workRequestList = mutableListOf() + + for (file in files) { + synchronized(cancelRequest) { + if (cancelRequest.get()) { + throw OperationCancelledException() + } + + workRequestList.add( + getOneTimeDownloadRequest( + user, + file, + behaviour, + downloadType, + activityName, + packageName, + conflictUploadId + ) + ) + } + } + + val chain = workManager + .beginWith(workRequestList.first()) + .then(workRequestList.subList(1, workRequestList.size)) + + chain.enqueue() + } + + override fun startFileDownloadJob( user: User, ocFile: OCFile, behaviour: String, @@ -518,22 +586,16 @@ internal class BackgroundJobManagerImpl( packageName: String, conflictUploadId: Long? ) { - val gson = Gson() - - val data = workDataOf( - FileDownloadWorker.USER_NAME to user.accountName, - FileDownloadWorker.FILE to gson.toJson(ocFile), - FileDownloadWorker.BEHAVIOUR to behaviour, - FileDownloadWorker.DOWNLOAD_TYPE to downloadType.toString(), - FileDownloadWorker.ACTIVITY_NAME to activityName, - FileDownloadWorker.PACKAGE_NAME to packageName, - FileDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId + val request = getOneTimeDownloadRequest( + user, + ocFile, + behaviour, + downloadType, + activityName, + packageName, + conflictUploadId ) - val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user) - .setInputData(data) - .build() - workManager.enqueueUniqueWork(JOB_FILES_DOWNLOAD + user.accountName, ExistingWorkPolicy.REPLACE, request) } diff --git a/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java b/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java index cf60dacea9..e69a96821b 100644 --- a/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -444,19 +444,9 @@ public class SynchronizeFolderOperation extends SyncOperation { startContentSynchronizations(mFilesToSyncContents); } - - private void startDirectDownloads() throws OperationCancelledException { - for (OCFile file : mFilesForDirectDownload) { - synchronized(mCancellationRequested) { - if (mCancellationRequested.get()) { - throw new OperationCancelledException(); - } - - FileDownloadHelper downloadHelper = new FileDownloadHelper(); - - downloadHelper.downloadFile(user, file); - } - } + private void startDirectDownloads() { + FileDownloadHelper downloadHelper = new FileDownloadHelper(); + downloadHelper.downloadFiles(user, mFilesForDirectDownload, mCancellationRequested); } /**