+ private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client)
+ throws OperationCancelledException {
+ FileDataStorageManager storageManager = getStorageManager();
+
+ // parse data from remote folder
+ OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) folderAndFiles.get(0));
+ remoteFolder.setParentId(mLocalFolder.getParentId());
+ remoteFolder.setFileId(mLocalFolder.getFileId());
+
+ Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath()
+ + " changed - starting update of local data ");
+
+ List<OCFile> updatedFiles = new Vector<OCFile>(folderAndFiles.size() - 1);
+ mFilesForDirectDownload.clear();
+ mFilesToSyncContents.clear();
+
+ if (mCancellationRequested.get()) {
+ throw new OperationCancelledException();
+ }
+
+ // get current data about local contents of the folder to synchronize
+ // TODO Enable when "On Device" is recovered ?
+ List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder/*, false*/);
+ Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
+ for (OCFile file : localFiles) {
+ localFilesMap.put(file.getRemotePath(), file);
+ }
+
+ // loop to synchronize every child
+ OCFile remoteFile = null, localFile = null, updatedFile = null;
+ RemoteFile r;
+ for (int i=1; i<folderAndFiles.size(); i++) {
+ /// new OCFile instance with the data from the server
+ r = (RemoteFile) folderAndFiles.get(i);
+ remoteFile = FileStorageUtils.fillOCFile(r);
+
+ /// new OCFile instance to merge fresh data from server with local state
+ updatedFile = FileStorageUtils.fillOCFile(r);
+ updatedFile.setParentId(mLocalFolder.getFileId());
+
+ /// retrieve local data for the read file
+ // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
+ localFile = localFilesMap.remove(remoteFile.getRemotePath());
+
+ /// add to updatedFile data about LOCAL STATE (not existing in server)
+ updatedFile.setLastSyncDateForProperties(mCurrentSyncTime);
+ if (localFile != null) {
+ updatedFile.setFileId(localFile.getFileId());
+ updatedFile.setFavorite(localFile.isFavorite());
+ updatedFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
+ updatedFile.setModificationTimestampAtLastSyncForData(
+ localFile.getModificationTimestampAtLastSyncForData()
+ );
+ updatedFile.setStoragePath(localFile.getStoragePath());
+ // eTag will not be updated unless file CONTENTS are synchronized
+ updatedFile.setEtag(localFile.getEtag());
+ if (updatedFile.isFolder()) {
+ updatedFile.setFileLength(localFile.getFileLength());
+ // TODO move operations about size of folders to FileContentProvider
+ } else if (mRemoteFolderChanged && remoteFile.isImage() &&
+ remoteFile.getModificationTimestamp() !=
+ localFile.getModificationTimestamp()) {
+ updatedFile.setNeedsUpdateThumbnail(true);
+ Log.d(TAG, "Image " + remoteFile.getFileName() + " updated on the server");
+ }
+ updatedFile.setPublicLink(localFile.getPublicLink());
+ updatedFile.setShareByLink(localFile.isShareByLink());
+ updatedFile.setEtagInConflict(localFile.getEtagInConflict());
+ } else {
+ // remote eTag will not be updated unless file CONTENTS are synchronized
+ updatedFile.setEtag("");
+ }
+
+ /// check and fix, if needed, local storage path
+ searchForLocalFileInDefaultPath(updatedFile);
+
+ /// classify file to sync/download contents later
+ if (remoteFile.isFolder()) {
+ /// to download children files recursively
+ synchronized (mCancellationRequested) {
+ if (mCancellationRequested.get()) {
+ throw new OperationCancelledException();
+ }
+ startSyncFolderOperation(remoteFile.getRemotePath());
+ }
+
+ } else {
+ /// prepare content synchronization for files (any file, not just favorites)
+ SynchronizeFileOperation operation = new SynchronizeFileOperation(
+ localFile,
+ remoteFile,
+ mAccount,
+ true,
+ mContext
+ );
+ mFilesToSyncContents.add(operation);
+
+ }
+
+ updatedFiles.add(updatedFile);
+ }
+
+ // save updated contents in local database
+ storageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
+