Fix cancelAllDownloads

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-01-03 14:06:18 +01:00 committed by Alper Öztürk
parent a180dcd5b3
commit 157d1864b3
5 changed files with 29 additions and 26 deletions

View file

@ -22,7 +22,9 @@
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
@ -81,6 +83,7 @@ class FileDownloadHelper {
fun cancelPendingOrCurrentDownloads(user: User?, file: OCFile?) {
if (user == null || file == null) return
cancelAllDownloads()
backgroundJobManager.cancelFilesDownloadJob(user, file)
}
@ -92,6 +95,8 @@ class FileDownloadHelper {
currentDownload.cancel()
}
removePendingDownload(accountName)
}
fun saveFile(

View file

@ -24,6 +24,7 @@ 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
@ -38,6 +39,8 @@ 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
@ -57,7 +60,7 @@ import java.util.Vector
@Suppress("LongParameterList")
class FileDownloadWorker(
viewThemeUtils: ViewThemeUtils,
private val viewThemeUtils: ViewThemeUtils,
private val accountManager: UserAccountManager,
private val uploadsStorageManager: UploadsStorageManager,
private var localBroadcastManager: LocalBroadcastManager,
@ -93,6 +96,24 @@ class FileDownloadWorker(
fun getDownloadFinishMessage(): String {
return FileDownloadWorker::class.java.name + "DOWNLOAD_FINISH"
}
private val pendingDownloads = IndexedForest<DownloadFileOperation>()
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
@ -100,7 +121,6 @@ class FileDownloadWorker(
private var lastPercent = 0
private val intents = FileDownloadIntents(context)
private val notificationManager = DownloadNotificationManager(context, viewThemeUtils)
private val pendingDownloads = IndexedForest<DownloadFileOperation>()
private var downloadProgressListener = FileDownloadProgressListener()
private var currentUser = Optional.empty<User>()
private var storageManager: FileDataStorageManager? = null
@ -266,10 +286,6 @@ class FileDownloadWorker(
}
}
private fun removePendingDownload(accountName: String?) {
pendingDownloads.remove(accountName)
}
private fun notifyDownloadStart(download: DownloadFileOperation) {
lastPercent = 0

View file

@ -24,7 +24,6 @@ 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

View file

@ -524,7 +524,7 @@ internal class BackgroundJobManagerImpl(
FileDownloadWorker.USER_NAME to user.accountName,
FileDownloadWorker.FOLDER_ID to folder.fileId,
FileDownloadWorker.FILES to gson.toJson(files),
FileDownloadWorker.DOWNLOAD_TYPE to DownloadType.DOWNLOAD.toString(),
FileDownloadWorker.DOWNLOAD_TYPE to DownloadType.DOWNLOAD.toString()
)
val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user)
@ -551,7 +551,7 @@ internal class BackgroundJobManagerImpl(
FileDownloadWorker.DOWNLOAD_TYPE to downloadType.toString(),
FileDownloadWorker.ACTIVITY_NAME to activityName,
FileDownloadWorker.PACKAGE_NAME to packageName,
FileDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId,
FileDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId
)
val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user)

View file

@ -448,23 +448,6 @@ public class SynchronizeFolderOperation extends SyncOperation {
FileDownloadHelper.Companion.instance().downloadFolder(mLocalFolder,
user,
mFilesForDirectDownload);
// FIXME cancel request
/*
folderDownloadStatusPair.put(mLocalFolder.getFileId(), true);
for (OCFile file : mFilesForDirectDownload) {
synchronized(mCancellationRequested) {
if (mCancellationRequested.get()) {
throw new OperationCancelledException();
}
FileDownloadHelper.Companion.instance().downloadFile(user, file);
}
}
folderDownloadStatusPair.replace(mLocalFolder.getFileId(), false);
*/
}
/**