Disable On Device option in Navigation Drawer
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RefreshFolderOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 package com.owncloud.android.operations;
22
23 import java.io.File;
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;
32 import java.util.Map;
33 import java.util.Vector;
34
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;
41
42 import com.owncloud.android.MainApp;
43 import com.owncloud.android.datamodel.FileDataStorageManager;
44 import com.owncloud.android.datamodel.OCFile;
45
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;
57
58 import com.owncloud.android.syncadapter.FileSyncAdapter;
59 import com.owncloud.android.utils.FileStorageUtils;
60
61
62
63 /**
64 * Remote operation performing the synchronization of the list of files contained
65 * in a folder identified with its remote path.
66 *
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.
69 *
70 * Does NOT enter in the child folders to synchronize their contents also.
71 */
72 public class RefreshFolderOperation extends RemoteOperation {
73
74 private static final String TAG = RefreshFolderOperation.class.getSimpleName();
75
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";
80
81 /** Time stamp for the synchronization process in progress */
82 private long mCurrentSyncTime;
83
84 /** Remote folder to synchronize */
85 private OCFile mLocalFolder;
86
87 /** Access to the local database */
88 private FileDataStorageManager mStorageManager;
89
90 /** Account where the file to synchronize belongs */
91 private Account mAccount;
92
93 /** Android context; necessary to send requests to the download service */
94 private Context mContext;
95
96 /** Files and folders contained in the synchronized folder after a successful operation */
97 private List<OCFile> mChildren;
98
99 /** Counter of conflicts found between local and remote files */
100 private int mConflictsFound;
101
102 /** Counter of failed operations in synchronization of kept-in-sync files */
103 private int mFailsInFavouritesFound;
104
105 /**
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
108 **/
109 private Map<String, String> mForgottenLocalFiles;
110
111 /** 'True' means that this operation is part of a full account synchronization */
112 private boolean mSyncFullAccount;
113
114 /** 'True' means that Share resources bound to the files into should be refreshed also */
115 private boolean mIsShareSupported;
116
117 /** 'True' means that the remote folder changed and should be fetched */
118 private boolean mRemoteFolderChanged;
119
120 /** 'True' means that Etag will be ignored */
121 private boolean mIgnoreETag;
122
123
124 /**
125 * Creates a new instance of {@link RefreshFolderOperation}.
126 *
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
130 * synchronization.
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
134 * change.
135 * @param dataStorageManager Interface with the local database.
136 * @param account ownCloud account where the folder is located.
137 * @param context Application context.
138 */
139 public RefreshFolderOperation(OCFile folder,
140 long currentSyncTime,
141 boolean syncFullAccount,
142 boolean isShareSupported,
143 boolean ignoreETag,
144 FileDataStorageManager dataStorageManager,
145 Account account,
146 Context context) {
147 mLocalFolder = folder;
148 mCurrentSyncTime = currentSyncTime;
149 mSyncFullAccount = syncFullAccount;
150 mIsShareSupported = isShareSupported;
151 mStorageManager = dataStorageManager;
152 mAccount = account;
153 mContext = context;
154 mForgottenLocalFiles = new HashMap<String, String>();
155 mRemoteFolderChanged = false;
156 mIgnoreETag = ignoreETag;
157 }
158
159
160 public int getConflictsFound() {
161 return mConflictsFound;
162 }
163
164 public int getFailsInFavouritesFound() {
165 return mFailsInFavouritesFound;
166 }
167
168 public Map<String, String> getForgottenLocalFiles() {
169 return mForgottenLocalFiles;
170 }
171
172 /**
173 * Returns the list of files and folders contained in the synchronized folder,
174 * if called after synchronization is complete.
175 *
176 * @return List of files and folders contained in the synchronized folder.
177 */
178 public List<OCFile> getChildren() {
179 return mChildren;
180 }
181
182 /**
183 * Performs the synchronization.
184 *
185 * {@inheritDoc}
186 */
187 @Override
188 protected RemoteOperationResult run(OwnCloudClient client) {
189 RemoteOperationResult result = null;
190 mFailsInFavouritesFound = 0;
191 mConflictsFound = 0;
192 mForgottenLocalFiles.clear();
193
194 if (FileUtils.PATH_SEPARATOR.equals(mLocalFolder.getRemotePath()) && !mSyncFullAccount) {
195 updateOCVersion(client);
196 }
197
198 result = checkForChanges(client);
199
200 if (result.isSuccess()) {
201 if (mRemoteFolderChanged) {
202 result = fetchAndSyncRemoteFolder(client);
203 } else {
204 // TODO Enable when "On Device" is recovered ?
205 mChildren = mStorageManager.getFolderContent(mLocalFolder/*, false*/);
206 }
207 }
208
209 if (!mSyncFullAccount) {
210 sendLocalBroadcast(
211 EVENT_SINGLE_FOLDER_CONTENTS_SYNCED, mLocalFolder.getRemotePath(), result
212 );
213 }
214
215 if (result.isSuccess() && mIsShareSupported && !mSyncFullAccount) {
216 refreshSharesForFolder(client); // share result is ignored
217 }
218
219 if (!mSyncFullAccount) {
220 sendLocalBroadcast(
221 EVENT_SINGLE_FOLDER_SHARES_SYNCED, mLocalFolder.getRemotePath(), result
222 );
223 }
224
225 return result;
226
227 }
228
229
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();
235 }
236 }
237
238
239 private RemoteOperationResult checkForChanges(OwnCloudClient client) {
240 mRemoteFolderChanged = true;
241 RemoteOperationResult result = null;
242 String remotePath = null;
243
244 remotePath = mLocalFolder.getRemotePath();
245 Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath);
246
247 // remote request
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));
252
253 if (!mIgnoreETag) {
254 // check if remote and local folder are different
255 mRemoteFolderChanged =
256 !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag()));
257 }
258
259 result = new RemoteOperationResult(ResultCode.OK);
260
261 Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " +
262 (mRemoteFolderChanged ? "changed" : "not changed"));
263
264 } else {
265 // check failed
266 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
267 removeLocalFolder();
268 }
269 if (result.isException()) {
270 Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " +
271 result.getLogMessage(), result.getException());
272 } else {
273 Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " +
274 result.getLogMessage());
275 }
276 }
277
278 return result;
279 }
280
281
282 private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) {
283 String remotePath = mLocalFolder.getRemotePath();
284 ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(remotePath);
285 RemoteOperationResult result = operation.execute(client);
286 Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
287
288 if (result.isSuccess()) {
289 synchronizeData(result.getData(), client);
290 if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
291 result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
292 // should be a different result code, but will do the job
293 }
294 } else {
295 if (result.getCode() == ResultCode.FILE_NOT_FOUND)
296 removeLocalFolder();
297 }
298
299 return result;
300 }
301
302
303 private void removeLocalFolder() {
304 if (mStorageManager.fileExists(mLocalFolder.getFileId())) {
305 String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
306 mStorageManager.removeFolder(
307 mLocalFolder,
308 true,
309 ( mLocalFolder.isDown() &&
310 mLocalFolder.getStoragePath().startsWith(currentSavePath)
311 )
312 );
313 }
314 }
315
316
317 /**
318 * Synchronizes the data retrieved from the server about the contents of the target folder
319 * with the current data in the local database.
320 *
321 * Grants that mChildren is updated with fresh data after execution.
322 *
323 * @param folderAndFiles Remote folder and children files in Folder
324 *
325 * @param client Client instance to the remote server where the data were
326 * retrieved.
327 * @return 'True' when any change was made in the local data, 'false' otherwise
328 */
329 private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client) {
330 // get 'fresh data' from the database
331 mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath());
332
333 // parse data from remote folder
334 OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0));
335 remoteFolder.setParentId(mLocalFolder.getParentId());
336 remoteFolder.setFileId(mLocalFolder.getFileId());
337
338 Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath()
339 + " changed - starting update of local data ");
340
341 List<OCFile> updatedFiles = new Vector<OCFile>(folderAndFiles.size() - 1);
342 List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
343
344 // get current data about local contents of the folder to synchronize
345 // TODO Enable when "On Device" is recovered ?
346 List<OCFile> localFiles = mStorageManager.getFolderContent(mLocalFolder/*, false*/);
347 Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
348 for (OCFile file : localFiles) {
349 localFilesMap.put(file.getRemotePath(), file);
350 }
351
352 // loop to update every child
353 OCFile remoteFile = null, localFile = null;
354 for (int i=1; i<folderAndFiles.size(); i++) {
355 /// new OCFile instance with the data from the server
356 remoteFile = fillOCFile((RemoteFile)folderAndFiles.get(i));
357 remoteFile.setParentId(mLocalFolder.getFileId());
358
359 /// retrieve local data for the read file
360 // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
361 localFile = localFilesMap.remove(remoteFile.getRemotePath());
362
363 /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in server)
364 remoteFile.setLastSyncDateForProperties(mCurrentSyncTime);
365 if (localFile != null) {
366 // some properties of local state are kept unmodified
367 remoteFile.setFileId(localFile.getFileId());
368 remoteFile.setKeepInSync(localFile.keepInSync());
369 remoteFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
370 remoteFile.setModificationTimestampAtLastSyncForData(
371 localFile.getModificationTimestampAtLastSyncForData()
372 );
373 remoteFile.setStoragePath(localFile.getStoragePath());
374 // eTag will not be updated unless contents are synchronized
375 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
376 remoteFile.setEtag(localFile.getEtag());
377 if (remoteFile.isFolder()) {
378 remoteFile.setFileLength(localFile.getFileLength());
379 // TODO move operations about size of folders to FileContentProvider
380 } else if (mRemoteFolderChanged && remoteFile.isImage() &&
381 remoteFile.getModificationTimestamp() !=
382 localFile.getModificationTimestamp()) {
383 remoteFile.setNeedsUpdateThumbnail(true);
384 Log.d(TAG, "Image " + remoteFile.getFileName() + " updated on the server");
385 }
386 remoteFile.setPublicLink(localFile.getPublicLink());
387 remoteFile.setShareByLink(localFile.isShareByLink());
388 } else {
389 // remote eTag will not be updated unless contents are synchronized
390 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
391 remoteFile.setEtag("");
392 }
393
394 /// check and fix, if needed, local storage path
395 checkAndFixForeignStoragePath(remoteFile); // policy - local files are COPIED
396 // into the ownCloud local folder;
397 searchForLocalFileInDefaultPath(remoteFile); // legacy
398
399 /// prepare content synchronization for kept-in-sync files
400 if (remoteFile.keepInSync()) {
401 SynchronizeFileOperation operation = new SynchronizeFileOperation( localFile,
402 remoteFile,
403 mAccount,
404 true,
405 mContext
406 );
407
408 filesToSyncContents.add(operation);
409 }
410
411 updatedFiles.add(remoteFile);
412 }
413
414 // save updated contents in local database
415 mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
416
417 // request for the synchronization of file contents AFTER saving current remote properties
418 startContentSynchronizations(filesToSyncContents, client);
419
420 mChildren = updatedFiles;
421 }
422
423 /**
424 * Performs a list of synchronization operations, determining if a download or upload is needed
425 * or if exists conflict due to changes both in local and remote contents of the each file.
426 *
427 * If download or upload is needed, request the operation to the corresponding service and goes
428 * on.
429 *
430 * @param filesToSyncContents Synchronization operations to execute.
431 * @param client Interface to the remote ownCloud server.
432 */
433 private void startContentSynchronizations(
434 List<SynchronizeFileOperation> filesToSyncContents, OwnCloudClient client
435 ) {
436 RemoteOperationResult contentsResult = null;
437 for (SynchronizeFileOperation op: filesToSyncContents) {
438 contentsResult = op.execute(mStorageManager, mContext); // async
439 if (!contentsResult.isSuccess()) {
440 if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
441 mConflictsFound++;
442 } else {
443 mFailsInFavouritesFound++;
444 if (contentsResult.getException() != null) {
445 Log_OC.e(TAG, "Error while synchronizing favourites : "
446 + contentsResult.getLogMessage(), contentsResult.getException());
447 } else {
448 Log_OC.e(TAG, "Error while synchronizing favourites : "
449 + contentsResult.getLogMessage());
450 }
451 }
452 } // won't let these fails break the synchronization process
453 }
454 }
455
456
457 public boolean isMultiStatus(int status) {
458 return (status == HttpStatus.SC_MULTI_STATUS);
459 }
460
461 /**
462 * Creates and populates a new {@link OCFile} object with the data read from the server.
463 *
464 * @param remote remote file read from the server (remote file or folder).
465 * @return New OCFile instance representing the remote resource described by we.
466 */
467 private OCFile fillOCFile(RemoteFile remote) {
468 OCFile file = new OCFile(remote.getRemotePath());
469 file.setCreationTimestamp(remote.getCreationTimestamp());
470 file.setFileLength(remote.getLength());
471 file.setMimetype(remote.getMimeType());
472 file.setModificationTimestamp(remote.getModifiedTimestamp());
473 file.setEtag(remote.getEtag());
474 file.setPermissions(remote.getPermissions());
475 file.setRemoteId(remote.getRemoteId());
476 return file;
477 }
478
479
480 /**
481 * Checks the storage path of the OCFile received as parameter.
482 * If it's out of the local ownCloud folder, tries to copy the file inside it.
483 *
484 * If the copy fails, the link to the local file is nullified. The account of forgotten
485 * files is kept in {@link #mForgottenLocalFiles}
486 *)
487 * @param file File to check and fix.
488 */
489 private void checkAndFixForeignStoragePath(OCFile file) {
490 String storagePath = file.getStoragePath();
491 String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
492 if (storagePath != null && !storagePath.equals(expectedPath)) {
493 /// fix storagePaths out of the local ownCloud folder
494 File originalFile = new File(storagePath);
495 if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) {
496 mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
497 file.setStoragePath(null);
498
499 } else {
500 InputStream in = null;
501 OutputStream out = null;
502 try {
503 File expectedFile = new File(expectedPath);
504 File expectedParent = expectedFile.getParentFile();
505 expectedParent.mkdirs();
506 if (!expectedParent.isDirectory()) {
507 throw new IOException(
508 "Unexpected error: parent directory could not be created"
509 );
510 }
511 expectedFile.createNewFile();
512 if (!expectedFile.isFile()) {
513 throw new IOException("Unexpected error: target file could not be created");
514 }
515 in = new FileInputStream(originalFile);
516 out = new FileOutputStream(expectedFile);
517 byte[] buf = new byte[1024];
518 int len;
519 while ((len = in.read(buf)) > 0){
520 out.write(buf, 0, len);
521 }
522 file.setStoragePath(expectedPath);
523
524 } catch (Exception e) {
525 Log_OC.e(TAG, "Exception while copying foreign file " + expectedPath, e);
526 mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
527 file.setStoragePath(null);
528
529 } finally {
530 try {
531 if (in != null) in.close();
532 } catch (Exception e) {
533 Log_OC.d(TAG, "Weird exception while closing input stream for "
534 + storagePath + " (ignoring)", e);
535 }
536 try {
537 if (out != null) out.close();
538 } catch (Exception e) {
539 Log_OC.d(TAG, "Weird exception while closing output stream for "
540 + expectedPath + " (ignoring)", e);
541 }
542 }
543 }
544 }
545 }
546
547
548 private RemoteOperationResult refreshSharesForFolder(OwnCloudClient client) {
549 RemoteOperationResult result = null;
550
551 // remote request
552 GetRemoteSharesForFileOperation operation =
553 new GetRemoteSharesForFileOperation(mLocalFolder.getRemotePath(), false, true);
554 result = operation.execute(client);
555
556 if (result.isSuccess()) {
557 // update local database
558 ArrayList<OCShare> shares = new ArrayList<OCShare>();
559 for(Object obj: result.getData()) {
560 shares.add((OCShare) obj);
561 }
562 mStorageManager.saveSharesInFolder(shares, mLocalFolder);
563 }
564
565 return result;
566 }
567
568
569 /**
570 * Scans the default location for saving local copies of files searching for
571 * a 'lost' file with the same full name as the {@link OCFile} received as
572 * parameter.
573 *
574 * @param file File to associate a possible 'lost' local file.
575 */
576 private void searchForLocalFileInDefaultPath(OCFile file) {
577 if (file.getStoragePath() == null && !file.isFolder()) {
578 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
579 if (f.exists()) {
580 file.setStoragePath(f.getAbsolutePath());
581 file.setLastSyncDateForData(f.lastModified());
582 }
583 }
584 }
585
586
587 /**
588 * Sends a message to any application component interested in the progress
589 * of the synchronization.
590 *
591 * @param event
592 * @param dirRemotePath Remote path of a folder that was just synchronized
593 * (with or without success)
594 * @param result
595 */
596 private void sendLocalBroadcast(
597 String event, String dirRemotePath, RemoteOperationResult result
598 ) {
599 Log_OC.d(TAG, "Send broadcast " + event);
600 Intent intent = new Intent(event);
601 intent.putExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME, mAccount.name);
602 if (dirRemotePath != null) {
603 intent.putExtra(FileSyncAdapter.EXTRA_FOLDER_PATH, dirRemotePath);
604 }
605 intent.putExtra(FileSyncAdapter.EXTRA_RESULT, result);
606 mContext.sendStickyBroadcast(intent);
607 //LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
608 }
609
610
611 public boolean getRemoteFolderChanged() {
612 return mRemoteFolderChanged;
613 }
614
615 }