1 /* ownCloud Android client application
2 * @author David A. Velasco
3 * Copyright (C) 2012-2014 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.operations
;
22 import java
.io
.FileInputStream
;
23 import java
.io
.FileOutputStream
;
24 import java
.io
.IOException
;
25 import java
.io
.InputStream
;
26 import java
.io
.OutputStream
;
27 import java
.util
.ArrayList
;
28 import java
.util
.HashMap
;
29 import java
.util
.List
;
31 import java
.util
.Vector
;
33 import org
.apache
.http
.HttpStatus
;
34 import android
.accounts
.Account
;
35 import android
.content
.Context
;
36 import android
.content
.Intent
;
37 import android
.util
.Log
;
38 //import android.support.v4.content.LocalBroadcastManager;
40 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
41 import com
.owncloud
.android
.datamodel
.OCFile
;
43 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
44 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
45 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
46 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
47 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
48 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
49 import com
.owncloud
.android
.lib
.resources
.shares
.GetRemoteSharesForFileOperation
;
50 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
51 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFileOperation
;
52 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFolderOperation
;
53 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
55 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
56 import com
.owncloud
.android
.utils
.FileStorageUtils
;
61 * Remote operation performing the synchronization of the list of files contained
62 * in a folder identified with its remote path.
64 * Fetches the list and properties of the files contained in the given folder, including their
65 * properties, and updates the local database with them.
67 * Does NOT enter in the child folders to synchronize their contents also.
69 public class RefreshFolderOperation
extends RemoteOperation
{
71 private static final String TAG
= RefreshFolderOperation
.class.getSimpleName();
73 public static final String EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
=
74 RefreshFolderOperation
.class.getName() + ".EVENT_SINGLE_FOLDER_CONTENTS_SYNCED";
75 public static final String EVENT_SINGLE_FOLDER_SHARES_SYNCED
=
76 RefreshFolderOperation
.class.getName() + ".EVENT_SINGLE_FOLDER_SHARES_SYNCED";
78 /** Time stamp for the synchronization process in progress */
79 private long mCurrentSyncTime
;
81 /** Remote folder to synchronize */
82 private OCFile mLocalFolder
;
84 /** Access to the local database */
85 private FileDataStorageManager mStorageManager
;
87 /** Account where the file to synchronize belongs */
88 private Account mAccount
;
90 /** Android context; necessary to send requests to the download service */
91 private Context mContext
;
93 /** Files and folders contained in the synchronized folder after a successful operation */
94 private List
<OCFile
> mChildren
;
96 /** Counter of conflicts found between local and remote files */
97 private int mConflictsFound
;
99 /** Counter of failed operations in synchronization of kept-in-sync files */
100 private int mFailsInFavouritesFound
;
103 * Map of remote and local paths to files that where locally stored in a location
104 * out of the ownCloud folder and couldn't be copied automatically into it
106 private Map
<String
, String
> mForgottenLocalFiles
;
108 /** 'True' means that this operation is part of a full account synchronization */
109 private boolean mSyncFullAccount
;
111 /** 'True' means that Share resources bound to the files into should be refreshed also */
112 private boolean mIsShareSupported
;
114 /** 'True' means that the remote folder changed and should be fetched */
115 private boolean mRemoteFolderChanged
;
117 /** 'True' means that Etag will be ignored */
118 private boolean mIgnoreETag
;
122 * Creates a new instance of {@link RefreshFolderOperation}.
124 * @param folder Folder to synchronize.
125 * @param currentSyncTime Time stamp for the synchronization process in progress.
126 * @param syncFullAccount 'True' means that this operation is part of a full account
128 * @param isShareSupported 'True' means that the server supports the sharing API.
129 * @param ignoreEtag 'True' means that the content of the remote folder should
130 * be fetched and updated even though the 'eTag' did not
132 * @param dataStorageManager Interface with the local database.
133 * @param account ownCloud account where the folder is located.
134 * @param context Application context.
136 public RefreshFolderOperation(OCFile folder
,
137 long currentSyncTime
,
138 boolean syncFullAccount
,
139 boolean isShareSupported
,
141 FileDataStorageManager dataStorageManager
,
144 mLocalFolder
= folder
;
145 mCurrentSyncTime
= currentSyncTime
;
146 mSyncFullAccount
= syncFullAccount
;
147 mIsShareSupported
= isShareSupported
;
148 mStorageManager
= dataStorageManager
;
151 mForgottenLocalFiles
= new HashMap
<String
, String
>();
152 mRemoteFolderChanged
= false
;
153 mIgnoreETag
= ignoreETag
;
157 public int getConflictsFound() {
158 return mConflictsFound
;
161 public int getFailsInFavouritesFound() {
162 return mFailsInFavouritesFound
;
165 public Map
<String
, String
> getForgottenLocalFiles() {
166 return mForgottenLocalFiles
;
170 * Returns the list of files and folders contained in the synchronized folder,
171 * if called after synchronization is complete.
173 * @return List of files and folders contained in the synchronized folder.
175 public List
<OCFile
> getChildren() {
180 * Performs the synchronization.
185 protected RemoteOperationResult
run(OwnCloudClient client
) {
186 RemoteOperationResult result
= null
;
187 mFailsInFavouritesFound
= 0;
189 mForgottenLocalFiles
.clear();
191 if (FileUtils
.PATH_SEPARATOR
.equals(mLocalFolder
.getRemotePath()) && !mSyncFullAccount
) {
192 updateOCVersion(client
);
195 result
= checkForChanges(client
);
197 if (result
.isSuccess()) {
198 if (mRemoteFolderChanged
) {
199 result
= fetchAndSyncRemoteFolder(client
);
201 mChildren
= mStorageManager
.getFolderContent(mLocalFolder
);
205 if (!mSyncFullAccount
) {
207 EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
, mLocalFolder
.getRemotePath(), result
211 if (result
.isSuccess() && mIsShareSupported
&& !mSyncFullAccount
) {
212 refreshSharesForFolder(client
); // share result is ignored
215 if (!mSyncFullAccount
) {
217 EVENT_SINGLE_FOLDER_SHARES_SYNCED
, mLocalFolder
.getRemotePath(), result
226 private void updateOCVersion(OwnCloudClient client
) {
227 UpdateOCVersionOperation update
= new UpdateOCVersionOperation(mAccount
, mContext
);
228 RemoteOperationResult result
= update
.execute(client
);
229 if (result
.isSuccess()) {
230 mIsShareSupported
= update
.getOCVersion().isSharedSupported();
235 private RemoteOperationResult
checkForChanges(OwnCloudClient client
) {
236 mRemoteFolderChanged
= true
;
237 RemoteOperationResult result
= null
;
238 String remotePath
= null
;
240 remotePath
= mLocalFolder
.getRemotePath();
241 Log_OC
.d(TAG
, "Checking changes in " + mAccount
.name
+ remotePath
);
244 ReadRemoteFileOperation operation
= new ReadRemoteFileOperation(remotePath
);
245 result
= operation
.execute(client
);
246 if (result
.isSuccess()){
247 OCFile remoteFolder
= FileStorageUtils
.fillOCFile((RemoteFile
) result
.getData().get(0));
250 // check if remote and local folder are different
251 mRemoteFolderChanged
=
252 !(remoteFolder
.getEtag().equalsIgnoreCase(mLocalFolder
.getEtag()));
255 result
= new RemoteOperationResult(ResultCode
.OK
);
257 Log_OC
.i(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
258 (mRemoteFolderChanged ?
"changed" : "not changed"));
262 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
265 if (result
.isException()) {
266 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
267 result
.getLogMessage(), result
.getException());
269 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
270 result
.getLogMessage());
278 private RemoteOperationResult
fetchAndSyncRemoteFolder(OwnCloudClient client
) {
279 String remotePath
= mLocalFolder
.getRemotePath();
280 ReadRemoteFolderOperation operation
= new ReadRemoteFolderOperation(remotePath
);
281 RemoteOperationResult result
= operation
.execute(client
);
282 Log_OC
.d(TAG
, "Synchronizing " + mAccount
.name
+ remotePath
);
284 if (result
.isSuccess()) {
285 synchronizeData(result
.getData(), client
);
286 if (mConflictsFound
> 0 || mFailsInFavouritesFound
> 0) {
287 result
= new RemoteOperationResult(ResultCode
.SYNC_CONFLICT
);
288 // should be a different result code, but will do the job
291 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
)
299 private void removeLocalFolder() {
300 if (mStorageManager
.fileExists(mLocalFolder
.getFileId())) {
301 String currentSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
302 mStorageManager
.removeFolder(
305 ( mLocalFolder
.isDown() &&
306 mLocalFolder
.getStoragePath().startsWith(currentSavePath
)
314 * Synchronizes the data retrieved from the server about the contents of the target folder
315 * with the current data in the local database.
317 * Grants that mChildren is updated with fresh data after execution.
319 * @param folderAndFiles Remote folder and children files in Folder
321 * @param client Client instance to the remote server where the data were
323 * @return 'True' when any change was made in the local data, 'false' otherwise
325 private void synchronizeData(ArrayList
<Object
> folderAndFiles
, OwnCloudClient client
) {
326 // get 'fresh data' from the database
327 mLocalFolder
= mStorageManager
.getFileByPath(mLocalFolder
.getRemotePath());
329 // parse data from remote folder
330 OCFile remoteFolder
= fillOCFile((RemoteFile
)folderAndFiles
.get(0));
331 remoteFolder
.setParentId(mLocalFolder
.getParentId());
332 remoteFolder
.setFileId(mLocalFolder
.getFileId());
334 Log_OC
.d(TAG
, "Remote folder " + mLocalFolder
.getRemotePath()
335 + " changed - starting update of local data ");
337 List
<OCFile
> updatedFiles
= new Vector
<OCFile
>(folderAndFiles
.size() - 1);
338 List
<SynchronizeFileOperation
> filesToSyncContents
= new Vector
<SynchronizeFileOperation
>();
340 // get current data about local contents of the folder to synchronize
341 List
<OCFile
> localFiles
= mStorageManager
.getFolderContent(mLocalFolder
);
342 Map
<String
, OCFile
> localFilesMap
= new HashMap
<String
, OCFile
>(localFiles
.size());
343 for (OCFile file
: localFiles
) {
344 localFilesMap
.put(file
.getRemotePath(), file
);
347 // loop to update every child
348 OCFile remoteFile
= null
, localFile
= null
;
349 for (int i
=1; i
<folderAndFiles
.size(); i
++) {
350 /// new OCFile instance with the data from the server
351 remoteFile
= fillOCFile((RemoteFile
)folderAndFiles
.get(i
));
352 remoteFile
.setParentId(mLocalFolder
.getFileId());
354 /// retrieve local data for the read file
355 // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
356 localFile
= localFilesMap
.remove(remoteFile
.getRemotePath());
358 /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in server)
359 remoteFile
.setLastSyncDateForProperties(mCurrentSyncTime
);
360 if (localFile
!= null
) {
361 // some properties of local state are kept unmodified
362 remoteFile
.setFileId(localFile
.getFileId());
363 remoteFile
.setKeepInSync(localFile
.keepInSync());
364 remoteFile
.setLastSyncDateForData(localFile
.getLastSyncDateForData());
365 remoteFile
.setModificationTimestampAtLastSyncForData(
366 localFile
.getModificationTimestampAtLastSyncForData()
368 remoteFile
.setStoragePath(localFile
.getStoragePath());
369 // eTag will not be updated unless contents are synchronized
370 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
371 remoteFile
.setEtag(localFile
.getEtag());
372 if (remoteFile
.isFolder()) {
373 remoteFile
.setFileLength(localFile
.getFileLength());
374 // TODO move operations about size of folders to FileContentProvider
375 } else if (mRemoteFolderChanged
&& remoteFile
.isImage() &&
376 remoteFile
.getModificationTimestamp() != localFile
.getModificationTimestamp()) {
377 remoteFile
.setNeedsUpdateThumbnail(true
);
378 Log
.d(TAG
, "Image " + remoteFile
.getFileName() + " updated on the server");
380 remoteFile
.setPublicLink(localFile
.getPublicLink());
381 remoteFile
.setShareByLink(localFile
.isShareByLink());
383 // remote eTag will not be updated unless contents are synchronized
384 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
385 remoteFile
.setEtag("");
388 /// check and fix, if needed, local storage path
389 checkAndFixForeignStoragePath(remoteFile
); // policy - local files are COPIED
390 // into the ownCloud local folder;
391 searchForLocalFileInDefaultPath(remoteFile
); // legacy
393 /// prepare content synchronization for kept-in-sync files
394 if (remoteFile
.keepInSync()) {
395 SynchronizeFileOperation operation
= new SynchronizeFileOperation( localFile
,
402 filesToSyncContents
.add(operation
);
405 updatedFiles
.add(remoteFile
);
408 // save updated contents in local database
409 mStorageManager
.saveFolder(remoteFolder
, updatedFiles
, localFilesMap
.values());
411 // request for the synchronization of file contents AFTER saving current remote properties
412 startContentSynchronizations(filesToSyncContents
, client
);
414 mChildren
= updatedFiles
;
418 * Performs a list of synchronization operations, determining if a download or upload is needed
419 * or if exists conflict due to changes both in local and remote contents of the each file.
421 * If download or upload is needed, request the operation to the corresponding service and goes
424 * @param filesToSyncContents Synchronization operations to execute.
425 * @param client Interface to the remote ownCloud server.
427 private void startContentSynchronizations(
428 List
<SynchronizeFileOperation
> filesToSyncContents
, OwnCloudClient client
430 RemoteOperationResult contentsResult
= null
;
431 for (SynchronizeFileOperation op
: filesToSyncContents
) {
432 contentsResult
= op
.execute(mStorageManager
, mContext
); // async
433 if (!contentsResult
.isSuccess()) {
434 if (contentsResult
.getCode() == ResultCode
.SYNC_CONFLICT
) {
437 mFailsInFavouritesFound
++;
438 if (contentsResult
.getException() != null
) {
439 Log_OC
.e(TAG
, "Error while synchronizing favourites : "
440 + contentsResult
.getLogMessage(), contentsResult
.getException());
442 Log_OC
.e(TAG
, "Error while synchronizing favourites : "
443 + contentsResult
.getLogMessage());
446 } // won't let these fails break the synchronization process
451 public boolean isMultiStatus(int status
) {
452 return (status
== HttpStatus
.SC_MULTI_STATUS
);
456 * Creates and populates a new {@link OCFile} object with the data read from the server.
458 * @param remote remote file read from the server (remote file or folder).
459 * @return New OCFile instance representing the remote resource described by we.
461 private OCFile
fillOCFile(RemoteFile remote
) {
462 OCFile file
= new OCFile(remote
.getRemotePath());
463 file
.setCreationTimestamp(remote
.getCreationTimestamp());
464 file
.setFileLength(remote
.getLength());
465 file
.setMimetype(remote
.getMimeType());
466 file
.setModificationTimestamp(remote
.getModifiedTimestamp());
467 file
.setEtag(remote
.getEtag());
468 file
.setPermissions(remote
.getPermissions());
469 file
.setRemoteId(remote
.getRemoteId());
475 * Checks the storage path of the OCFile received as parameter.
476 * If it's out of the local ownCloud folder, tries to copy the file inside it.
478 * If the copy fails, the link to the local file is nullified. The account of forgotten
479 * files is kept in {@link #mForgottenLocalFiles}
481 * @param file File to check and fix.
483 private void checkAndFixForeignStoragePath(OCFile file
) {
484 String storagePath
= file
.getStoragePath();
485 String expectedPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
486 if (storagePath
!= null
&& !storagePath
.equals(expectedPath
)) {
487 /// fix storagePaths out of the local ownCloud folder
488 File originalFile
= new File(storagePath
);
489 if (FileStorageUtils
.getUsableSpace(mAccount
.name
) < originalFile
.length()) {
490 mForgottenLocalFiles
.put(file
.getRemotePath(), storagePath
);
491 file
.setStoragePath(null
);
494 InputStream
in = null
;
495 OutputStream out
= null
;
497 File expectedFile
= new File(expectedPath
);
498 File expectedParent
= expectedFile
.getParentFile();
499 expectedParent
.mkdirs();
500 if (!expectedParent
.isDirectory()) {
501 throw new IOException(
502 "Unexpected error: parent directory could not be created"
505 expectedFile
.createNewFile();
506 if (!expectedFile
.isFile()) {
507 throw new IOException("Unexpected error: target file could not be created");
509 in = new FileInputStream(originalFile
);
510 out
= new FileOutputStream(expectedFile
);
511 byte[] buf
= new byte[1024];
513 while ((len
= in.read(buf
)) > 0){
514 out
.write(buf
, 0, len
);
516 file
.setStoragePath(expectedPath
);
518 } catch (Exception e
) {
519 Log_OC
.e(TAG
, "Exception while copying foreign file " + expectedPath
, e
);
520 mForgottenLocalFiles
.put(file
.getRemotePath(), storagePath
);
521 file
.setStoragePath(null
);
525 if (in != null
) in.close();
526 } catch (Exception e
) {
527 Log_OC
.d(TAG
, "Weird exception while closing input stream for "
528 + storagePath
+ " (ignoring)", e
);
531 if (out
!= null
) out
.close();
532 } catch (Exception e
) {
533 Log_OC
.d(TAG
, "Weird exception while closing output stream for "
534 + expectedPath
+ " (ignoring)", e
);
542 private RemoteOperationResult
refreshSharesForFolder(OwnCloudClient client
) {
543 RemoteOperationResult result
= null
;
546 GetRemoteSharesForFileOperation operation
=
547 new GetRemoteSharesForFileOperation(mLocalFolder
.getRemotePath(), false
, true
);
548 result
= operation
.execute(client
);
550 if (result
.isSuccess()) {
551 // update local database
552 ArrayList
<OCShare
> shares
= new ArrayList
<OCShare
>();
553 for(Object obj
: result
.getData()) {
554 shares
.add((OCShare
) obj
);
556 mStorageManager
.saveSharesInFolder(shares
, mLocalFolder
);
564 * Scans the default location for saving local copies of files searching for
565 * a 'lost' file with the same full name as the {@link OCFile} received as
568 * @param file File to associate a possible 'lost' local file.
570 private void searchForLocalFileInDefaultPath(OCFile file
) {
571 if (file
.getStoragePath() == null
&& !file
.isFolder()) {
572 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
574 file
.setStoragePath(f
.getAbsolutePath());
575 file
.setLastSyncDateForData(f
.lastModified());
582 * Sends a message to any application component interested in the progress
583 * of the synchronization.
586 * @param dirRemotePath Remote path of a folder that was just synchronized
587 * (with or without success)
590 private void sendLocalBroadcast(
591 String event
, String dirRemotePath
, RemoteOperationResult result
593 Log_OC
.d(TAG
, "Send broadcast " + event
);
594 Intent intent
= new Intent(event
);
595 intent
.putExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
, mAccount
.name
);
596 if (dirRemotePath
!= null
) {
597 intent
.putExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
, dirRemotePath
);
599 intent
.putExtra(FileSyncAdapter
.EXTRA_RESULT
, result
);
600 mContext
.sendStickyBroadcast(intent
);
601 //LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
605 public boolean getRemoteFolderChanged() {
606 return mRemoteFolderChanged
;