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 fce3cf22a4..6c967e1441 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt @@ -146,8 +146,6 @@ interface BackgroundJobManager { fun cancelFilesDownloadJob(user: User, fileId: Long) - fun isStartFileDownloadJobScheduled(user: User, fileId: Long): Boolean - @Suppress("LongParameterList") fun startFileDownloadJob( user: User, 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 0399b4f0fc..be7d333b61 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt @@ -597,10 +597,6 @@ internal class BackgroundJobManagerImpl( return JOB_FOLDER_DOWNLOAD + user.accountName + fileId } - override fun isStartFileDownloadJobScheduled(user: User, fileId: Long): Boolean { - return workManager.isWorkScheduled(startFileDownloadJobTag(user, fileId)) - } - override fun startFileDownloadJob( user: User, file: OCFile, diff --git a/app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadHelper.kt b/app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadHelper.kt index e677b278b8..58bc099bc6 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadHelper.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/download/FileDownloadHelper.kt @@ -9,6 +9,7 @@ package com.nextcloud.client.jobs.download import com.nextcloud.client.account.User import com.nextcloud.client.jobs.BackgroundJobManager +import com.nextcloud.client.jobs.sync.SyncWorker import com.owncloud.android.MainApp import com.owncloud.android.datamodel.FileDataStorageManager import com.owncloud.android.datamodel.OCFile @@ -46,12 +47,8 @@ class FileDownloadHelper { return false } - val fileStorageManager = FileDataStorageManager(user, MainApp.getAppContext().contentResolver) - val topParentId = fileStorageManager.getTopParentId(file) - - val isJobScheduled = backgroundJobManager.isStartFileDownloadJobScheduled(user, file.fileId) - return isJobScheduled || if (file.isFolder) { - backgroundJobManager.isStartFileDownloadJobScheduled(user, topParentId) + return if (file.isFolder) { + SyncWorker.isDownloading(file.fileId) } else { FileDownloadWorker.isDownloading(user.accountName, file.fileId) } diff --git a/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorker.kt b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorker.kt index 8a3a9a2559..d627570e0f 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorker.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorker.kt @@ -12,6 +12,7 @@ import androidx.work.CoroutineWorker import androidx.work.WorkerParameters import com.nextcloud.client.account.User import com.owncloud.android.datamodel.FileDataStorageManager +import com.owncloud.android.datamodel.OCFile import com.owncloud.android.lib.common.OwnCloudClientManagerFactory import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.operations.DownloadFileOperation @@ -26,8 +27,17 @@ class SyncWorker( ) : CoroutineWorker(context, params) { companion object { - const val FILE_PATHS = "FILE_PATHS" private const val TAG = "SyncWorker" + + const val FILE_PATHS = "FILE_PATHS" + + // FIXME it's not synchronous + fun isDownloading(fileId: Long): Boolean { + return downloadFileIds.contains(fileId) || currentDownloadFolderId == fileId + } + + private var currentDownloadFolderId: Long? = null + private var downloadFileIds = ArrayList() } private val notificationManager = SyncWorkerNotificationManager(context) @@ -59,15 +69,15 @@ class SyncWorker( } fileDataStorageManager.getFileByDecryptedRemotePath(path)?.let { file -> - withContext(Dispatchers.Main) { notificationManager.showProgressNotification(file.fileName, index, filePaths.size) } delay(1000) - // TODO dont download downloaded files?? val operation = DownloadFileOperation(user, file, context).execute(client) + setCurrentDownloadFileIds(fileDataStorageManager, file) + Log_OC.d(TAG, "Syncing file: " + file.decryptedRemotePath) if (!operation.isSuccess) { result = false @@ -78,25 +88,26 @@ class SyncWorker( // TODO add isDownloading // TODO add cancel only one file download + downloadFileIds.clear() + withContext(Dispatchers.Main) { + notificationManager.showCompletionMessage(result) + } + if (result) { Log_OC.d(TAG, "SyncWorker completed") - - withContext(Dispatchers.Main) { - notificationManager.showSuccessNotification() - } - Result.success() } else { Log_OC.d(TAG, "SyncWorker failed") - - withContext(Dispatchers.Main) { - notificationManager.showErrorNotification() - delay(1000) - notificationManager.dismiss() - } - Result.failure() } } } + + private fun setCurrentDownloadFileIds(fileDataStorageManager: FileDataStorageManager, file: OCFile) { + if (currentDownloadFolderId == null) { + currentDownloadFolderId = fileDataStorageManager.getTopParentId(file) + } + + downloadFileIds.add(file.fileId) + } } diff --git a/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerNotificationManager.kt b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerNotificationManager.kt index c21e23a168..4ed7025922 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerNotificationManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/sync/SyncWorkerNotificationManager.kt @@ -15,6 +15,7 @@ import android.content.Intent import android.os.Build import androidx.core.app.NotificationCompat import com.owncloud.android.ui.notifications.NotificationUtils +import kotlinx.coroutines.delay class SyncWorkerNotificationManager(private val context: Context) { @@ -75,12 +76,23 @@ class SyncWorkerNotificationManager(private val context: Context) { notificationManager.notify(notificationId, notification) } - fun showSuccessNotification() { + suspend fun showCompletionMessage(success: Boolean) { + if (success) { + showSuccessNotification() + } else { + showErrorNotification() + } + + delay(1000) + dismiss() + } + + private fun showSuccessNotification() { val notification = getNotification("Download Complete", "File downloaded successfully") notificationManager.notify(notificationId, notification) } - fun showErrorNotification() { + private fun showErrorNotification() { val notification = getNotification("Download Failed", "Error downloading file") notificationManager.notify(notificationId, notification) }