From 47cd4b2fd5f4d2f72f63c4838c8702c6dfe349f2 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Tue, 9 Jul 2024 12:02:46 +0200 Subject: [PATCH] Adopt to async execution Signed-off-by: alperozturk --- .../client/jobs/upload/FileUploadHelper.kt | 43 ++++++++++++++++--- .../operations/UploadFileOperation.java | 4 ++ .../ui/activity/ConflictsResolveActivity.kt | 25 +++++++++-- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt b/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt index 1f2a94511d..ea624be46b 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt @@ -16,6 +16,7 @@ import com.nextcloud.client.device.BatteryStatus import com.nextcloud.client.device.PowerManagementService import com.nextcloud.client.jobs.BackgroundJobManager import com.nextcloud.client.jobs.upload.FileUploadWorker.Companion.currentUploadFileOperation +import com.nextcloud.client.network.ClientFactory import com.nextcloud.client.network.Connectivity import com.nextcloud.client.network.ConnectivityService import com.owncloud.android.MainApp @@ -31,9 +32,12 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation import com.owncloud.android.lib.resources.files.model.RemoteFile +import com.owncloud.android.operations.RemoveFileOperation import com.owncloud.android.operations.UploadFileOperation -import com.owncloud.android.ui.helpers.FileOperationsHelper import com.owncloud.android.utils.FileUtil +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import java.io.File import java.util.concurrent.Semaphore import javax.inject.Inject @@ -319,7 +323,7 @@ class FileUploadHelper { // For file conflicts check old file remote path upload.remotePath == currentUploadFileOperation.remotePath || upload.remotePath == currentUploadFileOperation.oldFile!! - .remotePath + .remotePath } else { upload.remotePath == currentUploadFileOperation.remotePath } @@ -354,12 +358,41 @@ class FileUploadHelper { backgroundJobManager.startFilesUploadJob(user) } - fun removeAnyOtherFileHaveSameName(newFile: OCFile, fileOperationsHelper: FileOperationsHelper) { + fun removeAnyOtherFileHaveSameName( + newFile: OCFile, + clientFactory: ClientFactory, + user: User, + onCompleted: () -> Unit + ) { val parentFile: OCFile? = fileStorageManager.getFileById(newFile.parentId) val folderContent: List = fileStorageManager.getFolderContent(parentFile, false) - folderContent.firstOrNull { it.fileName == newFile.fileName }?.let { replacedFile -> - fileOperationsHelper.removeFiles(listOf(replacedFile), false, true) + val replacedFile: OCFile? = folderContent.find { it.fileName == newFile.fileName } + + replacedFile?.let { + val job = CoroutineScope(Dispatchers.IO) + + job.launch { + val client = clientFactory.create(user) + val removeFileOperation = RemoveFileOperation( + it, + false, + user, + true, + MainApp.getAppContext(), + fileStorageManager + ) + + val result = removeFileOperation.execute(client) + + if (result.isSuccess) { + Log_OC.d(TAG, "Replaced file successfully removed") + + launch(Dispatchers.Main) { + onCompleted() + } + } + } } } diff --git a/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java b/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java index a03a8f0b69..9dd098458a 100644 --- a/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java @@ -881,6 +881,10 @@ public class UploadFileOperation extends SyncOperation { result = unlockFolderResult; } + if (unlockFolderResult != null && unlockFolderResult.isSuccess()) { + Log_OC.d(TAG, "Folder successfully unlocked: " + e2eFiles.getParentFile().getFileName()); + } + e2eFiles.deleteEncryptedTempFile(); return result; diff --git a/app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt b/app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt index 1901c0c506..172b2c14c4 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt +++ b/app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt @@ -13,12 +13,15 @@ import android.content.Context import android.content.Intent import android.os.Bundle import android.widget.Toast +import androidx.lifecycle.Observer import com.nextcloud.client.account.User import com.nextcloud.client.jobs.download.FileDownloadHelper import com.nextcloud.client.jobs.upload.FileUploadHelper import com.nextcloud.client.jobs.upload.FileUploadWorker import com.nextcloud.client.jobs.upload.UploadNotificationManager import com.nextcloud.model.HTTPStatusCodes +import com.nextcloud.model.WorkerState +import com.nextcloud.model.WorkerStateLiveData import com.nextcloud.utils.extensions.getParcelableArgument import com.nextcloud.utils.extensions.logFileSize import com.owncloud.android.R @@ -83,6 +86,24 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener } } + private val workerObserver = Observer { state -> + if (state is WorkerState.Upload) { + Log_OC.d(TAG, "Upload observation started") + + newFile?.let { + val user = user.orElseThrow { RuntimeException() } + FileUploadHelper.instance().removeAnyOtherFileHaveSameName(file, clientFactory, user, onCompleted = { + removeWorkerObserver() + }) + } + } + } + + private fun removeWorkerObserver() { + Log_OC.d(TAG, "Upload observation stopped") + WorkerStateLiveData.instance().removeObserver(workerObserver) + } + private fun setupOnConflictDecisionMadeListener(upload: OCUpload?) { listener = OnConflictDecisionMadeListener { decision: Decision? -> val file = newFile // local file got changed, so either upload it or replace it again by server @@ -101,9 +122,7 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener NameCollisionPolicy.OVERWRITE ) - file?.let { - FileUploadHelper.instance().removeAnyOtherFileHaveSameName(file, fileOperationsHelper) - } + WorkerStateLiveData.instance().observe(this, workerObserver) } Decision.KEEP_BOTH -> {