1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 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
.syncadapter
;
21 import java
.io
.IOException
;
22 import java
.util
.ArrayList
;
23 import java
.util
.HashMap
;
24 import java
.util
.List
;
27 import org
.apache
.jackrabbit
.webdav
.DavException
;
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
;
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
;
56 * Implementation of {@link AbstractThreadedSyncAdapter} responsible for synchronizing
59 * Performs a full synchronization of the account recieved in {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}.
61 * @author Bartek Przybylski
62 * @author David A. Velasco
64 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
66 private final static String TAG
= FileSyncAdapter
.class.getSimpleName();
68 /** Maximum number of failed folder synchronizations that are supported before finishing the synchronization operation */
69 private static final int MAX_FAILED_RESULTS
= 3;
72 /** Time stamp for the current synchronization process, used to distinguish fresh data */
73 private long mCurrentSyncTime
;
75 /** Flag made 'true' when a request to cancel the synchronization is received */
76 private boolean mCancellation
;
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
;
81 /** Counter for failed operations in the synchronization process */
82 private int mFailedResultsCounter
;
84 /** Result of the last failed operation */
85 private RemoteOperationResult mLastFailedResult
;
87 /** Counter of conflicts found between local and remote files */
88 private int mConflictsFound
;
90 /** Counter of failed operations in synchronization of kept-in-sync files */
91 private int mFailsInFavouritesFound
;
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
;
96 /** {@link SyncResult} instance to return to the system when the synchronization finish */
97 private SyncResult mSyncResult
;
101 * Creates a {@link FileSyncAdapter}
105 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
106 super(context
, autoInitialize
);
111 * Creates a {@link FileSyncAdapter}
115 public FileSyncAdapter(Context context
, boolean autoInitialize
, boolean allowParallelSyncs
) {
116 super(context
, autoInitialize
, allowParallelSyncs
);
124 public synchronized void onPerformSync(Account account
, Bundle extras
,
125 String authority
, ContentProviderClient providerClient
,
126 SyncResult syncResult
) {
128 mCancellation
= false
;
129 mIsManualSync
= extras
.getBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, false
);
130 mFailedResultsCounter
= 0;
131 mLastFailedResult
= null
;
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
139 this.setAccount(account
);
140 this.setContentProviderClient(providerClient
);
141 this.setStorageManager(new FileDataStorageManager(account
, providerClient
));
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();
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();
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
161 mCurrentSyncTime
= System
.currentTimeMillis();
162 if (!mCancellation
) {
163 synchronizeFolder(getStorageManager().getFileByPath(OCFile
.ROOT_PATH
));
166 Log_OC
.d(TAG
, "Leaving synchronization before synchronizing the root folder because cancelation request");
171 // it's important making this although very unexpected errors occur; that's the reason for the finally
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
;
178 /// notify the user about the failure of MANUAL synchronization
179 notifyFailedSynchronization();
181 if (mConflictsFound
> 0 || mFailsInFavouritesFound
> 0) {
182 notifyFailsInFavourites();
184 if (mForgottenLocalFiles
.size() > 0) {
185 notifyForgottenLocalFiles();
187 sendStickyBroadcast(false
, null
, mLastFailedResult
); // message to signal the end to the UI
193 * Called by system SyncManager when a synchronization is required to be cancelled.
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
199 * See {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}
200 * and {@link #synchronizeFolder(String, long)}.
203 public void onSyncCanceled() {
204 Log_OC
.d(TAG
, "Synchronization of " + getAccount().name
+ " has been requested to cancel");
205 mCancellation
= true
;
206 super.onSyncCanceled();
211 * Updates the locally stored version value of the ownCloud server
213 private void updateOCVersion() {
214 UpdateOCVersionOperation update
= new UpdateOCVersionOperation(getAccount(), getContext());
215 RemoteOperationResult result
= update
.execute(getClient());
216 if (!result
.isSuccess()) {
217 mLastFailedResult
= result
;
223 * Synchronizes the list of files contained in a folder identified with its remote path.
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.
228 * Enters in the child folders to synchronize their contents also, following a recursive
229 * depth first strategy.
231 * @param folder Folder to synchronize.
233 private void synchronizeFolder(OCFile folder
) {
235 if (mFailedResultsCounter
> MAX_FAILED_RESULTS
|| isFinisher(mLastFailedResult
))
240 long currentSyncTime,
241 boolean updateFolderProperties,
242 boolean syncFullAccount,
243 DataStorageManager dataStorageManager,
249 // folder synchronization
250 SynchronizeFolderOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
257 RemoteOperationResult result
= synchFolderOp
.execute(getClient());
260 // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
261 sendStickyBroadcast(true
, folder
.getRemotePath(), null
);
263 // check the result of synchronizing the folder
264 if (result
.isSuccess() || result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
266 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
267 mConflictsFound
+= synchFolderOp
.getConflictsFound();
268 mFailsInFavouritesFound
+= synchFolderOp
.getFailsInFavouritesFound();
270 if (synchFolderOp
.getForgottenLocalFiles().size() > 0) {
271 mForgottenLocalFiles
.putAll(synchFolderOp
.getForgottenLocalFiles());
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!
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
++;
287 } else if (result
.getException() instanceof DavException
) {
288 mSyncResult
.stats
.numParseExceptions
++;
290 } else if (result
.getException() instanceof IOException
) {
291 mSyncResult
.stats
.numIoExceptions
++;
293 mFailedResultsCounter
++;
294 mLastFailedResult
= result
;
300 * Checks if a failed result should terminate the synchronization process immediately, according to
303 * @param failedResult Remote operation result to check.
304 * @return 'True' if the result should immediately finish the synchronization
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
));
318 * Triggers the synchronization of any folder contained in the list of received files.
320 * @param files Files to recursively synchronize.
322 private void fetchChildren(OCFile parent
, List
<OCFile
> files
, boolean parentEtagChanged
) {
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()) {
331 etag = newFile.getEtag();
332 syncDown = (parentEtagChanged || etag == null || etag.length() == 0);
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
342 if (mCancellation
&& i
<files
.size()) Log_OC
.d(TAG
, "Leaving synchronization before synchronizing " + files
.get(i
).getRemotePath() + " due to cancelation request");
347 * Sends a message to any application component interested in the progress of the synchronization.
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)
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
);
359 if (result
!= null
) {
360 i
.putExtra(FileSyncService
.SYNC_RESULT
, result
);
362 getContext().sendStickyBroadcast(i
);
368 * Notifies the user about a failed synchronization through the status notification bar
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()))
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
);
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
);
402 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_fail_ticker
, notification
);
407 * Notifies the user about conflicts and strange fails when trying to synchronize the contents of kept-in-sync files.
409 * By now, we won't consider a failed synchronization.
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
);
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
);
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.
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.
443 * We won't consider a synchronization as failed when foreign files can not be copied to the ownCloud local directory.
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
;
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
);
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
);