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