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
.Log_OC
;
30 import com
.owncloud
.android
.MainApp
;
31 import com
.owncloud
.android
.R
;
32 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
33 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
34 import com
.owncloud
.android
.datamodel
.OCFile
;
35 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
36 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
37 import com
.owncloud
.android
.operations
.UpdateOCVersionOperation
;
38 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
39 import com
.owncloud
.android
.ui
.activity
.ErrorsWhileCopyingHandlerActivity
;
41 import android
.accounts
.Account
;
42 import android
.accounts
.AccountsException
;
43 import android
.app
.Notification
;
44 import android
.app
.NotificationManager
;
45 import android
.app
.PendingIntent
;
46 import android
.content
.AbstractThreadedSyncAdapter
;
47 import android
.content
.ContentProviderClient
;
48 import android
.content
.ContentResolver
;
49 import android
.content
.Context
;
50 import android
.content
.Intent
;
51 import android
.content
.SyncResult
;
52 import android
.os
.Bundle
;
55 * Implementation of {@link AbstractThreadedSyncAdapter} responsible for synchronizing
58 * Performs a full synchronization of the account recieved in {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}.
60 * @author Bartek Przybylski
61 * @author David A. Velasco
63 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
65 private final static String TAG
= FileSyncAdapter
.class.getSimpleName();
67 /** Maximum number of failed folder synchronizations that are supported before finishing the synchronization operation */
68 private static final int MAX_FAILED_RESULTS
= 3;
71 /** Time stamp for the current synchronization process, used to distinguish fresh data */
72 private long mCurrentSyncTime
;
74 /** Flag made 'true' when a request to cancel the synchronization is received */
75 private boolean mCancellation
;
77 /** When 'true' the process was requested by the user through the user interface; when 'false', it was requested automatically by the system */
78 private boolean mIsManualSync
;
80 /** Counter for failed operations in the synchronization process */
81 private int mFailedResultsCounter
;
83 /** Result of the last failed operation */
84 private RemoteOperationResult mLastFailedResult
;
86 /** Counter of conflicts found between local and remote files */
87 private int mConflictsFound
;
89 /** Counter of failed operations in synchronization of kept-in-sync files */
90 private int mFailsInFavouritesFound
;
92 /** 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 */
93 private Map
<String
, String
> mForgottenLocalFiles
;
95 /** {@link SyncResult} instance to return to the system when the synchronization finish */
96 private SyncResult mSyncResult
;
100 * Creates an {@link FileSyncAdapter}
104 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
105 super(context
, autoInitialize
);
113 public synchronized void onPerformSync(Account account
, Bundle extras
,
114 String authority
, ContentProviderClient provider
,
115 SyncResult syncResult
) {
117 mCancellation
= false
;
118 mIsManualSync
= extras
.getBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, false
);
119 mFailedResultsCounter
= 0;
120 mLastFailedResult
= null
;
122 mFailsInFavouritesFound
= 0;
123 mForgottenLocalFiles
= new HashMap
<String
, String
>();
124 mSyncResult
= syncResult
;
125 mSyncResult
.fullSyncRequested
= false
;
126 mSyncResult
.delayUntil
= 60*60*24; // avoid too many automatic synchronizations
128 this.setAccount(account
);
129 this.setContentProvider(provider
);
130 this.setStorageManager(new FileDataStorageManager(account
, provider
));
132 this.initClientForCurrentAccount();
133 } catch (IOException e
) {
134 /// the account is unknown for the Synchronization Manager, unreachable this context, or can not be authenticated; don't try this again
135 mSyncResult
.tooManyRetries
= true
;
136 notifyFailedSynchronization();
138 } catch (AccountsException e
) {
139 /// the account is unknown for the Synchronization Manager, unreachable this context, or can not be authenticated; don't try this again
140 mSyncResult
.tooManyRetries
= true
;
141 notifyFailedSynchronization();
145 Log_OC
.d(TAG
, "Synchronization of ownCloud account " + account
.name
+ " starting");
146 sendStickyBroadcast(true
, null
, null
); // message to signal the start of the synchronization to the UI
150 mCurrentSyncTime
= System
.currentTimeMillis();
151 if (!mCancellation
) {
152 synchronizeFolder(getStorageManager().getFileByPath(OCFile
.ROOT_PATH
));
155 Log_OC
.d(TAG
, "Leaving synchronization before synchronizing the root folder because cancelation request");
160 // it's important making this although very unexpected errors occur; that's the reason for the finally
162 if (mFailedResultsCounter
> 0 && mIsManualSync
) {
163 /// don't let the system synchronization manager retries MANUAL synchronizations
164 // (be careful: "MANUAL" currently includes the synchronization requested when a new account is created and when the user changes the current account)
165 mSyncResult
.tooManyRetries
= true
;
167 /// notify the user about the failure of MANUAL synchronization
168 notifyFailedSynchronization();
170 if (mConflictsFound
> 0 || mFailsInFavouritesFound
> 0) {
171 notifyFailsInFavourites();
173 if (mForgottenLocalFiles
.size() > 0) {
174 notifyForgottenLocalFiles();
176 sendStickyBroadcast(false
, null
, mLastFailedResult
); // message to signal the end to the UI
182 * Called by system SyncManager when a synchronization is required to be cancelled.
184 * Sets the mCancellation flag to 'true'. THe synchronization will be stopped later,
185 * before a new folder is fetched. Data of the last folder synchronized will be still
188 * See {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}
189 * and {@link #synchronizeFolder(String, long)}.
192 public void onSyncCanceled() {
193 Log_OC
.d(TAG
, "Synchronization of " + getAccount().name
+ " has been requested to cancel");
194 mCancellation
= true
;
195 super.onSyncCanceled();
200 * Updates the locally stored version value of the ownCloud server
202 private void updateOCVersion() {
203 UpdateOCVersionOperation update
= new UpdateOCVersionOperation(getAccount(), getContext());
204 RemoteOperationResult result
= update
.execute(getClient());
205 if (!result
.isSuccess()) {
206 mLastFailedResult
= result
;
212 * Synchronizes the list of files contained in a folder identified with its remote path.
214 * Fetches the list and properties of the files contained in the given folder, including their
215 * properties, and updates the local database with them.
217 * Enters in the child folders to synchronize their contents also, following a recursive
218 * depth first strategy.
220 * @param folder Folder to synchronize.
222 private void synchronizeFolder(OCFile folder
) {
224 if (mFailedResultsCounter
> MAX_FAILED_RESULTS
|| isFinisher(mLastFailedResult
))
229 long currentSyncTime,
230 boolean updateFolderProperties,
231 boolean syncFullAccount,
232 DataStorageManager dataStorageManager,
238 // folder synchronization
239 SynchronizeFolderOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
246 RemoteOperationResult result
= synchFolderOp
.execute(getClient());
249 // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
250 sendStickyBroadcast(true
, folder
.getRemotePath(), null
);
252 // check the result of synchronizing the folder
253 if (result
.isSuccess() || result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
255 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
256 mConflictsFound
+= synchFolderOp
.getConflictsFound();
257 mFailsInFavouritesFound
+= synchFolderOp
.getFailsInFavouritesFound();
259 if (synchFolderOp
.getForgottenLocalFiles().size() > 0) {
260 mForgottenLocalFiles
.putAll(synchFolderOp
.getForgottenLocalFiles());
262 if (result
.isSuccess()) {
263 // synchronize children folders
264 List
<OCFile
> children
= synchFolderOp
.getChildren();
265 fetchChildren(folder
, children
, synchFolderOp
.getRemoteFolderChanged()); // beware of the 'hidden' recursion here!
269 // in failures, the statistics for the global result are updated
270 if (result
.getCode() == RemoteOperationResult
.ResultCode
.UNAUTHORIZED
||
271 ( result
.isIdPRedirection() &&
272 MainApp
.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))) {
273 mSyncResult
.stats
.numAuthExceptions
++;
275 } else if (result
.getException() instanceof DavException
) {
276 mSyncResult
.stats
.numParseExceptions
++;
278 } else if (result
.getException() instanceof IOException
) {
279 mSyncResult
.stats
.numIoExceptions
++;
281 mFailedResultsCounter
++;
282 mLastFailedResult
= result
;
288 * Checks if a failed result should terminate the synchronization process immediately, according to
291 * @param failedResult Remote operation result to check.
292 * @return 'True' if the result should immediately finish the synchronization
294 private boolean isFinisher(RemoteOperationResult failedResult
) {
295 if (failedResult
!= null
) {
296 RemoteOperationResult
.ResultCode code
= failedResult
.getCode();
297 return (code
.equals(RemoteOperationResult
.ResultCode
.SSL_ERROR
) ||
298 code
.equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
) ||
299 code
.equals(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
) ||
300 code
.equals(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
));
306 * Triggers the synchronization of any folder contained in the list of received files.
308 * @param files Files to recursively synchronize.
310 private void fetchChildren(OCFile parent
, List
<OCFile
> files
, boolean parentEtagChanged
) {
312 OCFile newFile
= null
;
314 boolean syncDown
= false
;
315 for (i
=0; i
< files
.size() && !mCancellation
; i
++) {
316 newFile
= files
.get(i
);
317 if (newFile
.isFolder()) {
319 etag = newFile.getEtag();
320 syncDown = (parentEtagChanged || etag == null || etag.length() == 0);
322 synchronizeFolder(newFile
);
323 // update the size of the parent folder again after recursive synchronization
324 //getStorageManager().updateFolderSize(parent.getFileId());
325 sendStickyBroadcast(true
, parent
.getRemotePath(), null
); // notify again to refresh size in UI
330 if (mCancellation
&& i
<files
.size()) Log_OC
.d(TAG
, "Leaving synchronization before synchronizing " + files
.get(i
).getRemotePath() + " due to cancelation request");
335 * Sends a message to any application component interested in the progress of the synchronization.
337 * @param inProgress 'True' when the synchronization progress is not finished.
338 * @param dirRemotePath Remote path of a folder that was just synchronized (with or without success)
340 private void sendStickyBroadcast(boolean inProgress
, String dirRemotePath
, RemoteOperationResult result
) {
341 Intent i
= new Intent(FileSyncService
.getSyncMessage());
342 i
.putExtra(FileSyncService
.IN_PROGRESS
, inProgress
);
343 i
.putExtra(FileSyncService
.ACCOUNT_NAME
, getAccount().name
);
344 if (dirRemotePath
!= null
) {
345 i
.putExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
, dirRemotePath
);
347 if (result
!= null
) {
348 i
.putExtra(FileSyncService
.SYNC_RESULT
, result
);
350 getContext().sendStickyBroadcast(i
);
356 * Notifies the user about a failed synchronization through the status notification bar
358 private void notifyFailedSynchronization() {
359 Notification notification
= new Notification(R
.drawable
.icon
, getContext().getString(R
.string
.sync_fail_ticker
), System
.currentTimeMillis());
360 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
361 boolean needsToUpdateCredentials
= (mLastFailedResult
!= null
&&
362 ( mLastFailedResult
.getCode() == ResultCode
.UNAUTHORIZED
||
363 // (mLastFailedResult.isTemporalRedirection() && mLastFailedResult.isIdPRedirection() &&
364 ( mLastFailedResult
.isIdPRedirection() &&
365 MainApp
.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))
368 // TODO put something smart in the contentIntent below for all the possible errors
369 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), new Intent(), 0);
370 if (needsToUpdateCredentials
) {
371 // let the user update credentials with one click
372 Intent updateAccountCredentials
= new Intent(getContext(), AuthenticatorActivity
.class);
373 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, getAccount());
374 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ENFORCED_UPDATE
, true
);
375 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
376 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
377 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
378 updateAccountCredentials
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
379 notification
.contentIntent
= PendingIntent
.getActivity(getContext(), (int)System
.currentTimeMillis(), updateAccountCredentials
, PendingIntent
.FLAG_ONE_SHOT
);
380 notification
.setLatestEventInfo(getContext().getApplicationContext(),
381 getContext().getString(R
.string
.sync_fail_ticker
),
382 String
.format(getContext().getString(R
.string
.sync_fail_content_unauthorized
), getAccount().name
),
383 notification
.contentIntent
);
385 notification
.setLatestEventInfo(getContext().getApplicationContext(),
386 getContext().getString(R
.string
.sync_fail_ticker
),
387 String
.format(getContext().getString(R
.string
.sync_fail_content
), getAccount().name
),
388 notification
.contentIntent
);
390 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_fail_ticker
, notification
);
395 * Notifies the user about conflicts and strange fails when trying to synchronize the contents of kept-in-sync files.
397 * By now, we won't consider a failed synchronization.
399 private void notifyFailsInFavourites() {
400 if (mFailedResultsCounter
> 0) {
401 Notification notification
= new Notification(R
.drawable
.icon
, getContext().getString(R
.string
.sync_fail_in_favourites_ticker
), System
.currentTimeMillis());
402 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
403 // TODO put something smart in the contentIntent below
404 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), new Intent(), 0);
405 notification
.setLatestEventInfo(getContext().getApplicationContext(),
406 getContext().getString(R
.string
.sync_fail_in_favourites_ticker
),
407 String
.format(getContext().getString(R
.string
.sync_fail_in_favourites_content
), mFailedResultsCounter
+ mConflictsFound
, mConflictsFound
),
408 notification
.contentIntent
);
409 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_fail_in_favourites_ticker
, notification
);
412 Notification notification
= new Notification(R
.drawable
.icon
, getContext().getString(R
.string
.sync_conflicts_in_favourites_ticker
), System
.currentTimeMillis());
413 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
414 // TODO put something smart in the contentIntent below
415 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), new Intent(), 0);
416 notification
.setLatestEventInfo(getContext().getApplicationContext(),
417 getContext().getString(R
.string
.sync_conflicts_in_favourites_ticker
),
418 String
.format(getContext().getString(R
.string
.sync_conflicts_in_favourites_content
), mConflictsFound
),
419 notification
.contentIntent
);
420 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_conflicts_in_favourites_ticker
, notification
);
425 * Notifies the user about local copies of files out of the ownCloud local directory that were 'forgotten' because
426 * copying them inside the ownCloud local directory was not possible.
428 * We don't want links to files out of the ownCloud local directory (foreign files) anymore. It's easy to have
429 * synchronization problems if a local file is linked to more than one remote file.
431 * We won't consider a synchronization as failed when foreign files can not be copied to the ownCloud local directory.
433 private void notifyForgottenLocalFiles() {
434 Notification notification
= new Notification(R
.drawable
.icon
, getContext().getString(R
.string
.sync_foreign_files_forgotten_ticker
), System
.currentTimeMillis());
435 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
437 /// includes a pending intent in the notification showing a more detailed explanation
438 Intent explanationIntent
= new Intent(getContext(), ErrorsWhileCopyingHandlerActivity
.class);
439 explanationIntent
.putExtra(ErrorsWhileCopyingHandlerActivity
.EXTRA_ACCOUNT
, getAccount());
440 ArrayList
<String
> remotePaths
= new ArrayList
<String
>();
441 ArrayList
<String
> localPaths
= new ArrayList
<String
>();
442 remotePaths
.addAll(mForgottenLocalFiles
.keySet());
443 localPaths
.addAll(mForgottenLocalFiles
.values());
444 explanationIntent
.putExtra(ErrorsWhileCopyingHandlerActivity
.EXTRA_LOCAL_PATHS
, localPaths
);
445 explanationIntent
.putExtra(ErrorsWhileCopyingHandlerActivity
.EXTRA_REMOTE_PATHS
, remotePaths
);
446 explanationIntent
.setFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
448 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), explanationIntent
, 0);
449 notification
.setLatestEventInfo(getContext().getApplicationContext(),
450 getContext().getString(R
.string
.sync_foreign_files_forgotten_ticker
),
451 String
.format(getContext().getString(R
.string
.sync_foreign_files_forgotten_content
), mForgottenLocalFiles
.size(), getContext().getString(R
.string
.app_name
)),
452 notification
.contentIntent
);
453 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_foreign_files_forgotten_ticker
, notification
);