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