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 fea0cc61e9a5..c1dce3cac56d 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 @@ -22,9 +22,6 @@ package com.nextcloud.client.files.downloader import com.nextcloud.client.account.User -import com.nextcloud.client.files.downloader.FileDownloadWorker.Companion.cancelAllDownloads -import com.nextcloud.client.files.downloader.FileDownloadWorker.Companion.folderDownloadStatusPair -import com.nextcloud.client.files.downloader.FileDownloadWorker.Companion.removePendingDownload import com.nextcloud.client.jobs.BackgroundJobManager import com.owncloud.android.MainApp import com.owncloud.android.datamodel.FileDataStorageManager @@ -64,7 +61,7 @@ class FileDownloadHelper { } return if (file.isFolder) { - isFolderDownloading(file) + FileDownloadWorker.isFolderDownloading(file) } else { backgroundJobManager.isStartFileDownloadJobScheduled( user, @@ -73,17 +70,8 @@ class FileDownloadHelper { } } - private fun isFolderDownloading(folder: OCFile): Boolean { - for ((id, status) in folderDownloadStatusPair) { - return id == folder.fileId && status - } - - return false - } - fun cancelPendingOrCurrentDownloads(user: User?, file: OCFile?) { if (user == null || file == null) return - cancelAllDownloads() backgroundJobManager.cancelFilesDownloadJob(user, file) } @@ -95,8 +83,6 @@ class FileDownloadHelper { currentDownload.cancel() } - - removePendingDownload(accountName) } fun saveFile( @@ -140,7 +126,6 @@ class FileDownloadHelper { } fun downloadFolder(folder: OCFile, user: User, files: List) { - folderDownloadStatusPair[folder.fileId] = true backgroundJobManager.startFolderDownloadJob(folder, user, files) } 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 f4dcbb4ac1fe..025f40f6be7d 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 @@ -24,7 +24,6 @@ package com.nextcloud.client.files.downloader import android.accounts.Account import android.accounts.AccountManager import android.accounts.OnAccountsUpdateListener -import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import androidx.core.util.component1 @@ -39,8 +38,6 @@ import com.nextcloud.client.account.UserAccountManager import com.nextcloud.java.util.Optional import com.nextcloud.model.WorkerState import com.nextcloud.model.WorkerStateLiveData -import com.owncloud.android.MainApp -import com.owncloud.android.R import com.owncloud.android.datamodel.FileDataStorageManager import com.owncloud.android.datamodel.OCFile import com.owncloud.android.datamodel.UploadsStorageManager @@ -71,7 +68,7 @@ class FileDownloadWorker( companion object { private val TAG = FileDownloadWorker::class.java.simpleName - var folderDownloadStatusPair = HashMap() + private var folderDownloadStatusPair = HashMap() const val FOLDER_ID = "FOLDER_ID" const val USER_NAME = "USER" @@ -89,6 +86,14 @@ class FileDownloadWorker( const val EXTRA_LINKED_TO_PATH = "LINKED_TO" const val ACCOUNT_NAME = "ACCOUNT_NAME" + fun isFolderDownloading(folder: OCFile): Boolean { + for ((id, status) in folderDownloadStatusPair) { + return id == folder.fileId && status + } + + return false + } + fun getDownloadAddedMessage(): String { return FileDownloadWorker::class.java.name + "DOWNLOAD_ADDED" } @@ -96,24 +101,6 @@ class FileDownloadWorker( fun getDownloadFinishMessage(): String { return FileDownloadWorker::class.java.name + "DOWNLOAD_FINISH" } - - private val pendingDownloads = IndexedForest() - - fun removePendingDownload(accountName: String?) { - pendingDownloads.remove(accountName) - } - - fun cancelAllDownloads() { - pendingDownloads.all.forEach { - it.value.payload?.cancel() - } - - val notificationManager = - MainApp.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - notificationManager.cancel(R.string.downloader_download_in_progress_ticker) - - pendingDownloads.all.clear() - } } private var currentDownload: DownloadFileOperation? = null @@ -127,6 +114,7 @@ class FileDownloadWorker( private var downloadClient: OwnCloudClient? = null private var user: User? = null private val gson = Gson() + private val pendingDownloads = IndexedForest() @Suppress("TooGenericExceptionCaught") override fun doWork(): Result { @@ -151,7 +139,13 @@ class FileDownloadWorker( } override fun onStopped() { + Log_OC.e(TAG, "FilesDownloadWorker stopped") + + cancelAllDownloads() + notificationManager.dismissDownloadInProgressNotification() + removePendingDownload(currentDownload?.user?.accountName) setIdleWorkerState() + super.onStopped() } @@ -200,6 +194,17 @@ class FileDownloadWorker( } } + private fun removePendingDownload(accountName: String?) { + pendingDownloads.remove(accountName) + } + + private fun cancelAllDownloads() { + pendingDownloads.all.forEach { + it.value.payload?.cancel() + } + pendingDownloads.all.clear() + } + private fun setUser() { val accountName = inputData.keyValueMap[USER_NAME] as String user = accountManager.getUser(accountName).get() @@ -231,6 +236,11 @@ class FileDownloadWorker( } private fun setWorkerState(user: User?, file: DownloadFileOperation?) { + val folderId = inputData.keyValueMap[FOLDER_ID] as Long? + folderId?.let { + folderDownloadStatusPair[folderId] = true + } + WorkerStateLiveData.instance().setWorkState(WorkerState.Download(user, file)) } 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 735270490574..caf4e4fd0bf3 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt @@ -527,12 +527,15 @@ internal class BackgroundJobManagerImpl( FileDownloadWorker.DOWNLOAD_TYPE to DownloadType.DOWNLOAD.toString() ) + val tag = startFileDownloadJobTag(user, folder) + val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user) + .addTag(tag) .setInputData(data) .build() - val tag = startFileDownloadJobTag(user, folder) - workManager.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, request) + workManager + .enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, request) } override fun startFileDownloadJob( @@ -554,11 +557,13 @@ internal class BackgroundJobManagerImpl( FileDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId ) + val tag = startFileDownloadJobTag(user, file) + val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user) + .addTag(tag) .setInputData(data) .build() - val tag = startFileDownloadJobTag(user, file) workManager.enqueueUniqueWork(tag, ExistingWorkPolicy.REPLACE, request) }