2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.operations
;
24 import java
.io
.FileInputStream
;
25 import java
.io
.FileOutputStream
;
26 import java
.io
.IOException
;
27 import java
.io
.InputStream
;
28 import java
.io
.OutputStream
;
29 import java
.util
.ArrayList
;
30 import java
.util
.HashMap
;
31 import java
.util
.List
;
33 import java
.util
.Vector
;
35 import org
.apache
.http
.HttpStatus
;
37 import android
.accounts
.Account
;
38 import android
.content
.Context
;
39 import android
.content
.Intent
;
40 import android
.util
.Log
;
42 import com
.owncloud
.android
.MainApp
;
43 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
44 import com
.owncloud
.android
.datamodel
.OCFile
;
45 import com
.owncloud
.android
.files
.services
.FileDownloader
;
46 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
47 import com
.owncloud
.android
.lib
.common
.operations
.OperationCancelledException
;
48 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
49 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
50 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
51 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
52 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
53 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFileOperation
;
54 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFolderOperation
;
55 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
56 import com
.owncloud
.android
.lib
.resources
.shares
.GetRemoteSharesForFileOperation
;
57 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
58 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
59 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
60 import com
.owncloud
.android
.services
.OperationsService
;
61 import com
.owncloud
.android
.utils
.FileStorageUtils
;
64 import java
.util
.ArrayList
;
65 import java
.util
.HashMap
;
66 import java
.util
.List
;
68 import java
.util
.Vector
;
69 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
71 //import android.support.v4.content.LocalBroadcastManager;
75 * Remote operation performing the synchronization of the list of files contained
76 * in a folder identified with its remote path.
78 * Fetches the list and properties of the files contained in the given folder, including their
79 * properties, and updates the local database with them.
81 * Does NOT enter in the child folders to synchronize their contents also.
83 public class SynchronizeFolderOperation
extends SyncOperation
{
85 private static final String TAG
= SynchronizeFolderOperation
.class.getSimpleName();
87 /** Time stamp for the synchronization process in progress */
88 private long mCurrentSyncTime
;
90 /** Remote path of the folder to synchronize */
91 private String mRemotePath
;
93 /** Account where the file to synchronize belongs */
94 private Account mAccount
;
96 /** Android context; necessary to send requests to the download service */
97 private Context mContext
;
99 /** Locally cached information about folder to synchronize */
100 private OCFile mLocalFolder
;
102 /** Files and folders contained in the synchronized folder after a successful operation */
103 //private List<OCFile> mChildren;
105 /** Counter of conflicts found between local and remote files */
106 private int mConflictsFound
;
108 /** Counter of failed operations in synchronization of kept-in-sync files */
109 private int mFailsInFileSyncsFound
;
111 /** 'True' means that the remote folder changed and should be fetched */
112 private boolean mRemoteFolderChanged
;
114 private List
<OCFile
> mFilesForDirectDownload
;
115 // to avoid extra PROPFINDs when there was no change in the folder
117 private List
<SyncOperation
> mFilesToSyncContentsWithoutUpload
;
118 // this will go out when 'folder synchronization' replaces 'folder download'; step by step
120 private List
<SyncOperation
> mFavouriteFilesToSyncContents
;
121 // this will be used for every file when 'folder synchronization' replaces 'folder download'
123 private final AtomicBoolean mCancellationRequested
;
126 * Creates a new instance of {@link SynchronizeFolderOperation}.
128 * @param context Application context.
129 * @param remotePath Path to synchronize.
130 * @param account ownCloud account where the folder is located.
131 * @param currentSyncTime Time stamp for the synchronization process in progress.
133 public SynchronizeFolderOperation(Context context
, String remotePath
, Account account
,
134 long currentSyncTime
){
135 mRemotePath
= remotePath
;
136 mCurrentSyncTime
= currentSyncTime
;
139 mRemoteFolderChanged
= false
;
140 mFilesForDirectDownload
= new Vector
<OCFile
>();
141 mFilesToSyncContentsWithoutUpload
= new Vector
<SyncOperation
>();
142 mFavouriteFilesToSyncContents
= new Vector
<SyncOperation
>();
143 mCancellationRequested
= new AtomicBoolean(false
);
147 public int getConflictsFound() {
148 return mConflictsFound
;
151 public int getFailsInFileSyncsFound() {
152 return mFailsInFileSyncsFound
;
156 * Performs the synchronization.
161 protected RemoteOperationResult
run(OwnCloudClient client
) {
162 RemoteOperationResult result
= null
;
163 mFailsInFileSyncsFound
= 0;
167 // get locally cached information about folder
168 mLocalFolder
= getStorageManager().getFileByPath(mRemotePath
);
170 result
= checkForChanges(client
);
172 if (result
.isSuccess()) {
173 if (mRemoteFolderChanged
) {
174 result
= fetchAndSyncRemoteFolder(client
);
177 prepareOpsFromLocalKnowledge();
180 if (result
.isSuccess()) {
181 syncContents(client
);
186 if (mCancellationRequested
.get()) {
187 throw new OperationCancelledException();
190 } catch (OperationCancelledException e
) {
191 result
= new RemoteOperationResult(e
);
198 private RemoteOperationResult
checkForChanges(OwnCloudClient client
)
199 throws OperationCancelledException
{
200 Log_OC
.d(TAG
, "Checking changes in " + mAccount
.name
+ mRemotePath
);
202 mRemoteFolderChanged
= true
;
203 RemoteOperationResult result
= null
;
205 if (mCancellationRequested
.get()) {
206 throw new OperationCancelledException();
210 ReadRemoteFileOperation operation
= new ReadRemoteFileOperation(mRemotePath
);
211 result
= operation
.execute(client
);
212 if (result
.isSuccess()){
213 OCFile remoteFolder
= FileStorageUtils
.fillOCFile((RemoteFile
) result
.getData().get(0));
215 // check if remote and local folder are different
216 mRemoteFolderChanged
=
217 !(remoteFolder
.getEtag().equalsIgnoreCase(mLocalFolder
.getEtag()));
219 result
= new RemoteOperationResult(ResultCode
.OK
);
221 Log_OC
.i(TAG
, "Checked " + mAccount
.name
+ mRemotePath
+ " : " +
222 (mRemoteFolderChanged ?
"changed" : "not changed"));
226 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
229 if (result
.isException()) {
230 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ mRemotePath
+ " : " +
231 result
.getLogMessage(), result
.getException());
233 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ mRemotePath
+ " : " +
234 result
.getLogMessage());
243 private RemoteOperationResult
fetchAndSyncRemoteFolder(OwnCloudClient client
)
244 throws OperationCancelledException
{
245 if (mCancellationRequested
.get()) {
246 throw new OperationCancelledException();
249 ReadRemoteFolderOperation operation
= new ReadRemoteFolderOperation(mRemotePath
);
250 RemoteOperationResult result
= operation
.execute(client
);
251 Log_OC
.d(TAG
, "Synchronizing " + mAccount
.name
+ mRemotePath
);
253 if (result
.isSuccess()) {
254 synchronizeData(result
.getData(), client
);
255 if (mConflictsFound
> 0 || mFailsInFileSyncsFound
> 0) {
256 result
= new RemoteOperationResult(ResultCode
.SYNC_CONFLICT
);
257 // should be a different result code, but will do the job
260 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
)
269 private void removeLocalFolder() {
270 FileDataStorageManager storageManager
= getStorageManager();
271 if (storageManager
.fileExists(mLocalFolder
.getFileId())) {
272 String currentSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
273 storageManager
.removeFolder(
276 ( mLocalFolder
.isDown() && // TODO: debug, I think this is
277 // always false for folders
278 mLocalFolder
.getStoragePath().startsWith(currentSavePath
)
286 * Synchronizes the data retrieved from the server about the contents of the target folder
287 * with the current data in the local database.
289 * Grants that mChildren is updated with fresh data after execution.
291 * @param folderAndFiles Remote folder and children files in Folder
293 * @param client Client instance to the remote server where the data were
295 * @return 'True' when any change was made in the local data, 'false' otherwise
297 private void synchronizeData(ArrayList
<Object
> folderAndFiles
, OwnCloudClient client
)
298 throws OperationCancelledException
{
299 FileDataStorageManager storageManager
= getStorageManager();
301 // parse data from remote folder
302 OCFile remoteFolder
= fillOCFile((RemoteFile
)folderAndFiles
.get(0));
303 remoteFolder
.setParentId(mLocalFolder
.getParentId());
304 remoteFolder
.setFileId(mLocalFolder
.getFileId());
306 Log_OC
.d(TAG
, "Remote folder " + mLocalFolder
.getRemotePath()
307 + " changed - starting update of local data ");
309 List
<OCFile
> updatedFiles
= new Vector
<OCFile
>(folderAndFiles
.size() - 1);
310 mFilesForDirectDownload
.clear();
311 mFilesToSyncContentsWithoutUpload
.clear();
312 mFavouriteFilesToSyncContents
.clear();
314 if (mCancellationRequested
.get()) {
315 throw new OperationCancelledException();
318 // get current data about local contents of the folder to synchronize
319 List
<OCFile
> localFiles
= storageManager
.getFolderContent(mLocalFolder
, false
);
320 Map
<String
, OCFile
> localFilesMap
= new HashMap
<String
, OCFile
>(localFiles
.size());
321 for (OCFile file
: localFiles
) {
322 localFilesMap
.put(file
.getRemotePath(), file
);
325 // loop to synchronize every child
326 OCFile remoteFile
= null
, localFile
= null
;
327 for (int i
=1; i
<folderAndFiles
.size(); i
++) {
328 /// new OCFile instance with the data from the server
329 remoteFile
= fillOCFile((RemoteFile
)folderAndFiles
.get(i
));
330 remoteFile
.setParentId(mLocalFolder
.getFileId());
332 /// retrieve local data for the read file
333 // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
334 localFile
= localFilesMap
.remove(remoteFile
.getRemotePath());
336 /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in server)
337 remoteFile
.setLastSyncDateForProperties(mCurrentSyncTime
);
338 if (localFile
!= null
) {
339 // some properties of local state are kept unmodified
340 remoteFile
.setFileId(localFile
.getFileId());
341 remoteFile
.setKeepInSync(localFile
.keepInSync());
342 remoteFile
.setLastSyncDateForData(localFile
.getLastSyncDateForData());
343 remoteFile
.setModificationTimestampAtLastSyncForData(
344 localFile
.getModificationTimestampAtLastSyncForData()
346 remoteFile
.setStoragePath(localFile
.getStoragePath());
347 // eTag will not be updated unless contents are synchronized
348 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
349 remoteFile
.setEtag(localFile
.getEtag());
350 if (remoteFile
.isFolder()) {
351 remoteFile
.setFileLength(localFile
.getFileLength());
352 // TODO move operations about size of folders to FileContentProvider
353 } else if (mRemoteFolderChanged
&& remoteFile
.isImage() &&
354 remoteFile
.getModificationTimestamp() !=
355 localFile
.getModificationTimestamp()) {
356 remoteFile
.setNeedsUpdateThumbnail(true
);
357 Log
.d(TAG
, "Image " + remoteFile
.getFileName() + " updated on the server");
359 remoteFile
.setPublicLink(localFile
.getPublicLink());
360 remoteFile
.setShareByLink(localFile
.isShareByLink());
362 // remote eTag will not be updated unless contents are synchronized
363 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
364 remoteFile
.setEtag("");
367 /// check and fix, if needed, local storage path
368 searchForLocalFileInDefaultPath(remoteFile
);
370 /// classify file to sync/download contents later
371 if (remoteFile
.isFolder()) {
372 /// to download children files recursively
373 synchronized(mCancellationRequested
) {
374 if (mCancellationRequested
.get()) {
375 throw new OperationCancelledException();
377 startSyncFolderOperation(remoteFile
.getRemotePath());
380 } else if (remoteFile
.keepInSync()) {
381 /// prepare content synchronization for kept-in-sync files
382 SynchronizeFileOperation operation
= new SynchronizeFileOperation(
389 mFavouriteFilesToSyncContents
.add(operation
);
392 /// prepare limited synchronization for regular files
393 SynchronizeFileOperation operation
= new SynchronizeFileOperation(
401 mFilesToSyncContentsWithoutUpload
.add(operation
);
404 updatedFiles
.add(remoteFile
);
407 // save updated contents in local database
408 storageManager
.saveFolder(remoteFolder
, updatedFiles
, localFilesMap
.values());
413 private void prepareOpsFromLocalKnowledge() throws OperationCancelledException
{
414 // TODO TOBI ist das richtig?
415 List
<OCFile
> children
= getStorageManager().getFolderContent(mLocalFolder
, false
);
416 for (OCFile child
: children
) {
417 /// classify file to sync/download contents later
418 if (child
.isFolder()) {
419 /// to download children files recursively
420 synchronized(mCancellationRequested
) {
421 if (mCancellationRequested
.get()) {
422 throw new OperationCancelledException();
424 startSyncFolderOperation(child
.getRemotePath());
428 /// prepare limited synchronization for regular files
429 if (!child
.isDown()) {
430 mFilesForDirectDownload
.add(child
);
437 private void syncContents(OwnCloudClient client
) throws OperationCancelledException
{
438 startDirectDownloads();
439 startContentSynchronizations(mFilesToSyncContentsWithoutUpload
, client
);
440 startContentSynchronizations(mFavouriteFilesToSyncContents
, client
);
444 private void startDirectDownloads() throws OperationCancelledException
{
445 for (OCFile file
: mFilesForDirectDownload
) {
446 synchronized(mCancellationRequested
) {
447 if (mCancellationRequested
.get()) {
448 throw new OperationCancelledException();
450 Intent i
= new Intent(mContext
, FileDownloader
.class);
451 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
452 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
453 mContext
.startService(i
);
459 * Performs a list of synchronization operations, determining if a download or upload is needed
460 * or if exists conflict due to changes both in local and remote contents of the each file.
462 * If download or upload is needed, request the operation to the corresponding service and goes
465 * @param filesToSyncContents Synchronization operations to execute.
466 * @param client Interface to the remote ownCloud server.
468 private void startContentSynchronizations(List
<SyncOperation
> filesToSyncContents
,
469 OwnCloudClient client
)
470 throws OperationCancelledException
{
472 Log_OC
.v(TAG
, "Starting content synchronization... ");
473 RemoteOperationResult contentsResult
= null
;
474 for (SyncOperation op
: filesToSyncContents
) {
475 if (mCancellationRequested
.get()) {
476 throw new OperationCancelledException();
478 contentsResult
= op
.execute(getStorageManager(), mContext
);
479 if (!contentsResult
.isSuccess()) {
480 if (contentsResult
.getCode() == ResultCode
.SYNC_CONFLICT
) {
483 mFailsInFileSyncsFound
++;
484 if (contentsResult
.getException() != null
) {
485 Log_OC
.e(TAG
, "Error while synchronizing file : "
486 + contentsResult
.getLogMessage(), contentsResult
.getException());
488 Log_OC
.e(TAG
, "Error while synchronizing file : "
489 + contentsResult
.getLogMessage());
492 // TODO - use the errors count in notifications
493 } // won't let these fails break the synchronization process
499 * Creates and populates a new {@link com.owncloud.android.datamodel.OCFile}
500 * object with the data read from the server.
502 * @param remote remote file read from the server (remote file or folder).
503 * @return New OCFile instance representing the remote resource described by we.
505 private OCFile
fillOCFile(RemoteFile remote
) {
506 OCFile file
= new OCFile(remote
.getRemotePath());
507 file
.setCreationTimestamp(remote
.getCreationTimestamp());
508 file
.setFileLength(remote
.getLength());
509 file
.setMimetype(remote
.getMimeType());
510 file
.setModificationTimestamp(remote
.getModifiedTimestamp());
511 file
.setEtag(remote
.getEtag());
512 file
.setPermissions(remote
.getPermissions());
513 file
.setRemoteId(remote
.getRemoteId());
519 * Scans the default location for saving local copies of files searching for
520 * a 'lost' file with the same full name as the {@link com.owncloud.android.datamodel.OCFile}
521 * received as parameter.
523 * @param file File to associate a possible 'lost' local file.
525 private void searchForLocalFileInDefaultPath(OCFile file
) {
526 if (file
.getStoragePath() == null
&& !file
.isFolder()) {
527 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
529 file
.setStoragePath(f
.getAbsolutePath());
530 file
.setLastSyncDateForData(f
.lastModified());
539 public void cancel() {
540 mCancellationRequested
.set(true
);
543 public String
getFolderPath() {
544 String path
= mLocalFolder
.getStoragePath();
545 if (path
!= null
&& path
.length() > 0) {
548 return FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mLocalFolder
);
551 private void startSyncFolderOperation(String path
){
552 Intent intent
= new Intent(mContext
, OperationsService
.class);
553 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
554 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mAccount
);
555 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, path
);
556 mContext
.startService(intent
);
559 public String
getRemotePath() {