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
.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
;
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;
59 * Implementation of {@link AbstractThreadedSyncAdapter} responsible for synchronizing
62 * Performs a full synchronization of the account recieved in {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}.
64 * @author Bartek Przybylski
65 * @author David A. Velasco
67 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
69 private final static String TAG
= FileSyncAdapter
.class.getSimpleName();
71 /** Maximum number of failed folder synchronizations that are supported before finishing the synchronization operation */
72 private static final int MAX_FAILED_RESULTS
= 3;
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";
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";
85 /** Time stamp for the current synchronization process, used to distinguish fresh data */
86 private long mCurrentSyncTime
;
88 /** Flag made 'true' when a request to cancel the synchronization is received */
89 private boolean mCancellation
;
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
;
94 /** Counter for failed operations in the synchronization process */
95 private int mFailedResultsCounter
;
97 /** Result of the last failed operation */
98 private RemoteOperationResult mLastFailedResult
;
100 /** Counter of conflicts found between local and remote files */
101 private int mConflictsFound
;
103 /** Counter of failed operations in synchronization of kept-in-sync files */
104 private int mFailsInFavouritesFound
;
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
;
109 /** {@link SyncResult} instance to return to the system when the synchronization finish */
110 private SyncResult mSyncResult
;
112 /** 'True' means that the server supports the share API */
113 private boolean mIsSharedSupported
;
117 * Creates a {@link FileSyncAdapter}
121 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
122 super(context
, autoInitialize
);
127 * Creates a {@link FileSyncAdapter}
131 public FileSyncAdapter(Context context
, boolean autoInitialize
, boolean allowParallelSyncs
) {
132 super(context
, autoInitialize
, allowParallelSyncs
);
140 public synchronized void onPerformSync(Account account
, Bundle extras
,
141 String authority
, ContentProviderClient providerClient
,
142 SyncResult syncResult
) {
144 mCancellation
= false
;
145 mIsManualSync
= extras
.getBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, false
);
146 mFailedResultsCounter
= 0;
147 mLastFailedResult
= null
;
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
155 this.setAccount(account
);
156 this.setContentProviderClient(providerClient
);
157 this.setStorageManager(new FileDataStorageManager(account
, providerClient
));
159 AccountManager accountManager
= getAccountManager();
160 mIsSharedSupported
= Boolean
.parseBoolean(accountManager
.getUserData(account
, OwnCloudAccount
.Constants
.KEY_SUPPORTS_SHARE_API
));
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();
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();
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
181 mCurrentSyncTime
= System
.currentTimeMillis();
182 if (!mCancellation
) {
183 synchronizeFolder(getStorageManager().getFileByPath(OCFile
.ROOT_PATH
));
186 Log_OC
.d(TAG
, "Leaving synchronization before synchronizing the root folder because cancelation request");
191 // it's important making this although very unexpected errors occur; that's the reason for the finally
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
;
198 /// notify the user about the failure of MANUAL synchronization
199 notifyFailedSynchronization();
201 if (mConflictsFound
> 0 || mFailsInFavouritesFound
> 0) {
202 notifyFailsInFavourites();
204 if (mForgottenLocalFiles
.size() > 0) {
205 notifyForgottenLocalFiles();
207 sendLocalBroadcast(EVENT_FULL_SYNC_END
, null
, mLastFailedResult
); // message to signal the end to the UI
213 * Called by system SyncManager when a synchronization is required to be cancelled.
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
219 * See {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}
220 * and {@link #synchronizeFolder(String, long)}.
223 public void onSyncCanceled() {
224 Log_OC
.d(TAG
, "Synchronization of " + getAccount().name
+ " has been requested to cancel");
225 mCancellation
= true
;
226 super.onSyncCanceled();
231 * Updates the locally stored version value of the ownCloud server
233 private void updateOCVersion() {
234 UpdateOCVersionOperation update
= new UpdateOCVersionOperation(getAccount(), getContext());
235 RemoteOperationResult result
= update
.execute(getClient());
236 if (!result
.isSuccess()) {
237 mLastFailedResult
= result
;
243 * Synchronizes the list of files contained in a folder identified with its remote path.
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.
248 * Enters in the child folders to synchronize their contents also, following a recursive
249 * depth first strategy.
251 * @param folder Folder to synchronize.
253 private void synchronizeFolder(OCFile folder
) {
255 if (mFailedResultsCounter
> MAX_FAILED_RESULTS
|| isFinisher(mLastFailedResult
))
260 long currentSyncTime,
261 boolean updateFolderProperties,
262 boolean syncFullAccount,
263 DataStorageManager dataStorageManager,
268 // folder synchronization
269 SynchronizeFolderOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
277 RemoteOperationResult result
= synchFolderOp
.execute(getClient());
280 // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
281 sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
, folder
.getRemotePath(), result
);
283 // check the result of synchronizing the folder
284 if (result
.isSuccess() || result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
286 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
287 mConflictsFound
+= synchFolderOp
.getConflictsFound();
288 mFailsInFavouritesFound
+= synchFolderOp
.getFailsInFavouritesFound();
290 if (synchFolderOp
.getForgottenLocalFiles().size() > 0) {
291 mForgottenLocalFiles
.putAll(synchFolderOp
.getForgottenLocalFiles());
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!
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
++;
307 } else if (result
.getException() instanceof DavException
) {
308 mSyncResult
.stats
.numParseExceptions
++;
310 } else if (result
.getException() instanceof IOException
) {
311 mSyncResult
.stats
.numIoExceptions
++;
313 mFailedResultsCounter
++;
314 mLastFailedResult
= result
;
320 * Checks if a failed result should terminate the synchronization process immediately, according to
323 * @param failedResult Remote operation result to check.
324 * @return 'True' if the result should immediately finish the synchronization
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
));
338 * Triggers the synchronization of any folder contained in the list of received files.
340 * @param files Files to recursively synchronize.
342 private void fetchChildren(OCFile parent
, List
<OCFile
> files
, boolean parentEtagChanged
) {
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()) {
351 etag = newFile.getEtag();
352 syncDown = (parentEtagChanged || etag == null || etag.length() == 0);
354 synchronizeFolder(newFile
);
355 sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_SIZE_SYNCED
, parent
.getRemotePath(), null
);
360 if (mCancellation
&& i
<files
.size()) Log_OC
.d(TAG
, "Leaving synchronization before synchronizing " + files
.get(i
).getRemotePath() + " due to cancelation request");
365 * Sends a message to any application component interested in the progress of the synchronization.
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.
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
);
378 if (result
!= null
) {
379 intent
.putExtra(FileSyncAdapter
.EXTRA_RESULT
, result
);
381 getContext().sendStickyBroadcast(intent
);
382 //LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
388 * Notifies the user about a failed synchronization through the status notification bar
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()))
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
);
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
);
422 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_fail_ticker
, notification
);
427 * Notifies the user about conflicts and strange fails when trying to synchronize the contents of kept-in-sync files.
429 * By now, we won't consider a failed synchronization.
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
);
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
);
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.
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.
463 * We won't consider a synchronization as failed when foreign files can not be copied to the ownCloud local directory.
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
;
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
);
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
);