浏览代码

Merge pull request #13993 from nextcloud/bugfix/dont-use-file-download-worker-for-two-way-sync

BugFix - Don't Use File Download Worker for Two Way Sync
Tobias Kaminsky 6 月之前
父节点
当前提交
ca29ea92af

+ 9 - 4
app/src/main/java/com/nextcloud/client/jobs/InternalTwoWaySyncWork.kt

@@ -32,6 +32,7 @@ class InternalTwoWaySyncWork(
     private val appPreferences: AppPreferences
     private val appPreferences: AppPreferences
 ) : Worker(context, params) {
 ) : Worker(context, params) {
     private var shouldRun = true
     private var shouldRun = true
+    private var operation: SynchronizeFolderOperation? = null
 
 
     override fun doWork(): Result {
     override fun doWork(): Result {
         Log_OC.d(TAG, "Worker started!")
         Log_OC.d(TAG, "Worker started!")
@@ -66,10 +67,10 @@ class InternalTwoWaySyncWork(
                 }
                 }
 
 
                 Log_OC.d(TAG, "Folder ${folder.remotePath}: started!")
                 Log_OC.d(TAG, "Folder ${folder.remotePath}: started!")
-                val operation = SynchronizeFolderOperation(context, folder.remotePath, user, fileDataStorageManager)
-                    .execute(context)
+                operation = SynchronizeFolderOperation(context, folder.remotePath, user, fileDataStorageManager, true)
+                val operationResult = operation?.execute(context)
 
 
-                if (operation.isSuccess) {
+                if (operationResult?.isSuccess == true) {
                     Log_OC.d(TAG, "Folder ${folder.remotePath}: finished!")
                     Log_OC.d(TAG, "Folder ${folder.remotePath}: finished!")
                 } else {
                 } else {
                     Log_OC.d(TAG, "Folder ${folder.remotePath} failed!")
                     Log_OC.d(TAG, "Folder ${folder.remotePath} failed!")
@@ -77,7 +78,10 @@ class InternalTwoWaySyncWork(
                 }
                 }
 
 
                 folder.apply {
                 folder.apply {
-                    internalFolderSyncResult = operation.code.toString()
+                    operationResult?.let {
+                        internalFolderSyncResult = it.code.toString()
+                    }
+
                     internalFolderSyncTimestamp = System.currentTimeMillis()
                     internalFolderSyncTimestamp = System.currentTimeMillis()
                 }
                 }
 
 
@@ -96,6 +100,7 @@ class InternalTwoWaySyncWork(
 
 
     override fun onStopped() {
     override fun onStopped() {
         Log_OC.d(TAG, "OnStopped of worker called!")
         Log_OC.d(TAG, "OnStopped of worker called!")
+        operation?.cancel()
         shouldRun = false
         shouldRun = false
         super.onStopped()
         super.onStopped()
     }
     }

+ 2 - 1
app/src/main/java/com/nextcloud/client/jobs/OfflineSyncWork.kt

@@ -77,7 +77,8 @@ class OfflineSyncWork constructor(
                     user,
                     user,
                     true,
                     true,
                     context,
                     context,
-                    storageManager
+                    storageManager,
+                    true
                 )
                 )
                 synchronizeFileOperation.execute(context)
                 synchronizeFileOperation.execute(context)
             }
             }

+ 33 - 11
app/src/main/java/com/owncloud/android/operations/SynchronizeFileOperation.java

@@ -46,6 +46,8 @@ public class SynchronizeFileOperation extends SyncOperation {
     private boolean mSyncFileContents;
     private boolean mSyncFileContents;
     private Context mContext;
     private Context mContext;
     private boolean mTransferWasRequested;
     private boolean mTransferWasRequested;
+    private final boolean syncInBackgroundWorker;
+
 
 
     /**
     /**
      * When 'false', uploads to the server are not done; only downloads or conflict detection. This is a temporal
      * When 'false', uploads to the server are not done; only downloads or conflict detection. This is a temporal
@@ -74,7 +76,8 @@ public class SynchronizeFileOperation extends SyncOperation {
         User user,
         User user,
         boolean syncFileContents,
         boolean syncFileContents,
         Context context,
         Context context,
-        FileDataStorageManager storageManager) {
+        FileDataStorageManager storageManager,
+        boolean syncInBackgroundWorker) {
         super(storageManager);
         super(storageManager);
 
 
         mRemotePath = remotePath;
         mRemotePath = remotePath;
@@ -84,6 +87,7 @@ public class SynchronizeFileOperation extends SyncOperation {
         mSyncFileContents = syncFileContents;
         mSyncFileContents = syncFileContents;
         mContext = context;
         mContext = context;
         mAllowUploads = true;
         mAllowUploads = true;
+        this.syncInBackgroundWorker = syncInBackgroundWorker;
     }
     }
 
 
 
 
@@ -110,7 +114,8 @@ public class SynchronizeFileOperation extends SyncOperation {
         User user,
         User user,
         boolean syncFileContents,
         boolean syncFileContents,
         Context context,
         Context context,
-        FileDataStorageManager storageManager) {
+        FileDataStorageManager storageManager,
+        boolean syncInBackgroundWorker) {
         super(storageManager);
         super(storageManager);
 
 
         mLocalFile = localFile;
         mLocalFile = localFile;
@@ -130,6 +135,7 @@ public class SynchronizeFileOperation extends SyncOperation {
         mSyncFileContents = syncFileContents;
         mSyncFileContents = syncFileContents;
         mContext = context;
         mContext = context;
         mAllowUploads = true;
         mAllowUploads = true;
+        this.syncInBackgroundWorker = syncInBackgroundWorker;
     }
     }
 
 
 
 
@@ -159,9 +165,9 @@ public class SynchronizeFileOperation extends SyncOperation {
         boolean syncFileContents,
         boolean syncFileContents,
         boolean allowUploads,
         boolean allowUploads,
         Context context,
         Context context,
-        FileDataStorageManager storageManager) {
-
-        this(localFile, serverFile, user, syncFileContents, context, storageManager);
+        FileDataStorageManager storageManager,
+        boolean syncInBackgroundWorker) {
+        this(localFile, serverFile, user, syncFileContents, context, storageManager, syncInBackgroundWorker);
         mAllowUploads = allowUploads;
         mAllowUploads = allowUploads;
     }
     }
 
 
@@ -295,13 +301,29 @@ public class SynchronizeFileOperation extends SyncOperation {
     }
     }
 
 
     private void requestForDownload(OCFile file) {
     private void requestForDownload(OCFile file) {
-        Log_OC.d("InternalTwoWaySyncWork", "download file: " + file.getFileName());
-        
-        FileDownloadHelper.Companion.instance().downloadFile(
-            mUser,
-            file);
+        if (syncInBackgroundWorker) {
+            Log_OC.d("InternalTwoWaySyncWork", "download file: " + file.getFileName());
 
 
-        mTransferWasRequested = true;
+            try {
+                final var operation = new DownloadFileOperation(mUser, file, mContext);
+                var result = operation.execute(getClient());
+
+                mTransferWasRequested = true;
+
+                String filename = file.getFileName();
+                if (filename != null) {
+                    if (result.isSuccess()) {
+                        Log_OC.d(TAG, "requestForDownload completed for: " + file.getFileName());
+                    } else {
+                        Log_OC.d(TAG, "requestForDownload failed for: " + file.getFileName());
+                    }
+                }
+            } catch (Exception e) {
+                Log_OC.d(TAG, "Exception caught at requestForDownload" + e);
+            }
+        } else {
+            FileDownloadHelper.Companion.instance().downloadFile(mUser, file);
+        }
     }
     }
 
 
     public boolean transferWasRequested() {
     public boolean transferWasRequested() {

+ 43 - 6
app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java

@@ -40,7 +40,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.Vector;
 import java.util.Vector;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.function.Consumer;
 
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
 
@@ -87,6 +86,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
 
     private final AtomicBoolean mCancellationRequested;
     private final AtomicBoolean mCancellationRequested;
 
 
+    private final boolean syncInBackgroundWorker;
+
     /**
     /**
      * Creates a new instance of {@link SynchronizeFolderOperation}.
      * Creates a new instance of {@link SynchronizeFolderOperation}.
      *
      *
@@ -97,7 +98,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
     public SynchronizeFolderOperation(Context context,
     public SynchronizeFolderOperation(Context context,
                                       String remotePath,
                                       String remotePath,
                                       User user,
                                       User user,
-                                      FileDataStorageManager storageManager) {
+                                      FileDataStorageManager storageManager,
+                                      boolean syncInBackgroundWorker) {
         super(storageManager);
         super(storageManager);
 
 
         mRemotePath = remotePath;
         mRemotePath = remotePath;
@@ -107,6 +109,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
         mFilesForDirectDownload = new Vector<>();
         mFilesForDirectDownload = new Vector<>();
         mFilesToSyncContents = new Vector<>();
         mFilesToSyncContents = new Vector<>();
         mCancellationRequested = new AtomicBoolean(false);
         mCancellationRequested = new AtomicBoolean(false);
+        this.syncInBackgroundWorker = syncInBackgroundWorker;
     }
     }
 
 
 
 
@@ -406,7 +409,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
                 user,
                 user,
                 true,
                 true,
                 mContext,
                 mContext,
-                getStorageManager()
+                getStorageManager(),
+                syncInBackgroundWorker
             );
             );
             mFilesToSyncContents.add(operation);
             mFilesToSyncContents.add(operation);
         }
         }
@@ -427,7 +431,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
                         user,
                         user,
                         true,
                         true,
                         mContext,
                         mContext,
-                        getStorageManager()
+                        getStorageManager(),
+                        syncInBackgroundWorker
                     );
                     );
                     mFilesToSyncContents.add(operation);
                     mFilesToSyncContents.add(operation);
                 }
                 }
@@ -441,8 +446,40 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
     }
 
 
     private void startDirectDownloads() {
     private void startDirectDownloads() {
-        final var fileDownloadHelper = FileDownloadHelper.Companion.instance();
-        mFilesForDirectDownload.forEach(file -> fileDownloadHelper.downloadFile(user, file));
+        if (syncInBackgroundWorker) {
+            try {
+                for (OCFile file: mFilesForDirectDownload) {
+                    synchronized (mCancellationRequested) {
+                        if (mCancellationRequested.get()) {
+                            break;
+                        }
+                    }
+
+                    if (file == null) {
+                        continue;
+                    }
+
+                    final var operation = new DownloadFileOperation(user, file, mContext);
+                    var result = operation.execute(getClient());
+
+                    String filename = file.getFileName();
+                    if (filename == null) {
+                        continue;
+                    }
+
+                    if (result.isSuccess()) {
+                        Log_OC.d(TAG, "startDirectDownloads completed for: " + file.getFileName());
+                    } else {
+                        Log_OC.d(TAG, "startDirectDownloads failed for: " + file.getFileName());
+                    }
+                }
+            } catch (Exception e) {
+                Log_OC.d(TAG, "Exception caught at startDirectDownloads" + e);
+            }
+        } else {
+            final var fileDownloadHelper = FileDownloadHelper.Companion.instance();
+            mFilesForDirectDownload.forEach(file -> fileDownloadHelper.downloadFile(user, file));
+        }
     }
     }
 
 
     /**
     /**

+ 4 - 2
app/src/main/java/com/owncloud/android/services/OperationsService.java

@@ -698,7 +698,8 @@ public class OperationsService extends Service {
                                                                  user,
                                                                  user,
                                                                  syncFileContents,
                                                                  syncFileContents,
                                                                  getApplicationContext(),
                                                                  getApplicationContext(),
-                                                                 fileDataStorageManager);
+                                                                 fileDataStorageManager,
+                                                                 false);
                         break;
                         break;
 
 
                     case ACTION_SYNC_FOLDER:
                     case ACTION_SYNC_FOLDER:
@@ -707,7 +708,8 @@ public class OperationsService extends Service {
                             this,                       // TODO remove this dependency from construction time
                             this,                       // TODO remove this dependency from construction time
                             remotePath,
                             remotePath,
                             user,
                             user,
-                            fileDataStorageManager
+                            fileDataStorageManager,
+                            false
                         );
                         );
                         break;
                         break;
 
 

+ 4 - 2
app/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

@@ -225,7 +225,8 @@ public class FileOperationsHelper {
                                                                     user,
                                                                     user,
                                                                     true,
                                                                     true,
                                                                     fileActivity,
                                                                     fileActivity,
-                                                                    storageManager);
+                                                                    storageManager,
+                                                                    false);
         RemoteOperationResult result = sfo.execute(fileActivity);
         RemoteOperationResult result = sfo.execute(fileActivity);
 
 
         if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) {
         if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) {
@@ -303,7 +304,8 @@ public class FileOperationsHelper {
                                                                                 user,
                                                                                 user,
                                                                                 true,
                                                                                 true,
                                                                                 fileActivity,
                                                                                 fileActivity,
-                                                                                storageManager);
+                                                                                storageManager,
+                                                                                false);
                     RemoteOperationResult result = sfo.execute(fileActivity);
                     RemoteOperationResult result = sfo.execute(fileActivity);
                     fileActivity.dismissLoadingDialog();
                     fileActivity.dismissLoadingDialog();
                     if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) {
                     if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) {