mirror of
https://github.com/nextcloud/android.git
synced 2024-12-21 08:24:08 +03:00
simplify...
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
7439bc387b
commit
57990c8453
4 changed files with 103 additions and 55 deletions
|
@ -173,4 +173,5 @@ interface BackgroundJobManager {
|
||||||
fun scheduleInternal2WaySync(intervalMinutes: Long)
|
fun scheduleInternal2WaySync(intervalMinutes: Long)
|
||||||
fun cancelAllFilesDownloadJobs()
|
fun cancelAllFilesDownloadJobs()
|
||||||
fun syncFolder(files: List<OCFile>)
|
fun syncFolder(files: List<OCFile>)
|
||||||
|
fun cancelSyncFolder()
|
||||||
}
|
}
|
||||||
|
|
|
@ -715,6 +715,7 @@ internal class BackgroundJobManagerImpl(
|
||||||
workManager.enqueueUniquePeriodicWork(JOB_INTERNAL_TWO_WAY_SYNC, ExistingPeriodicWorkPolicy.UPDATE, request)
|
workManager.enqueueUniquePeriodicWork(JOB_INTERNAL_TWO_WAY_SYNC, ExistingPeriodicWorkPolicy.UPDATE, request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Add tag for chosen folder
|
||||||
override fun syncFolder(files: List<OCFile>) {
|
override fun syncFolder(files: List<OCFile>) {
|
||||||
val filePaths = files.map { it.decryptedRemotePath }
|
val filePaths = files.map { it.decryptedRemotePath }
|
||||||
|
|
||||||
|
@ -729,4 +730,9 @@ internal class BackgroundJobManagerImpl(
|
||||||
|
|
||||||
workManager.enqueueUniqueWork(JOB_SYNC_FOLDER, ExistingWorkPolicy.REPLACE, request)
|
workManager.enqueueUniqueWork(JOB_SYNC_FOLDER, ExistingWorkPolicy.REPLACE, request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Add cancellation for chosen folder
|
||||||
|
override fun cancelSyncFolder() {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,13 +14,15 @@ import androidx.work.CoroutineWorker
|
||||||
import androidx.work.WorkerParameters
|
import androidx.work.WorkerParameters
|
||||||
import com.nextcloud.client.account.User
|
import com.nextcloud.client.account.User
|
||||||
import com.owncloud.android.datamodel.FileDataStorageManager
|
import com.owncloud.android.datamodel.FileDataStorageManager
|
||||||
|
import com.owncloud.android.datamodel.OCFile
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient
|
||||||
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
|
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
|
||||||
import com.owncloud.android.lib.common.utils.Log_OC
|
import com.owncloud.android.lib.common.utils.Log_OC
|
||||||
import com.owncloud.android.operations.DownloadFileOperation
|
import com.owncloud.android.operations.DownloadFileOperation
|
||||||
import com.owncloud.android.ui.helpers.FileOperationsHelper
|
import com.owncloud.android.ui.helpers.FileOperationsHelper
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.util.Collections
|
||||||
|
|
||||||
class SyncWorker(
|
class SyncWorker(
|
||||||
private val user: User,
|
private val user: User,
|
||||||
|
@ -37,7 +39,7 @@ class SyncWorker(
|
||||||
const val FILE_DOWNLOAD_COMPLETION_BROADCAST = "FILE_DOWNLOAD_COMPLETION_BROADCAST"
|
const val FILE_DOWNLOAD_COMPLETION_BROADCAST = "FILE_DOWNLOAD_COMPLETION_BROADCAST"
|
||||||
const val FILE_PATH = "FILE_PATH"
|
const val FILE_PATH = "FILE_PATH"
|
||||||
|
|
||||||
private var downloadingFilePaths = ArrayList<String>()
|
private var downloadingFilePaths = mutableListOf<String>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It is used to add the sync icon next to the file in the folder.
|
* It is used to add the sync icon next to the file in the folder.
|
||||||
|
@ -49,45 +51,37 @@ class SyncWorker(
|
||||||
|
|
||||||
private val notificationManager = SyncWorkerNotificationManager(context)
|
private val notificationManager = SyncWorkerNotificationManager(context)
|
||||||
|
|
||||||
@Suppress("DEPRECATION", "MagicNumber")
|
@Suppress("TooGenericExceptionCaught")
|
||||||
override suspend fun doWork(): Result {
|
override suspend fun doWork(): Result {
|
||||||
|
Log_OC.d(TAG, "SyncWorker started")
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
notificationManager.showStartNotification()
|
notificationManager.showStartNotification()
|
||||||
}
|
}
|
||||||
|
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
Log_OC.d(TAG, "SyncWorker started")
|
try {
|
||||||
val filePaths = inputData.getStringArray(FILE_PATHS)
|
val filePaths = inputData.getStringArray(FILE_PATHS)
|
||||||
|
if (filePaths.isNullOrEmpty()) {
|
||||||
if (filePaths.isNullOrEmpty()) {
|
return@withContext Result.success()
|
||||||
return@withContext Result.success()
|
|
||||||
}
|
|
||||||
|
|
||||||
val fileDataStorageManager = FileDataStorageManager(user, context.contentResolver)
|
|
||||||
|
|
||||||
// Add the topParentPath to mark the sync icon on the selected folder.
|
|
||||||
val topParentPath = getTopParentPath(fileDataStorageManager, filePaths.first())
|
|
||||||
downloadingFilePaths = ArrayList(filePaths.toList()).apply {
|
|
||||||
add(topParentPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
val account = user.toOwnCloudAccount()
|
|
||||||
val client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(account, context)
|
|
||||||
|
|
||||||
var result = true
|
|
||||||
filePaths.forEachIndexed { index, path ->
|
|
||||||
if (isStopped) {
|
|
||||||
notificationManager.dismiss()
|
|
||||||
return@withContext Result.failure()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fileDataStorageManager.getFileByDecryptedRemotePath(path)?.let { file ->
|
val storageManager = FileDataStorageManager(user, context.contentResolver)
|
||||||
val fileSizeInByte = file.fileLength
|
|
||||||
val availableDiskSpace = FileOperationsHelper.getAvailableSpaceOnDevice()
|
|
||||||
|
|
||||||
// TODO check
|
val topParentPath = prepareDownloadingFilePathsAndGetTopParentPath(storageManager, filePaths)
|
||||||
if (availableDiskSpace < fileSizeInByte) {
|
|
||||||
notificationManager.showNotAvailableDiskSpace()
|
val client = getClient()
|
||||||
|
|
||||||
|
var result = true
|
||||||
|
filePaths.forEachIndexed { index, path ->
|
||||||
|
if (isStopped) {
|
||||||
|
notificationManager.dismiss()
|
||||||
|
return@withContext Result.failure()
|
||||||
|
}
|
||||||
|
|
||||||
|
val file = storageManager.getFileByDecryptedRemotePath(path) ?: return@forEachIndexed
|
||||||
|
|
||||||
|
if (!checkDiskSize(file)) {
|
||||||
return@withContext Result.failure()
|
return@withContext Result.failure()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,39 +89,82 @@ class SyncWorker(
|
||||||
notificationManager.showProgressNotification(file.fileName, index, filePaths.size)
|
notificationManager.showProgressNotification(file.fileName, index, filePaths.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(1000)
|
val syncFileResult = syncFile(file, path, client)
|
||||||
|
if (!syncFileResult) {
|
||||||
val operation = DownloadFileOperation(user, file, context).execute(client)
|
|
||||||
Log_OC.d(TAG, "Syncing file: " + file.decryptedRemotePath)
|
|
||||||
|
|
||||||
if (operation.isSuccess) {
|
|
||||||
sendFileDownloadCompletionBroadcast(path)
|
|
||||||
downloadingFilePaths.remove(path)
|
|
||||||
} else {
|
|
||||||
result = false
|
result = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
notificationManager.showCompletionMessage(result)
|
notificationManager.showCompletionMessage(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
downloadingFilePaths.remove(topParentPath)
|
downloadingFilePaths.remove(topParentPath)
|
||||||
sendSyncWorkerCompletionBroadcast()
|
sendSyncWorkerCompletionBroadcast()
|
||||||
Log_OC.d(TAG, "SyncWorker completed")
|
Log_OC.d(TAG, "SyncWorker completed")
|
||||||
Result.success()
|
Result.success()
|
||||||
} else {
|
} else {
|
||||||
Log_OC.d(TAG, "SyncWorker failed")
|
Log_OC.d(TAG, "SyncWorker failed")
|
||||||
|
Result.failure()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log_OC.d(TAG, "SyncWorker failed reason: $e")
|
||||||
Result.failure()
|
Result.failure()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTopParentPath(fileDataStorageManager: FileDataStorageManager, firstFilePath: String): String {
|
@Suppress("DEPRECATION")
|
||||||
val firstFile = fileDataStorageManager.getFileByDecryptedRemotePath(firstFilePath)
|
private fun getClient(): OwnCloudClient {
|
||||||
val topParentFile = fileDataStorageManager.getTopParent(firstFile)
|
val account = user.toOwnCloudAccount()
|
||||||
|
return OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(account, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the topParentPath to mark the sync icon on the selected folder.
|
||||||
|
*/
|
||||||
|
private fun prepareDownloadingFilePathsAndGetTopParentPath(
|
||||||
|
storageManager: FileDataStorageManager,
|
||||||
|
filePaths: Array<String>
|
||||||
|
): String {
|
||||||
|
val topParentPath = getTopParentPath(storageManager, filePaths.first())
|
||||||
|
downloadingFilePaths = Collections.synchronizedList(ArrayList(filePaths.toList())).apply {
|
||||||
|
add(topParentPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return topParentPath
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun checkDiskSize(file: OCFile): Boolean {
|
||||||
|
val fileSizeInByte = file.fileLength
|
||||||
|
val availableDiskSpace = FileOperationsHelper.getAvailableSpaceOnDevice()
|
||||||
|
|
||||||
|
return if (availableDiskSpace < fileSizeInByte) {
|
||||||
|
notificationManager.showNotAvailableDiskSpace()
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
private fun syncFile(file: OCFile, path: String, client: OwnCloudClient): Boolean {
|
||||||
|
val operation = DownloadFileOperation(user, file, context).execute(client)
|
||||||
|
Log_OC.d(TAG, "Syncing file: " + file.decryptedRemotePath)
|
||||||
|
|
||||||
|
return if (operation.isSuccess) {
|
||||||
|
sendFileDownloadCompletionBroadcast(path)
|
||||||
|
downloadingFilePaths.remove(path)
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTopParentPath(storageManager: FileDataStorageManager, firstFilePath: String): String {
|
||||||
|
val firstFile = storageManager.getFileByDecryptedRemotePath(firstFilePath)
|
||||||
|
val topParentFile = storageManager.getTopParent(firstFile)
|
||||||
return topParentFile.decryptedRemotePath
|
return topParentFile.decryptedRemotePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -975,6 +975,10 @@ public class FileOperationsHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (file.isFolder()) {
|
||||||
|
// TODO: Call cancellation function
|
||||||
|
}
|
||||||
|
|
||||||
if (FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) {
|
if (FileDownloadHelper.Companion.instance().isDownloading(currentUser, file)) {
|
||||||
List<OCFile> files = fileActivity.getStorageManager().getAllFilesRecursivelyInsideFolder(file);
|
List<OCFile> files = fileActivity.getStorageManager().getAllFilesRecursivelyInsideFolder(file);
|
||||||
FileDownloadHelper.Companion.instance().cancelPendingOrCurrentDownloads(currentUser, files);
|
FileDownloadHelper.Companion.instance().cancelPendingOrCurrentDownloads(currentUser, files);
|
||||||
|
|
Loading…
Reference in a new issue