Notify the download cancelation for updating the screen and remove the yellow arrow...
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFolderOperation.java
index 4e1e5bc..f12d5dd 100644 (file)
@@ -27,7 +27,6 @@ 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.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.RemoteOperation;
 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;
 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;
@@ -89,7 +88,6 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
     /** 'True' means that the remote folder changed and should be fetched */
     private boolean mRemoteFolderChanged;
 
     /** 'True' means that the remote folder changed and should be fetched */
     private boolean mRemoteFolderChanged;
-    private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
 
     private List<OCFile> mFilesForDirectDownload;
         // to avoid extra PROPFINDs when there was no change in the folder
 
     private List<OCFile> mFilesForDirectDownload;
         // to avoid extra PROPFINDs when there was no change in the folder
@@ -102,6 +100,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
 
     private List<SyncOperation> mFoldersToWalkDown;
 
 
     private List<SyncOperation> mFoldersToWalkDown;
 
+    private final AtomicBoolean mCancellationRequested;
 
     /**
      * Creates a new instance of {@link SynchronizeFolderOperation}.
 
     /**
      * Creates a new instance of {@link SynchronizeFolderOperation}.
@@ -121,7 +120,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
         mFilesToSyncContentsWithoutUpload = new Vector<SyncOperation>();
         mFavouriteFilesToSyncContents = new Vector<SyncOperation>();
         mFoldersToWalkDown = new Vector<SyncOperation>();
         mFilesToSyncContentsWithoutUpload = new Vector<SyncOperation>();
         mFavouriteFilesToSyncContents = new Vector<SyncOperation>();
         mFoldersToWalkDown = new Vector<SyncOperation>();
-        
+        mCancellationRequested = new AtomicBoolean(false);
     }
 
 
     }
 
 
@@ -143,45 +142,64 @@ public class SynchronizeFolderOperation extends SyncOperation {
         RemoteOperationResult result = null;
         mFailsInFileSyncsFound = 0;
         mConflictsFound = 0;
         RemoteOperationResult result = null;
         mFailsInFileSyncsFound = 0;
         mConflictsFound = 0;
-
-        synchronized(mCancellationRequested) {
-            if (mCancellationRequested.get()) {
-                // Cancel each operation in mFoldersToWalkDown
-                for (SyncOperation  synchOp: mFoldersToWalkDown) {
-                    ((SynchronizeFolderOperation) synchOp).cancel();
-                }
-                return new RemoteOperationResult(new OperationCancelledException());
-            }
-        }
-
-        // get locally cached information about folder 
-        mLocalFolder = getStorageManager().getFileByPath(mRemotePath);   
         
         
-        result = checkForChanges(client);
-
-        if (result.isSuccess()) {
-            if (mRemoteFolderChanged) {
-                result = fetchAndSyncRemoteFolder(client);
+        try {
+            // get locally cached information about folder 
+            mLocalFolder = getStorageManager().getFileByPath(mRemotePath);   
+            
+            result = checkForChanges(client);
+    
+            if (result.isSuccess()) {
+                if (mRemoteFolderChanged) {
+                    result = fetchAndSyncRemoteFolder(client);
+                    
+                } else {
+                    prepareOpsFromLocalKnowledge();
+                }
                 
                 
-            } else {
-                prepareOpsFromLocalKnowledge();
+                if (result.isSuccess()) {
+                    syncContents(client);
+                }
+
+                if (mFilesForDirectDownload.isEmpty()) {
+                    sendBroadcastForNotifyingUIUpdate(result.isSuccess());
+                }
             }
             
             }
             
-            if (result.isSuccess()) {
-                syncContents(client);
+            if (mCancellationRequested.get()) {
+                throw new OperationCancelledException();
             }
             }
+            
+        } catch (OperationCancelledException e) {
+            result = new RemoteOperationResult(e);
+
+            // Needed in case that cancellation occurs before starting any download.
+            // If not, yellow arrow continues being shown.
+            sendBroadcastForNotifyingUIUpdate(result.isSuccess());
+
+            /// cancellation of download needs to be done separately in any case; a SynchronizeFolderOperation
+            //  may finish much sooner than the real download of the files in the folder 
+            Intent intent = new Intent(mContext, FileDownloader.class);
+            intent.setAction(FileDownloader.ACTION_CANCEL_FILE_DOWNLOAD);
+            intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
+            intent.putExtra(FileDownloader.EXTRA_FILE, mLocalFolder);
+            mContext.startService(intent);            
         }
 
         return result;
 
     }
 
         }
 
         return result;
 
     }
 
-    private RemoteOperationResult checkForChanges(OwnCloudClient client) {
+    private RemoteOperationResult checkForChanges(OwnCloudClient client) throws OperationCancelledException {
         Log_OC.d(TAG, "Checking changes in " + mAccount.name + mRemotePath);
 
         mRemoteFolderChanged = true;
         RemoteOperationResult result = null;
         
         Log_OC.d(TAG, "Checking changes in " + mAccount.name + mRemotePath);
 
         mRemoteFolderChanged = true;
         RemoteOperationResult result = null;
         
+        if (mCancellationRequested.get()) {
+            throw new OperationCancelledException();
+        }
+        
         // remote request
         ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mRemotePath);
         result = operation.execute(client);
         // remote request
         ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mRemotePath);
         result = operation.execute(client);
@@ -209,13 +227,19 @@ public class SynchronizeFolderOperation extends SyncOperation {
                 Log_OC.e(TAG, "Checked " + mAccount.name + mRemotePath + " : " +
                         result.getLogMessage());
             }
                 Log_OC.e(TAG, "Checked " + mAccount.name + mRemotePath + " : " +
                         result.getLogMessage());
             }
+
+            sendBroadcastForNotifyingUIUpdate(result.isSuccess());
         }
 
         return result;
     }
 
 
         }
 
         return result;
     }
 
 
-    private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) {
+    private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) throws OperationCancelledException {
+        if (mCancellationRequested.get()) {
+            throw new OperationCancelledException();
+        }
+        
         ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(mRemotePath);
         RemoteOperationResult result = operation.execute(client);
         Log_OC.d(TAG, "Synchronizing " + mAccount.name + mRemotePath);
         ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(mRemotePath);
         RemoteOperationResult result = operation.execute(client);
         Log_OC.d(TAG, "Synchronizing " + mAccount.name + mRemotePath);
@@ -263,7 +287,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
      *                          retrieved.
      *  @return                 'True' when any change was made in the local data, 'false' otherwise
      */
      *                          retrieved.
      *  @return                 'True' when any change was made in the local data, 'false' otherwise
      */
-    private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client) {
+    private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client)
+            throws OperationCancelledException {
         FileDataStorageManager storageManager = getStorageManager();
         
         // parse data from remote folder
         FileDataStorageManager storageManager = getStorageManager();
         
         // parse data from remote folder
@@ -278,7 +303,13 @@ public class SynchronizeFolderOperation extends SyncOperation {
         mFilesForDirectDownload.clear();
         mFilesToSyncContentsWithoutUpload.clear();
         mFavouriteFilesToSyncContents.clear();
         mFilesForDirectDownload.clear();
         mFilesToSyncContentsWithoutUpload.clear();
         mFavouriteFilesToSyncContents.clear();
-        mFoldersToWalkDown.clear();
+
+        synchronized(mFoldersToWalkDown) {
+            if (mCancellationRequested.get()) {
+                throw new OperationCancelledException();
+            }
+            mFoldersToWalkDown.clear();
+        }
 
         // get current data about local contents of the folder to synchronize
         List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder);
 
         // get current data about local contents of the folder to synchronize
         List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder);
@@ -340,8 +371,14 @@ public class SynchronizeFolderOperation extends SyncOperation {
                         mAccount,
                         mCurrentSyncTime
                 );
                         mAccount,
                         mCurrentSyncTime
                 );
-                mFoldersToWalkDown.add(synchFolderOp);
-                
+
+                synchronized(mFoldersToWalkDown) {
+                    if (mCancellationRequested.get()) {
+                        throw new OperationCancelledException();
+                    }
+                    mFoldersToWalkDown.add(synchFolderOp);
+                }
+
             } else if (remoteFile.keepInSync()) {
                 /// prepare content synchronization for kept-in-sync files
                 SynchronizeFileOperation operation = new SynchronizeFileOperation(
             } else if (remoteFile.keepInSync()) {
                 /// prepare content synchronization for kept-in-sync files
                 SynchronizeFileOperation operation = new SynchronizeFileOperation(
@@ -375,7 +412,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
     
     
     }
     
     
-    private void prepareOpsFromLocalKnowledge() {
+    private void prepareOpsFromLocalKnowledge() throws OperationCancelledException {
         List<OCFile> children = getStorageManager().getFolderContent(mLocalFolder);
         for (OCFile child : children) {
             /// classify file to sync/download contents later
         List<OCFile> children = getStorageManager().getFolderContent(mLocalFolder);
         for (OCFile child : children) {
             /// classify file to sync/download contents later
@@ -387,8 +424,14 @@ public class SynchronizeFolderOperation extends SyncOperation {
                         mAccount,
                         mCurrentSyncTime
                 );
                         mAccount,
                         mCurrentSyncTime
                 );
-                mFoldersToWalkDown.add(synchFolderOp);
-                
+
+                synchronized(mFoldersToWalkDown) {
+                    if (mCancellationRequested.get()) {
+                        throw new OperationCancelledException();
+                    }
+                    mFoldersToWalkDown.add(synchFolderOp);
+                }
+
             } else {
                 /// prepare limited synchronization for regular files
                 if (!child.isDown()) {
             } else {
                 /// prepare limited synchronization for regular files
                 if (!child.isDown()) {
@@ -399,7 +442,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
 
 
     }
 
 
-    private void syncContents(OwnCloudClient client) {
+    private void syncContents(OwnCloudClient client) throws OperationCancelledException {
         startDirectDownloads();
         startContentSynchronizations(mFilesToSyncContentsWithoutUpload, client);
         startContentSynchronizations(mFavouriteFilesToSyncContents, client);
         startDirectDownloads();
         startContentSynchronizations(mFilesToSyncContentsWithoutUpload, client);
         startContentSynchronizations(mFavouriteFilesToSyncContents, client);
@@ -407,8 +450,11 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
 
     
     }
 
     
-    private void startDirectDownloads() {
+    private void startDirectDownloads() throws OperationCancelledException {
         for (OCFile file : mFilesForDirectDownload) {
         for (OCFile file : mFilesForDirectDownload) {
+            if (mCancellationRequested.get()) {
+                throw new OperationCancelledException();
+            }
             Intent i = new Intent(mContext, FileDownloader.class);
             i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
             i.putExtra(FileDownloader.EXTRA_FILE, file);
             Intent i = new Intent(mContext, FileDownloader.class);
             i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
             i.putExtra(FileDownloader.EXTRA_FILE, file);
@@ -426,9 +472,14 @@ public class SynchronizeFolderOperation extends SyncOperation {
      * @param filesToSyncContents       Synchronization operations to execute.
      * @param client                    Interface to the remote ownCloud server.
      */
      * @param filesToSyncContents       Synchronization operations to execute.
      * @param client                    Interface to the remote ownCloud server.
      */
-    private void startContentSynchronizations(List<SyncOperation> filesToSyncContents, OwnCloudClient client) {
+    private void startContentSynchronizations(List<SyncOperation> filesToSyncContents, OwnCloudClient client) 
+            throws OperationCancelledException {
+        
         RemoteOperationResult contentsResult = null;
         for (SyncOperation op: filesToSyncContents) {
         RemoteOperationResult contentsResult = null;
         for (SyncOperation op: filesToSyncContents) {
+            if (mCancellationRequested.get()) {
+                throw new OperationCancelledException();
+            }
             contentsResult = op.execute(getStorageManager(), mContext);
             if (!contentsResult.isSuccess()) {
                 if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
             contentsResult = op.execute(getStorageManager(), mContext);
             if (!contentsResult.isSuccess()) {
                 if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
@@ -449,9 +500,12 @@ public class SynchronizeFolderOperation extends SyncOperation {
     }
 
 
     }
 
 
-    private void walkSubfolders(OwnCloudClient client) {
+    private void walkSubfolders(OwnCloudClient client) throws OperationCancelledException {
         RemoteOperationResult contentsResult = null;
         for (SyncOperation op: mFoldersToWalkDown) {
         RemoteOperationResult contentsResult = null;
         for (SyncOperation op: mFoldersToWalkDown) {
+            if (mCancellationRequested.get()) {
+                throw new OperationCancelledException();
+            }
             contentsResult = op.execute(client, getStorageManager());   // to watch out: possibly deep recursion
             if (!contentsResult.isSuccess()) {
                 // TODO - some kind of error count, and use it with notifications
             contentsResult = op.execute(client, getStorageManager());   // to watch out: possibly deep recursion
             if (!contentsResult.isSuccess()) {
                 // TODO - some kind of error count, and use it with notifications
@@ -465,7 +519,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
         }
     }
 
         }
     }
 
-
+    
     /**
      * Creates and populates a new {@link com.owncloud.android.datamodel.OCFile} object with the data read from the server.
      *
     /**
      * Creates and populates a new {@link com.owncloud.android.datamodel.OCFile} object with the data read from the server.
      *
@@ -502,11 +556,36 @@ public class SynchronizeFolderOperation extends SyncOperation {
         }
     }
 
         }
     }
 
+    private void sendBroadcastForNotifyingUIUpdate(boolean result) {
+        // Send a broadcast message for notifying UI update
+        Intent uiUpdate = new Intent(FileDownloader.getDownloadFinishMessage());
+        uiUpdate.putExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, result);
+        uiUpdate.putExtra(FileDownloader.ACCOUNT_NAME, mAccount.name);
+        uiUpdate.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mRemotePath);
+        uiUpdate.putExtra(FileDownloader.EXTRA_FILE_PATH, mLocalFolder.getRemotePath());
+        mContext.sendStickyBroadcast(uiUpdate);
+    }
+
     
     /**
      * Cancel operation
      */
     
     /**
      * Cancel operation
      */
-    public void cancel(){
+    public void cancel() {
         mCancellationRequested.set(true);
         mCancellationRequested.set(true);
+
+        synchronized(mFoldersToWalkDown) {
+            // cancel 'child' synchronizations
+            for (SyncOperation synchOp : mFoldersToWalkDown) {
+                ((SynchronizeFolderOperation) synchOp).cancel();
+            }
+        }
+    }
+
+    public String getFolderPath() {
+        String path = mLocalFolder.getStoragePath();
+        if (path != null && path.length() > 0) {
+            return path;
+        }
+        return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mLocalFolder);
     }
 }
     }
 }