|
@@ -20,26 +20,17 @@
|
|
|
|
|
|
package com.owncloud.android.operations;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Vector;
|
|
|
|
|
|
-import org.apache.http.HttpStatus;
|
|
|
import android.accounts.Account;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.util.Log;
|
|
|
-//import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
|
|
-import com.owncloud.android.MainApp;
|
|
|
import com.owncloud.android.datamodel.FileDataStorageManager;
|
|
|
import com.owncloud.android.datamodel.OCFile;
|
|
|
|
|
@@ -50,7 +41,6 @@ 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.resources.shares.GetRemoteSharesForFileOperation;
|
|
|
-import com.owncloud.android.lib.resources.files.FileUtils;
|
|
|
import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
|
|
|
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
|
|
|
import com.owncloud.android.lib.resources.files.RemoteFile;
|
|
@@ -120,7 +110,10 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
/** 'True' means that Etag will be ignored */
|
|
|
private boolean mIgnoreETag;
|
|
|
|
|
|
-
|
|
|
+ private List<SynchronizeFileOperation> mFilesToSyncContents;
|
|
|
+ // this will be used for every file when 'folder synchronization' replaces 'folder download'
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* Creates a new instance of {@link RefreshFolderOperation}.
|
|
|
*
|
|
@@ -154,6 +147,7 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
mForgottenLocalFiles = new HashMap<String, String>();
|
|
|
mRemoteFolderChanged = false;
|
|
|
mIgnoreETag = ignoreETag;
|
|
|
+ mFilesToSyncContents = new Vector<SynchronizeFileOperation>();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -191,7 +185,7 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
mConflictsFound = 0;
|
|
|
mForgottenLocalFiles.clear();
|
|
|
|
|
|
- if (FileUtils.PATH_SEPARATOR.equals(mLocalFolder.getRemotePath()) && !mSyncFullAccount) {
|
|
|
+ if (OCFile.ROOT_PATH.equals(mLocalFolder.getRemotePath()) && !mSyncFullAccount) {
|
|
|
updateOCVersion(client);
|
|
|
}
|
|
|
|
|
@@ -201,9 +195,14 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
if (mRemoteFolderChanged) {
|
|
|
result = fetchAndSyncRemoteFolder(client);
|
|
|
} else {
|
|
|
- // TODO Enable when "On Device" is recovered ?
|
|
|
+ fetchFavoritesToSyncFromLocalData();
|
|
|
mChildren = mStorageManager.getFolderContent(mLocalFolder/*, false*/);
|
|
|
}
|
|
|
+
|
|
|
+ if (result.isSuccess()) {
|
|
|
+ // request for the synchronization of KEPT-IN-SYNC file contents
|
|
|
+ startContentSynchronizations(mFilesToSyncContents, client);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (!mSyncFullAccount) {
|
|
@@ -239,9 +238,8 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
private RemoteOperationResult checkForChanges(OwnCloudClient client) {
|
|
|
mRemoteFolderChanged = true;
|
|
|
RemoteOperationResult result = null;
|
|
|
- String remotePath = null;
|
|
|
+ String remotePath = mLocalFolder.getRemotePath();
|
|
|
|
|
|
- remotePath = mLocalFolder.getRemotePath();
|
|
|
Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath);
|
|
|
|
|
|
// remote request
|
|
@@ -264,7 +262,7 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
|
|
|
result = new RemoteOperationResult(ResultCode.OK);
|
|
|
|
|
|
- Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " +
|
|
|
+ Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " +
|
|
|
(mRemoteFolderChanged ? "changed" : "not changed"));
|
|
|
|
|
|
} else {
|
|
@@ -337,15 +335,15 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath());
|
|
|
|
|
|
// parse data from remote folder
|
|
|
- OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0));
|
|
|
+ OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) folderAndFiles.get(0));
|
|
|
remoteFolder.setParentId(mLocalFolder.getParentId());
|
|
|
remoteFolder.setFileId(mLocalFolder.getFileId());
|
|
|
|
|
|
- Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath()
|
|
|
+ Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath()
|
|
|
+ " changed - starting update of local data ");
|
|
|
|
|
|
List<OCFile> updatedFiles = new Vector<OCFile>(folderAndFiles.size() - 1);
|
|
|
- List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
|
|
|
+ mFilesToSyncContents.clear();
|
|
|
|
|
|
// get current data about local contents of the folder to synchronize
|
|
|
// TODO Enable when "On Device" is recovered ?
|
|
@@ -359,7 +357,7 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
OCFile remoteFile = null, localFile = null;
|
|
|
for (int i=1; i<folderAndFiles.size(); i++) {
|
|
|
/// new OCFile instance with the data from the server
|
|
|
- remoteFile = fillOCFile((RemoteFile)folderAndFiles.get(i));
|
|
|
+ remoteFile = FileStorageUtils.fillOCFile((RemoteFile) folderAndFiles.get(i));
|
|
|
remoteFile.setParentId(mLocalFolder.getFileId());
|
|
|
|
|
|
/// retrieve local data for the read file
|
|
@@ -377,9 +375,8 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
localFile.getModificationTimestampAtLastSyncForData()
|
|
|
);
|
|
|
remoteFile.setStoragePath(localFile.getStoragePath());
|
|
|
- // eTag will not be updated unless contents are synchronized
|
|
|
- // (Synchronize[File|Folder]Operation with remoteFile as parameter)
|
|
|
- remoteFile.setEtag(localFile.getEtag());
|
|
|
+ // eTag will not be updated unless file CONTENTS are synchronized
|
|
|
+ remoteFile.setEtag(localFile.getEtag());
|
|
|
if (remoteFile.isFolder()) {
|
|
|
remoteFile.setFileLength(localFile.getFileLength());
|
|
|
// TODO move operations about size of folders to FileContentProvider
|
|
@@ -392,15 +389,12 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
remoteFile.setPublicLink(localFile.getPublicLink());
|
|
|
remoteFile.setShareByLink(localFile.isShareByLink());
|
|
|
} else {
|
|
|
- // remote eTag will not be updated unless contents are synchronized
|
|
|
- // (Synchronize[File|Folder]Operation with remoteFile as parameter)
|
|
|
- remoteFile.setEtag("");
|
|
|
+ // remote eTag will not be updated unless file CONTENTS are synchronized
|
|
|
+ remoteFile.setEtag("");
|
|
|
}
|
|
|
|
|
|
/// check and fix, if needed, local storage path
|
|
|
- checkAndFixForeignStoragePath(remoteFile); // policy - local files are COPIED
|
|
|
- // into the ownCloud local folder;
|
|
|
- searchForLocalFileInDefaultPath(remoteFile); // legacy
|
|
|
+ FileStorageUtils.searchForLocalFileInDefaultPath(remoteFile, mAccount);
|
|
|
|
|
|
/// prepare content synchronization for kept-in-sync files
|
|
|
if (remoteFile.isFavorite()) {
|
|
@@ -411,18 +405,15 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
mContext
|
|
|
);
|
|
|
|
|
|
- filesToSyncContents.add(operation);
|
|
|
+ mFilesToSyncContents.add(operation);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
updatedFiles.add(remoteFile);
|
|
|
}
|
|
|
|
|
|
// save updated contents in local database
|
|
|
mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
|
|
|
|
|
|
- // request for the synchronization of file contents AFTER saving current remote properties
|
|
|
- startContentSynchronizations(filesToSyncContents, client);
|
|
|
-
|
|
|
mChildren = updatedFiles;
|
|
|
}
|
|
|
|
|
@@ -460,97 +451,6 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public boolean isMultiStatus(int status) {
|
|
|
- return (status == HttpStatus.SC_MULTI_STATUS);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Creates and populates a new {@link OCFile} object with the data read from the server.
|
|
|
- *
|
|
|
- * @param remote remote file read from the server (remote file or folder).
|
|
|
- * @return New OCFile instance representing the remote resource described by we.
|
|
|
- */
|
|
|
- private OCFile fillOCFile(RemoteFile remote) {
|
|
|
- OCFile file = new OCFile(remote.getRemotePath());
|
|
|
- file.setCreationTimestamp(remote.getCreationTimestamp());
|
|
|
- file.setFileLength(remote.getLength());
|
|
|
- file.setMimetype(remote.getMimeType());
|
|
|
- file.setModificationTimestamp(remote.getModifiedTimestamp());
|
|
|
- file.setEtag(remote.getEtag());
|
|
|
- file.setPermissions(remote.getPermissions());
|
|
|
- file.setRemoteId(remote.getRemoteId());
|
|
|
- return file;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * Checks the storage path of the OCFile received as parameter.
|
|
|
- * If it's out of the local ownCloud folder, tries to copy the file inside it.
|
|
|
- *
|
|
|
- * If the copy fails, the link to the local file is nullified. The account of forgotten
|
|
|
- * files is kept in {@link #mForgottenLocalFiles}
|
|
|
- *)
|
|
|
- * @param file File to check and fix.
|
|
|
- */
|
|
|
- private void checkAndFixForeignStoragePath(OCFile file) {
|
|
|
- String storagePath = file.getStoragePath();
|
|
|
- String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
|
|
|
- if (storagePath != null && !storagePath.equals(expectedPath)) {
|
|
|
- /// fix storagePaths out of the local ownCloud folder
|
|
|
- File originalFile = new File(storagePath);
|
|
|
- if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) {
|
|
|
- mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
|
|
|
- file.setStoragePath(null);
|
|
|
-
|
|
|
- } else {
|
|
|
- InputStream in = null;
|
|
|
- OutputStream out = null;
|
|
|
- try {
|
|
|
- File expectedFile = new File(expectedPath);
|
|
|
- File expectedParent = expectedFile.getParentFile();
|
|
|
- expectedParent.mkdirs();
|
|
|
- if (!expectedParent.isDirectory()) {
|
|
|
- throw new IOException(
|
|
|
- "Unexpected error: parent directory could not be created"
|
|
|
- );
|
|
|
- }
|
|
|
- expectedFile.createNewFile();
|
|
|
- if (!expectedFile.isFile()) {
|
|
|
- throw new IOException("Unexpected error: target file could not be created");
|
|
|
- }
|
|
|
- in = new FileInputStream(originalFile);
|
|
|
- out = new FileOutputStream(expectedFile);
|
|
|
- byte[] buf = new byte[1024];
|
|
|
- int len;
|
|
|
- while ((len = in.read(buf)) > 0){
|
|
|
- out.write(buf, 0, len);
|
|
|
- }
|
|
|
- file.setStoragePath(expectedPath);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- Log_OC.e(TAG, "Exception while copying foreign file " + expectedPath, e);
|
|
|
- mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
|
|
|
- file.setStoragePath(null);
|
|
|
-
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (in != null) in.close();
|
|
|
- } catch (Exception e) {
|
|
|
- Log_OC.d(TAG, "Weird exception while closing input stream for "
|
|
|
- + storagePath + " (ignoring)", e);
|
|
|
- }
|
|
|
- try {
|
|
|
- if (out != null) out.close();
|
|
|
- } catch (Exception e) {
|
|
|
- Log_OC.d(TAG, "Weird exception while closing output stream for "
|
|
|
- + expectedPath + " (ignoring)", e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
private RemoteOperationResult refreshSharesForFolder(OwnCloudClient client) {
|
|
|
RemoteOperationResult result = null;
|
|
|
|
|
@@ -572,24 +472,6 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * Scans the default location for saving local copies of files searching for
|
|
|
- * a 'lost' file with the same full name as the {@link OCFile} received as
|
|
|
- * parameter.
|
|
|
- *
|
|
|
- * @param file File to associate a possible 'lost' local file.
|
|
|
- */
|
|
|
- private void searchForLocalFileInDefaultPath(OCFile file) {
|
|
|
- if (file.getStoragePath() == null && !file.isFolder()) {
|
|
|
- File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
|
|
|
- if (f.exists()) {
|
|
|
- file.setStoragePath(f.getAbsolutePath());
|
|
|
- file.setLastSyncDateForData(f.lastModified());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* Sends a message to any application component interested in the progress
|
|
|
* of the synchronization.
|
|
@@ -614,8 +496,20 @@ public class RefreshFolderOperation extends RemoteOperation {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public boolean getRemoteFolderChanged() {
|
|
|
- return mRemoteFolderChanged;
|
|
|
+ private void fetchFavoritesToSyncFromLocalData() {
|
|
|
+ List<OCFile> children = mStorageManager.getFolderContent(mLocalFolder);
|
|
|
+ for (OCFile child : children) {
|
|
|
+ if (!child.isFolder() && child.isFavorite()) {
|
|
|
+ SynchronizeFileOperation operation = new SynchronizeFileOperation(
|
|
|
+ child,
|
|
|
+ child, // cheating with the remote file to get an update to server; to refactor
|
|
|
+ mAccount,
|
|
|
+ true,
|
|
|
+ mContext
|
|
|
+ );
|
|
|
+ mFilesToSyncContents.add(operation);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|