mirror of
https://github.com/nextcloud/android.git
synced 2024-11-27 09:39:25 +03:00
Fix sync
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
f14abd4b8e
commit
1c2c56fdd4
4 changed files with 111 additions and 32 deletions
|
@ -31,6 +31,7 @@ import com.owncloud.android.operations.DownloadFileOperation
|
||||||
import com.owncloud.android.operations.DownloadType
|
import com.owncloud.android.operations.DownloadType
|
||||||
import com.owncloud.android.utils.MimeTypeUtil
|
import com.owncloud.android.utils.MimeTypeUtil
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class FileDownloadHelper {
|
class FileDownloadHelper {
|
||||||
|
@ -79,8 +80,21 @@ class FileDownloadHelper {
|
||||||
storageManager?.saveConflict(file, null)
|
storageManager?.saveConflict(file, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun downloadFile(user: User, ocFile: OCFile) {
|
fun downloadFiles(user: User, ocFile: List<OCFile>, cancelRequest: AtomicBoolean) {
|
||||||
backgroundJobManager.startFilesDownloadJob(
|
backgroundJobManager.startFilesDownloadJob(
|
||||||
|
user,
|
||||||
|
ocFile,
|
||||||
|
"",
|
||||||
|
DownloadType.DOWNLOAD,
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
null,
|
||||||
|
cancelRequest
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun downloadFile(user: User, ocFile: OCFile) {
|
||||||
|
backgroundJobManager.startFileDownloadJob(
|
||||||
user,
|
user,
|
||||||
ocFile,
|
ocFile,
|
||||||
"",
|
"",
|
||||||
|
@ -92,7 +106,7 @@ class FileDownloadHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun downloadFile(user: User, ocFile: OCFile, behaviour: String) {
|
fun downloadFile(user: User, ocFile: OCFile, behaviour: String) {
|
||||||
backgroundJobManager.startFilesDownloadJob(
|
backgroundJobManager.startFileDownloadJob(
|
||||||
user,
|
user,
|
||||||
ocFile,
|
ocFile,
|
||||||
behaviour,
|
behaviour,
|
||||||
|
@ -104,7 +118,7 @@ class FileDownloadHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun downloadFile(user: User, ocFile: OCFile, downloadType: DownloadType) {
|
fun downloadFile(user: User, ocFile: OCFile, downloadType: DownloadType) {
|
||||||
backgroundJobManager.startFilesDownloadJob(
|
backgroundJobManager.startFileDownloadJob(
|
||||||
user,
|
user,
|
||||||
ocFile,
|
ocFile,
|
||||||
"",
|
"",
|
||||||
|
@ -116,7 +130,7 @@ class FileDownloadHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun downloadFile(user: User, ocFile: OCFile, conflictUploadId: Long) {
|
fun downloadFile(user: User, ocFile: OCFile, conflictUploadId: Long) {
|
||||||
backgroundJobManager.startFilesDownloadJob(
|
backgroundJobManager.startFileDownloadJob(
|
||||||
user,
|
user,
|
||||||
ocFile,
|
ocFile,
|
||||||
"",
|
"",
|
||||||
|
@ -137,7 +151,7 @@ class FileDownloadHelper {
|
||||||
packageName: String,
|
packageName: String,
|
||||||
conflictUploadId: Long?
|
conflictUploadId: Long?
|
||||||
) {
|
) {
|
||||||
backgroundJobManager.startFilesDownloadJob(
|
backgroundJobManager.startFileDownloadJob(
|
||||||
user,
|
user,
|
||||||
ocFile,
|
ocFile,
|
||||||
behaviour,
|
behaviour,
|
||||||
|
|
|
@ -24,6 +24,7 @@ import androidx.work.ListenableWorker
|
||||||
import com.nextcloud.client.account.User
|
import com.nextcloud.client.account.User
|
||||||
import com.owncloud.android.datamodel.OCFile
|
import com.owncloud.android.datamodel.OCFile
|
||||||
import com.owncloud.android.operations.DownloadType
|
import com.owncloud.android.operations.DownloadType
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This interface allows to control, schedule and monitor all application
|
* This interface allows to control, schedule and monitor all application
|
||||||
|
@ -147,6 +148,18 @@ interface BackgroundJobManager {
|
||||||
|
|
||||||
@Suppress("LongParameterList")
|
@Suppress("LongParameterList")
|
||||||
fun startFilesDownloadJob(
|
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,
|
user: User,
|
||||||
ocFile: OCFile,
|
ocFile: OCFile,
|
||||||
behaviour: String,
|
behaviour: String,
|
||||||
|
|
|
@ -42,10 +42,12 @@ import com.nextcloud.client.documentscan.GeneratePdfFromImagesWork
|
||||||
import com.nextcloud.client.files.downloader.FileDownloadWorker
|
import com.nextcloud.client.files.downloader.FileDownloadWorker
|
||||||
import com.nextcloud.client.preferences.AppPreferences
|
import com.nextcloud.client.preferences.AppPreferences
|
||||||
import com.owncloud.android.datamodel.OCFile
|
import com.owncloud.android.datamodel.OCFile
|
||||||
|
import com.owncloud.android.lib.common.operations.OperationCancelledException
|
||||||
import com.owncloud.android.operations.DownloadType
|
import com.owncloud.android.operations.DownloadType
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -509,7 +511,73 @@ internal class BackgroundJobManagerImpl(
|
||||||
workManager.enqueueUniqueWork(JOB_FILES_UPLOAD + user.accountName, ExistingWorkPolicy.KEEP, request)
|
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(
|
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,
|
user: User,
|
||||||
ocFile: OCFile,
|
ocFile: OCFile,
|
||||||
behaviour: String,
|
behaviour: String,
|
||||||
|
@ -518,22 +586,16 @@ internal class BackgroundJobManagerImpl(
|
||||||
packageName: String,
|
packageName: String,
|
||||||
conflictUploadId: Long?
|
conflictUploadId: Long?
|
||||||
) {
|
) {
|
||||||
val gson = Gson()
|
val request = getOneTimeDownloadRequest(
|
||||||
|
user,
|
||||||
val data = workDataOf(
|
ocFile,
|
||||||
FileDownloadWorker.USER_NAME to user.accountName,
|
behaviour,
|
||||||
FileDownloadWorker.FILE to gson.toJson(ocFile),
|
downloadType,
|
||||||
FileDownloadWorker.BEHAVIOUR to behaviour,
|
activityName,
|
||||||
FileDownloadWorker.DOWNLOAD_TYPE to downloadType.toString(),
|
packageName,
|
||||||
FileDownloadWorker.ACTIVITY_NAME to activityName,
|
conflictUploadId
|
||||||
FileDownloadWorker.PACKAGE_NAME to packageName,
|
|
||||||
FileDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId
|
|
||||||
)
|
)
|
||||||
|
|
||||||
val request = oneTimeRequestBuilder(FileDownloadWorker::class, JOB_FILES_DOWNLOAD, user)
|
|
||||||
.setInputData(data)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
workManager.enqueueUniqueWork(JOB_FILES_DOWNLOAD + user.accountName, ExistingWorkPolicy.REPLACE, request)
|
workManager.enqueueUniqueWork(JOB_FILES_DOWNLOAD + user.accountName, ExistingWorkPolicy.REPLACE, request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -444,19 +444,9 @@ public class SynchronizeFolderOperation extends SyncOperation {
|
||||||
startContentSynchronizations(mFilesToSyncContents);
|
startContentSynchronizations(mFilesToSyncContents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void startDirectDownloads() {
|
||||||
private void startDirectDownloads() throws OperationCancelledException {
|
FileDownloadHelper downloadHelper = new FileDownloadHelper();
|
||||||
for (OCFile file : mFilesForDirectDownload) {
|
downloadHelper.downloadFiles(user, mFilesForDirectDownload, mCancellationRequested);
|
||||||
synchronized(mCancellationRequested) {
|
|
||||||
if (mCancellationRequested.get()) {
|
|
||||||
throw new OperationCancelledException();
|
|
||||||
}
|
|
||||||
|
|
||||||
FileDownloadHelper downloadHelper = new FileDownloadHelper();
|
|
||||||
|
|
||||||
downloadHelper.downloadFile(user, file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue