Merge branch 'develop' into refactor_remote_operation_to_read_file
[pub/Android/ownCloud.git] / src / com / owncloud / android / syncadapter / FileSyncAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.syncadapter;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.jackrabbit.webdav.DavException;
28
29 import com.owncloud.android.R;
30 import com.owncloud.android.authentication.AuthenticatorActivity;
31 import com.owncloud.android.datamodel.FileDataStorageManager;
32 import com.owncloud.android.datamodel.OCFile;
33 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
34 import com.owncloud.android.operations.SynchronizeFolderOperation;
35 import com.owncloud.android.operations.UpdateOCVersionOperation;
36 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
37 import com.owncloud.android.ui.activity.ErrorsWhileCopyingHandlerActivity;
38 import com.owncloud.android.utils.DisplayUtils;
39 import com.owncloud.android.utils.Log_OC;
40
41
42 import android.accounts.Account;
43 import android.accounts.AccountsException;
44 import android.app.Notification;
45 import android.app.NotificationManager;
46 import android.app.PendingIntent;
47 import android.content.AbstractThreadedSyncAdapter;
48 import android.content.ContentProviderClient;
49 import android.content.ContentResolver;
50 import android.content.Context;
51 import android.content.Intent;
52 import android.content.SyncResult;
53 import android.os.Bundle;
54
55 /**
56 * Implementation of {@link AbstractThreadedSyncAdapter} responsible for synchronizing
57 * ownCloud files.
58 *
59 * Performs a full synchronization of the account recieved in {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}.
60 *
61 * @author Bartek Przybylski
62 * @author David A. Velasco
63 */
64 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
65
66 private final static String TAG = FileSyncAdapter.class.getSimpleName();
67
68 /** Maximum number of failed folder synchronizations that are supported before finishing the synchronization operation */
69 private static final int MAX_FAILED_RESULTS = 3;
70
71
72 /** Time stamp for the current synchronization process, used to distinguish fresh data */
73 private long mCurrentSyncTime;
74
75 /** Flag made 'true' when a request to cancel the synchronization is received */
76 private boolean mCancellation;
77
78 /** When 'true' the process was requested by the user through the user interface; when 'false', it was requested automatically by the system */
79 private boolean mIsManualSync;
80
81 /** Counter for failed operations in the synchronization process */
82 private int mFailedResultsCounter;
83
84 /** Result of the last failed operation */
85 private RemoteOperationResult mLastFailedResult;
86
87 /** Counter of conflicts found between local and remote files */
88 private int mConflictsFound;
89
90 /** Counter of failed operations in synchronization of kept-in-sync files */
91 private int mFailsInFavouritesFound;
92
93 /** Map of remote and local paths to files that where locally stored in a location out of the ownCloud folder and couldn't be copied automatically into it */
94 private Map<String, String> mForgottenLocalFiles;
95
96 /** {@link SyncResult} instance to return to the system when the synchronization finish */
97 private SyncResult mSyncResult;
98
99
100 /**
101 * Creates a {@link FileSyncAdapter}
102 *
103 * {@inheritDoc}
104 */
105 public FileSyncAdapter(Context context, boolean autoInitialize) {
106 super(context, autoInitialize);
107 }
108
109
110 /**
111 * Creates a {@link FileSyncAdapter}
112 *
113 * {@inheritDoc}
114 */
115 public FileSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
116 super(context, autoInitialize, allowParallelSyncs);
117 }
118
119
120 /**
121 * {@inheritDoc}
122 */
123 @Override
124 public synchronized void onPerformSync(Account account, Bundle extras,
125 String authority, ContentProviderClient providerClient,
126 SyncResult syncResult) {
127
128 mCancellation = false;
129 mIsManualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
130 mFailedResultsCounter = 0;
131 mLastFailedResult = null;
132 mConflictsFound = 0;
133 mFailsInFavouritesFound = 0;
134 mForgottenLocalFiles = new HashMap<String, String>();
135 mSyncResult = syncResult;
136 mSyncResult.fullSyncRequested = false;
137 mSyncResult.delayUntil = 60*60*24; // avoid too many automatic synchronizations
138
139 this.setAccount(account);
140 this.setContentProviderClient(providerClient);
141 this.setStorageManager(new FileDataStorageManager(account, providerClient));
142 try {
143 this.initClientForCurrentAccount();
144 } catch (IOException e) {
145 /// the account is unknown for the Synchronization Manager, unreachable this context, or can not be authenticated; don't try this again
146 mSyncResult.tooManyRetries = true;
147 notifyFailedSynchronization();
148 return;
149 } catch (AccountsException e) {
150 /// the account is unknown for the Synchronization Manager, unreachable this context, or can not be authenticated; don't try this again
151 mSyncResult.tooManyRetries = true;
152 notifyFailedSynchronization();
153 return;
154 }
155
156 Log_OC.d(TAG, "Synchronization of ownCloud account " + account.name + " starting");
157 sendStickyBroadcast(true, null, null); // message to signal the start of the synchronization to the UI
158
159 try {
160 updateOCVersion();
161 mCurrentSyncTime = System.currentTimeMillis();
162 if (!mCancellation) {
163 synchronizeFolder(getStorageManager().getFileByPath(OCFile.ROOT_PATH));
164
165 } else {
166 Log_OC.d(TAG, "Leaving synchronization before synchronizing the root folder because cancelation request");
167 }
168
169
170 } finally {
171 // it's important making this although very unexpected errors occur; that's the reason for the finally
172
173 if (mFailedResultsCounter > 0 && mIsManualSync) {
174 /// don't let the system synchronization manager retries MANUAL synchronizations
175 // (be careful: "MANUAL" currently includes the synchronization requested when a new account is created and when the user changes the current account)
176 mSyncResult.tooManyRetries = true;
177
178 /// notify the user about the failure of MANUAL synchronization
179 notifyFailedSynchronization();
180 }
181 if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
182 notifyFailsInFavourites();
183 }
184 if (mForgottenLocalFiles.size() > 0) {
185 notifyForgottenLocalFiles();
186 }
187 sendStickyBroadcast(false, null, mLastFailedResult); // message to signal the end to the UI
188 }
189
190 }
191
192 /**
193 * Called by system SyncManager when a synchronization is required to be cancelled.
194 *
195 * Sets the mCancellation flag to 'true'. THe synchronization will be stopped later,
196 * before a new folder is fetched. Data of the last folder synchronized will be still
197 * locally saved.
198 *
199 * See {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}
200 * and {@link #synchronizeFolder(String, long)}.
201 */
202 @Override
203 public void onSyncCanceled() {
204 Log_OC.d(TAG, "Synchronization of " + getAccount().name + " has been requested to cancel");
205 mCancellation = true;
206 super.onSyncCanceled();
207 }
208
209
210 /**
211 * Updates the locally stored version value of the ownCloud server
212 */
213 private void updateOCVersion() {
214 UpdateOCVersionOperation update = new UpdateOCVersionOperation(getAccount(), getContext());
215 RemoteOperationResult result = update.execute(getClient());
216 if (!result.isSuccess()) {
217 mLastFailedResult = result;
218 }
219 }
220
221
222 /**
223 * Synchronizes the list of files contained in a folder identified with its remote path.
224 *
225 * Fetches the list and properties of the files contained in the given folder, including their
226 * properties, and updates the local database with them.
227 *
228 * Enters in the child folders to synchronize their contents also, following a recursive
229 * depth first strategy.
230 *
231 * @param folder Folder to synchronize.
232 */
233 private void synchronizeFolder(OCFile folder) {
234
235 if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult))
236 return;
237
238 /*
239 OCFile folder,
240 long currentSyncTime,
241 boolean updateFolderProperties,
242 boolean syncFullAccount,
243 DataStorageManager dataStorageManager,
244 Account account,
245 Context context ) {
246
247 }
248 */
249 // folder synchronization
250 SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation( folder,
251 mCurrentSyncTime,
252 true,
253 getStorageManager(),
254 getAccount(),
255 getContext()
256 );
257 RemoteOperationResult result = synchFolderOp.execute(getClient());
258
259
260 // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
261 sendStickyBroadcast(true, folder.getRemotePath(), null);
262
263 // check the result of synchronizing the folder
264 if (result.isSuccess() || result.getCode() == ResultCode.SYNC_CONFLICT) {
265
266 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
267 mConflictsFound += synchFolderOp.getConflictsFound();
268 mFailsInFavouritesFound += synchFolderOp.getFailsInFavouritesFound();
269 }
270 if (synchFolderOp.getForgottenLocalFiles().size() > 0) {
271 mForgottenLocalFiles.putAll(synchFolderOp.getForgottenLocalFiles());
272 }
273 if (result.isSuccess()) {
274 // synchronize children folders
275 List<OCFile> children = synchFolderOp.getChildren();
276 fetchChildren(folder, children, synchFolderOp.getRemoteFolderChanged()); // beware of the 'hidden' recursion here!
277 }
278
279 } else {
280 // in failures, the statistics for the global result are updated
281 if (result.getCode() == RemoteOperationResult.ResultCode.UNAUTHORIZED ||
282 ( result.isIdPRedirection() &&
283 getClient().getCredentials() == null )) {
284 //MainApp.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))) {
285 mSyncResult.stats.numAuthExceptions++;
286
287 } else if (result.getException() instanceof DavException) {
288 mSyncResult.stats.numParseExceptions++;
289
290 } else if (result.getException() instanceof IOException) {
291 mSyncResult.stats.numIoExceptions++;
292 }
293 mFailedResultsCounter++;
294 mLastFailedResult = result;
295 }
296
297 }
298
299 /**
300 * Checks if a failed result should terminate the synchronization process immediately, according to
301 * OUR OWN POLICY
302 *
303 * @param failedResult Remote operation result to check.
304 * @return 'True' if the result should immediately finish the synchronization
305 */
306 private boolean isFinisher(RemoteOperationResult failedResult) {
307 if (failedResult != null) {
308 RemoteOperationResult.ResultCode code = failedResult.getCode();
309 return (code.equals(RemoteOperationResult.ResultCode.SSL_ERROR) ||
310 code.equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) ||
311 code.equals(RemoteOperationResult.ResultCode.BAD_OC_VERSION) ||
312 code.equals(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED));
313 }
314 return false;
315 }
316
317 /**
318 * Triggers the synchronization of any folder contained in the list of received files.
319 *
320 * @param files Files to recursively synchronize.
321 */
322 private void fetchChildren(OCFile parent, List<OCFile> files, boolean parentEtagChanged) {
323 int i;
324 OCFile newFile = null;
325 //String etag = null;
326 //boolean syncDown = false;
327 for (i=0; i < files.size() && !mCancellation; i++) {
328 newFile = files.get(i);
329 if (newFile.isFolder()) {
330 /*
331 etag = newFile.getEtag();
332 syncDown = (parentEtagChanged || etag == null || etag.length() == 0);
333 if(syncDown) { */
334 synchronizeFolder(newFile);
335 // update the size of the parent folder again after recursive synchronization
336 //getStorageManager().updateFolderSize(parent.getFileId());
337 sendStickyBroadcast(true, parent.getRemotePath(), null); // notify again to refresh size in UI
338 //}
339 }
340 }
341
342 if (mCancellation && i <files.size()) Log_OC.d(TAG, "Leaving synchronization before synchronizing " + files.get(i).getRemotePath() + " due to cancelation request");
343 }
344
345
346 /**
347 * Sends a message to any application component interested in the progress of the synchronization.
348 *
349 * @param inProgress 'True' when the synchronization progress is not finished.
350 * @param dirRemotePath Remote path of a folder that was just synchronized (with or without success)
351 */
352 private void sendStickyBroadcast(boolean inProgress, String dirRemotePath, RemoteOperationResult result) {
353 Intent i = new Intent(FileSyncService.getSyncMessage());
354 i.putExtra(FileSyncService.IN_PROGRESS, inProgress);
355 i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);
356 if (dirRemotePath != null) {
357 i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath);
358 }
359 if (result != null) {
360 i.putExtra(FileSyncService.SYNC_RESULT, result);
361 }
362 getContext().sendStickyBroadcast(i);
363 }
364
365
366
367 /**
368 * Notifies the user about a failed synchronization through the status notification bar
369 */
370 private void notifyFailedSynchronization() {
371 Notification notification = new Notification(DisplayUtils.getSeasonalIconId(), getContext().getString(R.string.sync_fail_ticker), System.currentTimeMillis());
372 notification.flags |= Notification.FLAG_AUTO_CANCEL;
373 boolean needsToUpdateCredentials = (mLastFailedResult != null &&
374 ( mLastFailedResult.getCode() == ResultCode.UNAUTHORIZED ||
375 ( mLastFailedResult.isIdPRedirection() &&
376 getClient().getCredentials() == null )
377 //MainApp.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))
378 )
379 );
380 // TODO put something smart in the contentIntent below for all the possible errors
381 notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0);
382 if (needsToUpdateCredentials) {
383 // let the user update credentials with one click
384 Intent updateAccountCredentials = new Intent(getContext(), AuthenticatorActivity.class);
385 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
386 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ENFORCED_UPDATE, true);
387 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
388 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
389 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
390 updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
391 notification.contentIntent = PendingIntent.getActivity(getContext(), (int)System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT);
392 notification.setLatestEventInfo(getContext().getApplicationContext(),
393 getContext().getString(R.string.sync_fail_ticker),
394 String.format(getContext().getString(R.string.sync_fail_content_unauthorized), getAccount().name),
395 notification.contentIntent);
396 } else {
397 notification.setLatestEventInfo(getContext().getApplicationContext(),
398 getContext().getString(R.string.sync_fail_ticker),
399 String.format(getContext().getString(R.string.sync_fail_content), getAccount().name),
400 notification.contentIntent);
401 }
402 ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_fail_ticker, notification);
403 }
404
405
406 /**
407 * Notifies the user about conflicts and strange fails when trying to synchronize the contents of kept-in-sync files.
408 *
409 * By now, we won't consider a failed synchronization.
410 */
411 private void notifyFailsInFavourites() {
412 if (mFailedResultsCounter > 0) {
413 Notification notification = new Notification(DisplayUtils.getSeasonalIconId(), getContext().getString(R.string.sync_fail_in_favourites_ticker), System.currentTimeMillis());
414 notification.flags |= Notification.FLAG_AUTO_CANCEL;
415 // TODO put something smart in the contentIntent below
416 notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0);
417 notification.setLatestEventInfo(getContext().getApplicationContext(),
418 getContext().getString(R.string.sync_fail_in_favourites_ticker),
419 String.format(getContext().getString(R.string.sync_fail_in_favourites_content), mFailedResultsCounter + mConflictsFound, mConflictsFound),
420 notification.contentIntent);
421 ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_fail_in_favourites_ticker, notification);
422
423 } else {
424 Notification notification = new Notification(DisplayUtils.getSeasonalIconId(), getContext().getString(R.string.sync_conflicts_in_favourites_ticker), System.currentTimeMillis());
425 notification.flags |= Notification.FLAG_AUTO_CANCEL;
426 // TODO put something smart in the contentIntent below
427 notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0);
428 notification.setLatestEventInfo(getContext().getApplicationContext(),
429 getContext().getString(R.string.sync_conflicts_in_favourites_ticker),
430 String.format(getContext().getString(R.string.sync_conflicts_in_favourites_content), mConflictsFound),
431 notification.contentIntent);
432 ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_conflicts_in_favourites_ticker, notification);
433 }
434 }
435
436 /**
437 * Notifies the user about local copies of files out of the ownCloud local directory that were 'forgotten' because
438 * copying them inside the ownCloud local directory was not possible.
439 *
440 * We don't want links to files out of the ownCloud local directory (foreign files) anymore. It's easy to have
441 * synchronization problems if a local file is linked to more than one remote file.
442 *
443 * We won't consider a synchronization as failed when foreign files can not be copied to the ownCloud local directory.
444 */
445 private void notifyForgottenLocalFiles() {
446 Notification notification = new Notification(DisplayUtils.getSeasonalIconId(), getContext().getString(R.string.sync_foreign_files_forgotten_ticker), System.currentTimeMillis());
447 notification.flags |= Notification.FLAG_AUTO_CANCEL;
448
449 /// includes a pending intent in the notification showing a more detailed explanation
450 Intent explanationIntent = new Intent(getContext(), ErrorsWhileCopyingHandlerActivity.class);
451 explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_ACCOUNT, getAccount());
452 ArrayList<String> remotePaths = new ArrayList<String>();
453 ArrayList<String> localPaths = new ArrayList<String>();
454 remotePaths.addAll(mForgottenLocalFiles.keySet());
455 localPaths.addAll(mForgottenLocalFiles.values());
456 explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_LOCAL_PATHS, localPaths);
457 explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_REMOTE_PATHS, remotePaths);
458 explanationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
459
460 notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), explanationIntent, 0);
461 notification.setLatestEventInfo(getContext().getApplicationContext(),
462 getContext().getString(R.string.sync_foreign_files_forgotten_ticker),
463 String.format(getContext().getString(R.string.sync_foreign_files_forgotten_content), mForgottenLocalFiles.size(), getContext().getString(R.string.app_name)),
464 notification.contentIntent);
465 ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_foreign_files_forgotten_ticker, notification);
466
467 }
468
469
470 }