Skip to content

Commit

Permalink
Fix cancelAllDownloads
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jan 3, 2024
1 parent 7d69a62 commit d1a44bf
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,6 +83,7 @@ class FileDownloadHelper {

fun cancelPendingOrCurrentDownloads(user: User?, file: OCFile?) {
if (user == null || file == null) return
cancelAllDownloads()
backgroundJobManager.cancelFilesDownloadJob(user, file)
}

Expand All @@ -92,6 +95,8 @@ class FileDownloadHelper {

currentDownload.cancel()
}

removePendingDownload(accountName)
}

fun saveFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -93,14 +96,31 @@ 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
private var conflictUploadId: Long? = null
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
Expand Down Expand Up @@ -266,10 +286,6 @@ class FileDownloadWorker(
}
}

private fun removePendingDownload(accountName: String?) {
pendingDownloads.remove(accountName)
}

private fun notifyDownloadStart(download: DownloadFileOperation) {
lastPercent = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public class IndexedForest<V> {

private ConcurrentMap<String, Node<V>> mMap = new ConcurrentHashMap<>();

public ConcurrentMap<String, Node<V>> getAll() {
return mMap;
}

@SuppressWarnings("PMD.ShortClassName")
private class Node<V> {
public class Node<V> {
private String mKey;
private Node<V> mParent;
private Set<Node<V>> mChildren = new HashSet<>(); // TODO be careful with hash()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,23 +448,6 @@ private void startDirectDownloads() {
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);
*/

}

/**
Expand Down

0 comments on commit d1a44bf

Please sign in to comment.