X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/56eaed4529542c1db660f66403111a60b0673abd..a6a010440d881d3f9dbd2fa50d6345275c0d0a0d:/src/com/owncloud/android/operations/SynchronizeFolderOperation.java diff --git a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java index 37f5b46d..72ccd135 100644 --- a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -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; @@ -73,9 +75,6 @@ public class SynchronizeFolderOperation extends SyncOperation { /** Remote folder to synchronize */ private OCFile mLocalFolder; - /** Access to the local database */ - private FileDataStorageManager mStorageManager; - /** Account where the file to synchronize belongs */ private Account mAccount; @@ -99,6 +98,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); /** @@ -112,7 +112,6 @@ public class SynchronizeFolderOperation extends SyncOperation { public SynchronizeFolderOperation(Context context, String remotePath, Account account, long currentSyncTime){ mLocalFolder = new OCFile(remotePath); mCurrentSyncTime = currentSyncTime; - mStorageManager = getStorageManager(); mAccount = account; mContext = context; mForgottenLocalFiles = new HashMap(); @@ -154,13 +153,20 @@ public class SynchronizeFolderOperation extends SyncOperation { mConflictsFound = 0; mForgottenLocalFiles.clear(); + /// perform the download + synchronized(mCancellationRequested) { + if (mCancellationRequested.get()) { + return new RemoteOperationResult(new OperationCancelledException()); + } + } + result = checkForChanges(client); if (result.isSuccess()) { if (mRemoteFolderChanged) { result = fetchAndSyncRemoteFolder(client); } else { - mChildren = mStorageManager.getFolderContent(mLocalFolder); + mChildren = getStorageManager().getFolderContent(mLocalFolder); } } @@ -231,9 +237,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,8 +264,10 @@ public class SynchronizeFolderOperation extends SyncOperation { * @return 'True' when any change was made in the local data, 'false' otherwise */ private void synchronizeData(ArrayList folderAndFiles, OwnCloudClient client) { + FileDataStorageManager storageManager = getStorageManager(); + // get 'fresh data' from the database - mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath()); + mLocalFolder = storageManager.getFileByPath(mLocalFolder.getRemotePath()); // parse data from remote folder OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0)); @@ -272,7 +281,7 @@ public class SynchronizeFolderOperation extends SyncOperation { List filesToSyncContents = new Vector(); // get current data about local contents of the folder to synchronize - List localFiles = mStorageManager.getFolderContent(mLocalFolder); + List localFiles = storageManager.getFolderContent(mLocalFolder); Map localFilesMap = new HashMap(localFiles.size()); for (OCFile file : localFiles) { localFilesMap.put(file.getRemotePath(), file); @@ -341,7 +350,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 +362,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 +385,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 +524,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; }