Delay load of cached information about folder (mLocalFolder) until the operation...
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFolderOperation.java
index 37f5b46..17db936 100644 (file)
@@ -26,6 +26,7 @@ import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.operations.OperationCancelledException;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
 import com.owncloud.android.lib.common.utils.Log_OC;
@@ -48,6 +49,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Vector;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 //import android.support.v4.content.LocalBroadcastManager;
 
@@ -70,18 +72,18 @@ public class SynchronizeFolderOperation extends SyncOperation {
     /** Time stamp for the synchronization process in progress */
     private long mCurrentSyncTime;
 
-    /** Remote folder to synchronize */
-    private OCFile mLocalFolder;
-
-    /** Access to the local database */
-    private FileDataStorageManager mStorageManager;
-
+    /** Remote path of the folder to synchronize */
+    private String mRemotePath;
+    
     /** Account where the file to synchronize belongs */
     private Account mAccount;
 
     /** Android context; necessary to send requests to the download service */
     private Context mContext;
 
+    /** Locally cached information about folder to synchronize */
+    private OCFile mLocalFolder;
+
     /** Files and folders contained in the synchronized folder after a successful operation */
     private List<OCFile> mChildren;
 
@@ -99,6 +101,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
     /** 'True' means that the remote folder changed and should be fetched */
     private boolean mRemoteFolderChanged;
+    private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
 
 
     /**
@@ -110,9 +113,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
      * @param   currentSyncTime         Time stamp for the synchronization process in progress.
      */
     public SynchronizeFolderOperation(Context context, String remotePath, Account account, long currentSyncTime){
-        mLocalFolder = new OCFile(remotePath);
+        mRemotePath = remotePath;
         mCurrentSyncTime = currentSyncTime;
-        mStorageManager = getStorageManager();
         mAccount = account;
         mContext = context;
         mForgottenLocalFiles = new HashMap<String, String>();
@@ -154,13 +156,22 @@ public class SynchronizeFolderOperation extends SyncOperation {
         mConflictsFound = 0;
         mForgottenLocalFiles.clear();
 
+        synchronized(mCancellationRequested) {
+            if (mCancellationRequested.get()) {
+                return new RemoteOperationResult(new OperationCancelledException());
+            }
+        }
+
+        // get locally cached information about folder 
+        OCFile mLocalFolder = getStorageManager().getFileByPath(mRemotePath);   
+        
         result = checkForChanges(client);
 
         if (result.isSuccess()) {
             if (mRemoteFolderChanged) {
                 result = fetchAndSyncRemoteFolder(client);
             } else {
-                mChildren = mStorageManager.getFolderContent(mLocalFolder);
+                mChildren = getStorageManager().getFolderContent(mLocalFolder);
             }
         }
 
@@ -169,15 +180,13 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
 
     private RemoteOperationResult checkForChanges(OwnCloudClient client) {
+        Log_OC.d(TAG, "Checking changes in " + mAccount.name + mRemotePath);
+
         mRemoteFolderChanged = true;
         RemoteOperationResult result = null;
-        String remotePath = null;
-
-        remotePath = mLocalFolder.getRemotePath();
-        Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath);
-
+        
         // remote request
-        ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath);
+        ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mRemotePath);
         result = operation.execute(client);
         if (result.isSuccess()){
             OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
@@ -188,7 +197,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
             result = new RemoteOperationResult(ResultCode.OK);
 
-            Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " +
+            Log_OC.i(TAG, "Checked " + mAccount.name + mRemotePath + " : " +
                     (mRemoteFolderChanged ? "changed" : "not changed"));
 
         } else {
@@ -197,10 +206,10 @@ public class SynchronizeFolderOperation extends SyncOperation {
                 removeLocalFolder();
             }
             if (result.isException()) {
-                Log_OC.e(TAG, "Checked " + mAccount.name + remotePath  + " : " +
+                Log_OC.e(TAG, "Checked " + mAccount.name + mRemotePath  + " : " +
                         result.getLogMessage(), result.getException());
             } else {
-                Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " +
+                Log_OC.e(TAG, "Checked " + mAccount.name + mRemotePath + " : " +
                         result.getLogMessage());
             }
         }
@@ -210,10 +219,9 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
 
     private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) {
-        String remotePath = mLocalFolder.getRemotePath();
-        ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(remotePath);
+        ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(mRemotePath);
         RemoteOperationResult result = operation.execute(client);
-        Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
+        Log_OC.d(TAG, "Synchronizing " + mAccount.name + mRemotePath);
 
         if (result.isSuccess()) {
             synchronizeData(result.getData(), client);
@@ -231,9 +239,10 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
 
     private void removeLocalFolder() {
-        if (mStorageManager.fileExists(mLocalFolder.getFileId())) {
+        FileDataStorageManager storageManager = getStorageManager();
+        if (storageManager.fileExists(mLocalFolder.getFileId())) {
             String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
-            mStorageManager.removeFolder(
+            storageManager.removeFolder(
                     mLocalFolder,
                     true,
                     (   mLocalFolder.isDown() &&
@@ -257,9 +266,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
      *  @return                 'True' when any change was made in the local data, 'false' otherwise
      */
     private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client) {
-        // get 'fresh data' from the database
-        mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath());
-
+        FileDataStorageManager storageManager = getStorageManager();
+        
         // parse data from remote folder
         OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0));
         remoteFolder.setParentId(mLocalFolder.getParentId());
@@ -272,7 +280,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
         List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
 
         // get current data about local contents of the folder to synchronize
-        List<OCFile> localFiles = mStorageManager.getFolderContent(mLocalFolder);
+        List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder);
         Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
         for (OCFile file : localFiles) {
             localFilesMap.put(file.getRemotePath(), file);
@@ -341,7 +349,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
                 requestForDownloadFile(remoteFile);
             } else {
                 // Run new SyncFolderOperation for download children files recursively from a folder
-                RemoteOperation synchFolderOp =  new SyncFolderOperation( mContext,
+                SynchronizeFolderOperation synchFolderOp =  new SynchronizeFolderOperation( mContext,
                         remoteFile.getRemotePath(),
                         mAccount,
                         mCurrentSyncTime);
@@ -353,7 +361,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
         }
 
         // save updated contents in local database
-        mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
+        storageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
 
         // request for the synchronization of file contents AFTER saving current remote properties
         startContentSynchronizations(filesToSyncContents, client);
@@ -376,7 +384,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
         ) {
         RemoteOperationResult contentsResult = null;
         for (SynchronizeFileOperation op: filesToSyncContents) {
-            contentsResult = op.execute(mStorageManager, mContext);   // async
+            contentsResult = op.execute(getStorageManager(), mContext);   // async
             if (!contentsResult.isSuccess()) {
                 if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
                     mConflictsFound++;
@@ -515,6 +523,14 @@ public class SynchronizeFolderOperation extends SyncOperation {
         mContext.startService(i);
     }
 
+    /**
+     * Cancel operation
+     */
+    public void cancel(){
+        // WIP Cancel the sync operation
+        mCancellationRequested.set(true);
+    }
+
     public boolean getRemoteFolderChanged() {
         return mRemoteFolderChanged;
     }