Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2023-12-27 15:47:48 +01:00 committed by Alper Öztürk
parent f14abd4b8e
commit 1c2c56fdd4
4 changed files with 111 additions and 32 deletions

View file

@ -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<OCFile>, 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,

View file

@ -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<OCFile>,
behaviour: String,
downloadType: DownloadType?,
activityName: String,
packageName: String,
conflictUploadId: Long?,
cancelRequest: AtomicBoolean
)
@Suppress("LongParameterList")
fun startFileDownloadJob(
user: User,
ocFile: OCFile,
behaviour: String,

View file

@ -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<OCFile>,
behaviour: String,
downloadType: DownloadType?,
activityName: String,
packageName: String,
conflictUploadId: Long?,
cancelRequest: AtomicBoolean
) {
val workRequestList = mutableListOf<OneTimeWorkRequest>()
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)
}

View file

@ -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);
}
/**