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
;
36 import android
.accounts
.Account
;
37 import android
.content
.Context
;
38 import android
.content
.Intent
;
39 import android
.util
.Log
;
40 //import android.support.v4.content.LocalBroadcastManager;
42 import com
.owncloud
.android
.MainApp
;
43 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
44 import com
.owncloud
.android
.datamodel
.OCFile
;
46 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
47 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
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
.shares
.GetRemoteSharesForFileOperation
;
53 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
54 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFileOperation
;
55 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFolderOperation
;
56 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
58 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
59 import com
.owncloud
.android
.utils
.FileStorageUtils
;
64 * Remote operation performing the synchronization of the list of files contained
65 * in a folder identified with its remote path.
67 * Fetches the list and properties of the files contained in the given folder, including their
68 * properties, and updates the local database with them.
70 * Does NOT enter in the child folders to synchronize their contents also.
72 public class RefreshFolderOperation
extends RemoteOperation
{
74 private static final String TAG
= RefreshFolderOperation
.class.getSimpleName();
76 public static final String EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
=
77 RefreshFolderOperation
.class.getName() + ".EVENT_SINGLE_FOLDER_CONTENTS_SYNCED";
78 public static final String EVENT_SINGLE_FOLDER_SHARES_SYNCED
=
79 RefreshFolderOperation
.class.getName() + ".EVENT_SINGLE_FOLDER_SHARES_SYNCED";
81 /** Time stamp for the synchronization process in progress */
82 private long mCurrentSyncTime
;
84 /** Remote folder to synchronize */
85 private OCFile mLocalFolder
;
87 /** Access to the local database */
88 private FileDataStorageManager mStorageManager
;
90 /** Account where the file to synchronize belongs */
91 private Account mAccount
;
93 /** Android context; necessary to send requests to the download service */
94 private Context mContext
;
96 /** Files and folders contained in the synchronized folder after a successful operation */
97 private List
<OCFile
> mChildren
;
99 /** Counter of conflicts found between local and remote files */
100 private int mConflictsFound
;
102 /** Counter of failed operations in synchronization of kept-in-sync files */
103 private int mFailsInFavouritesFound
;
106 * Map of remote and local paths to files that where locally stored in a location
107 * out of the ownCloud folder and couldn't be copied automatically into it
109 private Map
<String
, String
> mForgottenLocalFiles
;
111 /** 'True' means that this operation is part of a full account synchronization */
112 private boolean mSyncFullAccount
;
114 /** 'True' means that Share resources bound to the files into should be refreshed also */
115 private boolean mIsShareSupported
;
117 /** 'True' means that the remote folder changed and should be fetched */
118 private boolean mRemoteFolderChanged
;
120 /** 'True' means that Etag will be ignored */
121 private boolean mIgnoreETag
;
125 * Creates a new instance of {@link RefreshFolderOperation}.
127 * @param folder Folder to synchronize.
128 * @param currentSyncTime Time stamp for the synchronization process in progress.
129 * @param syncFullAccount 'True' means that this operation is part of a full account
131 * @param isShareSupported 'True' means that the server supports the sharing API.
132 * @param ignoreETag 'True' means that the content of the remote folder should
133 * be fetched and updated even though the 'eTag' did not
135 * @param dataStorageManager Interface with the local database.
136 * @param account ownCloud account where the folder is located.
137 * @param context Application context.
139 public RefreshFolderOperation(OCFile folder
,
140 long currentSyncTime
,
141 boolean syncFullAccount
,
142 boolean isShareSupported
,
144 FileDataStorageManager dataStorageManager
,
147 mLocalFolder
= folder
;
148 mCurrentSyncTime
= currentSyncTime
;
149 mSyncFullAccount
= syncFullAccount
;
150 mIsShareSupported
= isShareSupported
;
151 mStorageManager
= dataStorageManager
;
154 mForgottenLocalFiles
= new HashMap
<String
, String
>();
155 mRemoteFolderChanged
= false
;
156 mIgnoreETag
= ignoreETag
;
160 public int getConflictsFound() {
161 return mConflictsFound
;
164 public int getFailsInFavouritesFound() {
165 return mFailsInFavouritesFound
;
168 public Map
<String
, String
> getForgottenLocalFiles() {
169 return mForgottenLocalFiles
;
173 * Returns the list of files and folders contained in the synchronized folder,
174 * if called after synchronization is complete.
176 * @return List of files and folders contained in the synchronized folder.
178 public List
<OCFile
> getChildren() {
183 * Performs the synchronization.
188 protected RemoteOperationResult
run(OwnCloudClient client
) {
189 RemoteOperationResult result
= null
;
190 mFailsInFavouritesFound
= 0;
192 mForgottenLocalFiles
.clear();
194 if (FileUtils
.PATH_SEPARATOR
.equals(mLocalFolder
.getRemotePath()) && !mSyncFullAccount
) {
195 updateOCVersion(client
);
198 result
= checkForChanges(client
);
200 if (result
.isSuccess()) {
201 if (mRemoteFolderChanged
) {
202 result
= fetchAndSyncRemoteFolder(client
);
204 // TODO Enable when "On Device" is recovered ?
205 mChildren
= mStorageManager
.getFolderContent(mLocalFolder
/*, false*/);
209 if (!mSyncFullAccount
) {
211 EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
, mLocalFolder
.getRemotePath(), result
215 if (result
.isSuccess() && mIsShareSupported
&& !mSyncFullAccount
) {
216 refreshSharesForFolder(client
); // share result is ignored
219 if (!mSyncFullAccount
) {
221 EVENT_SINGLE_FOLDER_SHARES_SYNCED
, mLocalFolder
.getRemotePath(), result
230 private void updateOCVersion(OwnCloudClient client
) {
231 UpdateOCVersionOperation update
= new UpdateOCVersionOperation(mAccount
, mContext
);
232 RemoteOperationResult result
= update
.execute(client
);
233 if (result
.isSuccess()) {
234 mIsShareSupported
= update
.getOCVersion().isSharedSupported();
239 private RemoteOperationResult
checkForChanges(OwnCloudClient client
) {
240 mRemoteFolderChanged
= true
;
241 RemoteOperationResult result
= null
;
242 String remotePath
= null
;
244 remotePath
= mLocalFolder
.getRemotePath();
245 Log_OC
.d(TAG
, "Checking changes in " + mAccount
.name
+ remotePath
);
248 ReadRemoteFileOperation operation
= new ReadRemoteFileOperation(remotePath
);
249 result
= operation
.execute(client
);
250 if (result
.isSuccess()){
251 OCFile remoteFolder
= FileStorageUtils
.fillOCFile((RemoteFile
) result
.getData().get(0));
254 // check if remote and local folder are different
255 String remoteFolderETag
= remoteFolder
.getEtag();
256 if (remoteFolderETag
!= null
) {
257 mRemoteFolderChanged
=
258 !(remoteFolderETag
.equalsIgnoreCase(mLocalFolder
.getEtag()));
260 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
261 "No ETag received from server");
265 result
= new RemoteOperationResult(ResultCode
.OK
);
267 Log_OC
.i(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
268 (mRemoteFolderChanged ?
"changed" : "not changed"));
272 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
275 if (result
.isException()) {
276 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
277 result
.getLogMessage(), result
.getException());
279 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
280 result
.getLogMessage());
288 private RemoteOperationResult
fetchAndSyncRemoteFolder(OwnCloudClient client
) {
289 String remotePath
= mLocalFolder
.getRemotePath();
290 ReadRemoteFolderOperation operation
= new ReadRemoteFolderOperation(remotePath
);
291 RemoteOperationResult result
= operation
.execute(client
);
292 Log_OC
.d(TAG
, "Synchronizing " + mAccount
.name
+ remotePath
);
294 if (result
.isSuccess()) {
295 synchronizeData(result
.getData(), client
);
296 if (mConflictsFound
> 0 || mFailsInFavouritesFound
> 0) {
297 result
= new RemoteOperationResult(ResultCode
.SYNC_CONFLICT
);
298 // should be a different result code, but will do the job
301 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
)
309 private void removeLocalFolder() {
310 if (mStorageManager
.fileExists(mLocalFolder
.getFileId())) {
311 String currentSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
312 mStorageManager
.removeFolder(
315 ( mLocalFolder
.isDown() &&
316 mLocalFolder
.getStoragePath().startsWith(currentSavePath
)
324 * Synchronizes the data retrieved from the server about the contents of the target folder
325 * with the current data in the local database.
327 * Grants that mChildren is updated with fresh data after execution.
329 * @param folderAndFiles Remote folder and children files in Folder
331 * @param client Client instance to the remote server where the data were
333 * @return 'True' when any change was made in the local data, 'false' otherwise
335 private void synchronizeData(ArrayList
<Object
> folderAndFiles
, OwnCloudClient client
) {
336 // get 'fresh data' from the database
337 mLocalFolder
= mStorageManager
.getFileByPath(mLocalFolder
.getRemotePath());
339 // parse data from remote folder
340 OCFile remoteFolder
= fillOCFile((RemoteFile
)folderAndFiles
.get(0));
341 remoteFolder
.setParentId(mLocalFolder
.getParentId());
342 remoteFolder
.setFileId(mLocalFolder
.getFileId());
344 Log_OC
.d(TAG
, "Remote folder " + mLocalFolder
.getRemotePath()
345 + " changed - starting update of local data ");
347 List
<OCFile
> updatedFiles
= new Vector
<OCFile
>(folderAndFiles
.size() - 1);
348 List
<SynchronizeFileOperation
> filesToSyncContents
= new Vector
<SynchronizeFileOperation
>();
350 // get current data about local contents of the folder to synchronize
351 // TODO Enable when "On Device" is recovered ?
352 List
<OCFile
> localFiles
= mStorageManager
.getFolderContent(mLocalFolder
/*, false*/);
353 Map
<String
, OCFile
> localFilesMap
= new HashMap
<String
, OCFile
>(localFiles
.size());
354 for (OCFile file
: localFiles
) {
355 localFilesMap
.put(file
.getRemotePath(), file
);
358 // loop to update every child
359 OCFile remoteFile
= null
, localFile
= null
;
360 for (int i
=1; i
<folderAndFiles
.size(); i
++) {
361 /// new OCFile instance with the data from the server
362 remoteFile
= fillOCFile((RemoteFile
)folderAndFiles
.get(i
));
363 remoteFile
.setParentId(mLocalFolder
.getFileId());
365 /// retrieve local data for the read file
366 // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
367 localFile
= localFilesMap
.remove(remoteFile
.getRemotePath());
369 /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in server)
370 remoteFile
.setLastSyncDateForProperties(mCurrentSyncTime
);
371 if (localFile
!= null
) {
372 // some properties of local state are kept unmodified
373 remoteFile
.setFileId(localFile
.getFileId());
374 remoteFile
.setFavorite(localFile
.isFavorite());
375 remoteFile
.setLastSyncDateForData(localFile
.getLastSyncDateForData());
376 remoteFile
.setModificationTimestampAtLastSyncForData(
377 localFile
.getModificationTimestampAtLastSyncForData()
379 remoteFile
.setStoragePath(localFile
.getStoragePath());
380 // eTag will not be updated unless contents are synchronized
381 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
382 remoteFile
.setEtag(localFile
.getEtag());
383 if (remoteFile
.isFolder()) {
384 remoteFile
.setFileLength(localFile
.getFileLength());
385 // TODO move operations about size of folders to FileContentProvider
386 } else if (mRemoteFolderChanged
&& remoteFile
.isImage() &&
387 remoteFile
.getModificationTimestamp() !=
388 localFile
.getModificationTimestamp()) {
389 remoteFile
.setNeedsUpdateThumbnail(true
);
390 Log
.d(TAG
, "Image " + remoteFile
.getFileName() + " updated on the server");
392 remoteFile
.setPublicLink(localFile
.getPublicLink());
393 remoteFile
.setShareByLink(localFile
.isShareByLink());
395 // remote eTag will not be updated unless contents are synchronized
396 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
397 remoteFile
.setEtag("");
400 /// check and fix, if needed, local storage path
401 checkAndFixForeignStoragePath(remoteFile
); // policy - local files are COPIED
402 // into the ownCloud local folder;
403 searchForLocalFileInDefaultPath(remoteFile
); // legacy
405 /// prepare content synchronization for kept-in-sync files
406 if (remoteFile
.isFavorite()) {
407 SynchronizeFileOperation operation
= new SynchronizeFileOperation( localFile
,
414 filesToSyncContents
.add(operation
);
417 updatedFiles
.add(remoteFile
);
420 // save updated contents in local database
421 mStorageManager
.saveFolder(remoteFolder
, updatedFiles
, localFilesMap
.values());
423 // request for the synchronization of file contents AFTER saving current remote properties
424 startContentSynchronizations(filesToSyncContents
, client
);
426 mChildren
= updatedFiles
;
430 * Performs a list of synchronization operations, determining if a download or upload is needed
431 * or if exists conflict due to changes both in local and remote contents of the each file.
433 * If download or upload is needed, request the operation to the corresponding service and goes
436 * @param filesToSyncContents Synchronization operations to execute.
437 * @param client Interface to the remote ownCloud server.
439 private void startContentSynchronizations(
440 List
<SynchronizeFileOperation
> filesToSyncContents
, OwnCloudClient client
442 RemoteOperationResult contentsResult
= null
;
443 for (SynchronizeFileOperation op
: filesToSyncContents
) {
444 contentsResult
= op
.execute(mStorageManager
, mContext
); // async
445 if (!contentsResult
.isSuccess()) {
446 if (contentsResult
.getCode() == ResultCode
.SYNC_CONFLICT
) {
449 mFailsInFavouritesFound
++;
450 if (contentsResult
.getException() != null
) {
451 Log_OC
.e(TAG
, "Error while synchronizing favourites : "
452 + contentsResult
.getLogMessage(), contentsResult
.getException());
454 Log_OC
.e(TAG
, "Error while synchronizing favourites : "
455 + contentsResult
.getLogMessage());
458 } // won't let these fails break the synchronization process
463 public boolean isMultiStatus(int status
) {
464 return (status
== HttpStatus
.SC_MULTI_STATUS
);
468 * Creates and populates a new {@link OCFile} object with the data read from the server.
470 * @param remote remote file read from the server (remote file or folder).
471 * @return New OCFile instance representing the remote resource described by we.
473 private OCFile
fillOCFile(RemoteFile remote
) {
474 OCFile file
= new OCFile(remote
.getRemotePath());
475 file
.setCreationTimestamp(remote
.getCreationTimestamp());
476 file
.setFileLength(remote
.getLength());
477 file
.setMimetype(remote
.getMimeType());
478 file
.setModificationTimestamp(remote
.getModifiedTimestamp());
479 file
.setEtag(remote
.getEtag());
480 file
.setPermissions(remote
.getPermissions());
481 file
.setRemoteId(remote
.getRemoteId());
487 * Checks the storage path of the OCFile received as parameter.
488 * If it's out of the local ownCloud folder, tries to copy the file inside it.
490 * If the copy fails, the link to the local file is nullified. The account of forgotten
491 * files is kept in {@link #mForgottenLocalFiles}
493 * @param file File to check and fix.
495 private void checkAndFixForeignStoragePath(OCFile file
) {
496 String storagePath
= file
.getStoragePath();
497 String expectedPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
498 if (storagePath
!= null
&& !storagePath
.equals(expectedPath
)) {
499 /// fix storagePaths out of the local ownCloud folder
500 File originalFile
= new File(storagePath
);
501 if (FileStorageUtils
.getUsableSpace(mAccount
.name
) < originalFile
.length()) {
502 mForgottenLocalFiles
.put(file
.getRemotePath(), storagePath
);
503 file
.setStoragePath(null
);
506 InputStream
in = null
;
507 OutputStream out
= null
;
509 File expectedFile
= new File(expectedPath
);
510 File expectedParent
= expectedFile
.getParentFile();
511 expectedParent
.mkdirs();
512 if (!expectedParent
.isDirectory()) {
513 throw new IOException(
514 "Unexpected error: parent directory could not be created"
517 expectedFile
.createNewFile();
518 if (!expectedFile
.isFile()) {
519 throw new IOException("Unexpected error: target file could not be created");
521 in = new FileInputStream(originalFile
);
522 out
= new FileOutputStream(expectedFile
);
523 byte[] buf
= new byte[1024];
525 while ((len
= in.read(buf
)) > 0){
526 out
.write(buf
, 0, len
);
528 file
.setStoragePath(expectedPath
);
530 } catch (Exception e
) {
531 Log_OC
.e(TAG
, "Exception while copying foreign file " + expectedPath
, e
);
532 mForgottenLocalFiles
.put(file
.getRemotePath(), storagePath
);
533 file
.setStoragePath(null
);
537 if (in != null
) in.close();
538 } catch (Exception e
) {
539 Log_OC
.d(TAG
, "Weird exception while closing input stream for "
540 + storagePath
+ " (ignoring)", e
);
543 if (out
!= null
) out
.close();
544 } catch (Exception e
) {
545 Log_OC
.d(TAG
, "Weird exception while closing output stream for "
546 + expectedPath
+ " (ignoring)", e
);
554 private RemoteOperationResult
refreshSharesForFolder(OwnCloudClient client
) {
555 RemoteOperationResult result
= null
;
558 GetRemoteSharesForFileOperation operation
=
559 new GetRemoteSharesForFileOperation(mLocalFolder
.getRemotePath(), false
, true
);
560 result
= operation
.execute(client
);
562 if (result
.isSuccess()) {
563 // update local database
564 ArrayList
<OCShare
> shares
= new ArrayList
<OCShare
>();
565 for(Object obj
: result
.getData()) {
566 shares
.add((OCShare
) obj
);
568 mStorageManager
.saveSharesInFolder(shares
, mLocalFolder
);
576 * Scans the default location for saving local copies of files searching for
577 * a 'lost' file with the same full name as the {@link OCFile} received as
580 * @param file File to associate a possible 'lost' local file.
582 private void searchForLocalFileInDefaultPath(OCFile file
) {
583 if (file
.getStoragePath() == null
&& !file
.isFolder()) {
584 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
586 file
.setStoragePath(f
.getAbsolutePath());
587 file
.setLastSyncDateForData(f
.lastModified());
594 * Sends a message to any application component interested in the progress
595 * of the synchronization.
598 * @param dirRemotePath Remote path of a folder that was just synchronized
599 * (with or without success)
602 private void sendLocalBroadcast(
603 String event
, String dirRemotePath
, RemoteOperationResult result
605 Log_OC
.d(TAG
, "Send broadcast " + event
);
606 Intent intent
= new Intent(event
);
607 intent
.putExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
, mAccount
.name
);
608 if (dirRemotePath
!= null
) {
609 intent
.putExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
, dirRemotePath
);
611 intent
.putExtra(FileSyncAdapter
.EXTRA_RESULT
, result
);
612 mContext
.sendStickyBroadcast(intent
);
613 //LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
617 public boolean getRemoteFolderChanged() {
618 return mRemoteFolderChanged
;