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
;
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 an {@link FileSyncAdapter}
105 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
106 super(context
, autoInitialize
);
114 public synchronized void onPerformSync(Account account
, Bundle extras
,
115 String authority
, ContentProviderClient provider
,
116 SyncResult syncResult
) {
118 mCancellation
= false
;
119 mIsManualSync
= extras
.getBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, false
);
120 mFailedResultsCounter
= 0;
121 mLastFailedResult
= null
;
123 mFailsInFavouritesFound
= 0;
124 mForgottenLocalFiles
= new HashMap
<String
, String
>();
125 mSyncResult
= syncResult
;
126 mSyncResult
.fullSyncRequested
= false
;
127 mSyncResult
.delayUntil
= 60*60*24; // avoid too many automatic synchronizations
129 this.setAccount(account
);
130 this.setContentProvider(provider
);
131 this.setStorageManager(new FileDataStorageManager(account
, provider
));
133 this.initClientForCurrentAccount();
134 } catch (IOException e
) {
135 /// the account is unknown for the Synchronization Manager, unreachable this context, or can not be authenticated; don't try this again
136 mSyncResult
.tooManyRetries
= true
;
137 notifyFailedSynchronization();
139 } catch (AccountsException e
) {
140 /// the account is unknown for the Synchronization Manager, unreachable this context, or can not be authenticated; don't try this again
141 mSyncResult
.tooManyRetries
= true
;
142 notifyFailedSynchronization();
146 Log_OC
.d(TAG
, "Synchronization of ownCloud account " + account
.name
+ " starting");
147 sendStickyBroadcast(true
, null
, null
); // message to signal the start of the synchronization to the UI
151 mCurrentSyncTime
= System
.currentTimeMillis();
152 if (!mCancellation
) {
153 synchronizeFolder(getStorageManager().getFileByPath(OCFile
.ROOT_PATH
));
156 Log_OC
.d(TAG
, "Leaving synchronization before synchronizing the root folder because cancelation request");
161 // it's important making this although very unexpected errors occur; that's the reason for the finally
163 if (mFailedResultsCounter
> 0 && mIsManualSync
) {
164 /// don't let the system synchronization manager retries MANUAL synchronizations
165 // (be careful: "MANUAL" currently includes the synchronization requested when a new account is created and when the user changes the current account)
166 mSyncResult
.tooManyRetries
= true
;
168 /// notify the user about the failure of MANUAL synchronization
169 notifyFailedSynchronization();
171 if (mConflictsFound
> 0 || mFailsInFavouritesFound
> 0) {
172 notifyFailsInFavourites();
174 if (mForgottenLocalFiles
.size() > 0) {
175 notifyForgottenLocalFiles();
177 sendStickyBroadcast(false
, null
, mLastFailedResult
); // message to signal the end to the UI
183 * Called by system SyncManager when a synchronization is required to be cancelled.
185 * Sets the mCancellation flag to 'true'. THe synchronization will be stopped later,
186 * before a new folder is fetched. Data of the last folder synchronized will be still
189 * See {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}
190 * and {@link #synchronizeFolder(String, long)}.
193 public void onSyncCanceled() {
194 Log_OC
.d(TAG
, "Synchronization of " + getAccount().name
+ " has been requested to cancel");
195 mCancellation
= true
;
196 super.onSyncCanceled();
201 * Updates the locally stored version value of the ownCloud server
203 private void updateOCVersion() {
204 UpdateOCVersionOperation update
= new UpdateOCVersionOperation(getAccount(), getContext());
205 RemoteOperationResult result
= update
.execute(getClient());
206 if (!result
.isSuccess()) {
207 mLastFailedResult
= result
;
213 * Synchronizes the list of files contained in a folder identified with its remote path.
215 * Fetches the list and properties of the files contained in the given folder, including their
216 * properties, and updates the local database with them.
218 * Enters in the child folders to synchronize their contents also, following a recursive
219 * depth first strategy.
221 * @param folder Folder to synchronize.
223 private void synchronizeFolder(OCFile folder
) {
225 if (mFailedResultsCounter
> MAX_FAILED_RESULTS
|| isFinisher(mLastFailedResult
))
230 long currentSyncTime,
231 boolean updateFolderProperties,
232 boolean syncFullAccount,
233 DataStorageManager dataStorageManager,
239 // folder synchronization
240 SynchronizeFolderOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
247 RemoteOperationResult result
= synchFolderOp
.execute(getClient());
250 // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
251 sendStickyBroadcast(true
, folder
.getRemotePath(), null
);
253 // check the result of synchronizing the folder
254 if (result
.isSuccess() || result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
256 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
257 mConflictsFound
+= synchFolderOp
.getConflictsFound();
258 mFailsInFavouritesFound
+= synchFolderOp
.getFailsInFavouritesFound();
260 if (synchFolderOp
.getForgottenLocalFiles().size() > 0) {
261 mForgottenLocalFiles
.putAll(synchFolderOp
.getForgottenLocalFiles());
263 if (result
.isSuccess()) {
264 // synchronize children folders
265 List
<OCFile
> children
= synchFolderOp
.getChildren();
266 fetchChildren(folder
, children
, synchFolderOp
.getRemoteFolderChanged()); // beware of the 'hidden' recursion here!
270 // in failures, the statistics for the global result are updated
271 if (result
.getCode() == RemoteOperationResult
.ResultCode
.UNAUTHORIZED
||
272 ( result
.isIdPRedirection() &&
273 MainApp
.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))) {
274 mSyncResult
.stats
.numAuthExceptions
++;
276 } else if (result
.getException() instanceof DavException
) {
277 mSyncResult
.stats
.numParseExceptions
++;
279 } else if (result
.getException() instanceof IOException
) {
280 mSyncResult
.stats
.numIoExceptions
++;
282 mFailedResultsCounter
++;
283 mLastFailedResult
= result
;
289 * Checks if a failed result should terminate the synchronization process immediately, according to
292 * @param failedResult Remote operation result to check.
293 * @return 'True' if the result should immediately finish the synchronization
295 private boolean isFinisher(RemoteOperationResult failedResult
) {
296 if (failedResult
!= null
) {
297 RemoteOperationResult
.ResultCode code
= failedResult
.getCode();
298 return (code
.equals(RemoteOperationResult
.ResultCode
.SSL_ERROR
) ||
299 code
.equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
) ||
300 code
.equals(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
) ||
301 code
.equals(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
));
307 * Triggers the synchronization of any folder contained in the list of received files.
309 * @param files Files to recursively synchronize.
311 private void fetchChildren(OCFile parent
, List
<OCFile
> files
, boolean parentEtagChanged
) {
313 OCFile newFile
= null
;
315 boolean syncDown
= false
;
316 for (i
=0; i
< files
.size() && !mCancellation
; i
++) {
317 newFile
= files
.get(i
);
318 if (newFile
.isFolder()) {
320 etag = newFile.getEtag();
321 syncDown = (parentEtagChanged || etag == null || etag.length() == 0);
323 synchronizeFolder(newFile
);
324 // update the size of the parent folder again after recursive synchronization
325 //getStorageManager().updateFolderSize(parent.getFileId());
326 sendStickyBroadcast(true
, parent
.getRemotePath(), null
); // notify again to refresh size in UI
331 if (mCancellation
&& i
<files
.size()) Log_OC
.d(TAG
, "Leaving synchronization before synchronizing " + files
.get(i
).getRemotePath() + " due to cancelation request");
336 * Sends a message to any application component interested in the progress of the synchronization.
338 * @param inProgress 'True' when the synchronization progress is not finished.
339 * @param dirRemotePath Remote path of a folder that was just synchronized (with or without success)
341 private void sendStickyBroadcast(boolean inProgress
, String dirRemotePath
, RemoteOperationResult result
) {
342 Intent i
= new Intent(FileSyncService
.getSyncMessage());
343 i
.putExtra(FileSyncService
.IN_PROGRESS
, inProgress
);
344 i
.putExtra(FileSyncService
.ACCOUNT_NAME
, getAccount().name
);
345 if (dirRemotePath
!= null
) {
346 i
.putExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
, dirRemotePath
);
348 if (result
!= null
) {
349 i
.putExtra(FileSyncService
.SYNC_RESULT
, result
);
351 getContext().sendStickyBroadcast(i
);
357 * Notifies the user about a failed synchronization through the status notification bar
359 private void notifyFailedSynchronization() {
360 Notification notification
= new Notification(R
.drawable
.icon
, getContext().getString(R
.string
.sync_fail_ticker
), System
.currentTimeMillis());
361 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
362 boolean needsToUpdateCredentials
= (mLastFailedResult
!= null
&&
363 ( mLastFailedResult
.getCode() == ResultCode
.UNAUTHORIZED
||
364 // (mLastFailedResult.isTemporalRedirection() && mLastFailedResult.isIdPRedirection() &&
365 ( mLastFailedResult
.isIdPRedirection() &&
366 MainApp
.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))
369 // TODO put something smart in the contentIntent below for all the possible errors
370 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), new Intent(), 0);
371 if (needsToUpdateCredentials
) {
372 // let the user update credentials with one click
373 Intent updateAccountCredentials
= new Intent(getContext(), AuthenticatorActivity
.class);
374 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, getAccount());
375 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ENFORCED_UPDATE
, true
);
376 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
377 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
378 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
379 updateAccountCredentials
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
380 notification
.contentIntent
= PendingIntent
.getActivity(getContext(), (int)System
.currentTimeMillis(), updateAccountCredentials
, PendingIntent
.FLAG_ONE_SHOT
);
381 notification
.setLatestEventInfo(getContext().getApplicationContext(),
382 getContext().getString(R
.string
.sync_fail_ticker
),
383 String
.format(getContext().getString(R
.string
.sync_fail_content_unauthorized
), getAccount().name
),
384 notification
.contentIntent
);
386 notification
.setLatestEventInfo(getContext().getApplicationContext(),
387 getContext().getString(R
.string
.sync_fail_ticker
),
388 String
.format(getContext().getString(R
.string
.sync_fail_content
), getAccount().name
),
389 notification
.contentIntent
);
391 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_fail_ticker
, notification
);
396 * Notifies the user about conflicts and strange fails when trying to synchronize the contents of kept-in-sync files.
398 * By now, we won't consider a failed synchronization.
400 private void notifyFailsInFavourites() {
401 if (mFailedResultsCounter
> 0) {
402 Notification notification
= new Notification(R
.drawable
.icon
, getContext().getString(R
.string
.sync_fail_in_favourites_ticker
), System
.currentTimeMillis());
403 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
404 // TODO put something smart in the contentIntent below
405 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), new Intent(), 0);
406 notification
.setLatestEventInfo(getContext().getApplicationContext(),
407 getContext().getString(R
.string
.sync_fail_in_favourites_ticker
),
408 String
.format(getContext().getString(R
.string
.sync_fail_in_favourites_content
), mFailedResultsCounter
+ mConflictsFound
, mConflictsFound
),
409 notification
.contentIntent
);
410 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_fail_in_favourites_ticker
, notification
);
413 Notification notification
= new Notification(R
.drawable
.icon
, getContext().getString(R
.string
.sync_conflicts_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_conflicts_in_favourites_ticker
),
419 String
.format(getContext().getString(R
.string
.sync_conflicts_in_favourites_content
), mConflictsFound
),
420 notification
.contentIntent
);
421 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_conflicts_in_favourites_ticker
, notification
);
426 * Notifies the user about local copies of files out of the ownCloud local directory that were 'forgotten' because
427 * copying them inside the ownCloud local directory was not possible.
429 * We don't want links to files out of the ownCloud local directory (foreign files) anymore. It's easy to have
430 * synchronization problems if a local file is linked to more than one remote file.
432 * We won't consider a synchronization as failed when foreign files can not be copied to the ownCloud local directory.
434 private void notifyForgottenLocalFiles() {
435 Notification notification
= new Notification(R
.drawable
.icon
, getContext().getString(R
.string
.sync_foreign_files_forgotten_ticker
), System
.currentTimeMillis());
436 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
438 /// includes a pending intent in the notification showing a more detailed explanation
439 Intent explanationIntent
= new Intent(getContext(), ErrorsWhileCopyingHandlerActivity
.class);
440 explanationIntent
.putExtra(ErrorsWhileCopyingHandlerActivity
.EXTRA_ACCOUNT
, getAccount());
441 ArrayList
<String
> remotePaths
= new ArrayList
<String
>();
442 ArrayList
<String
> localPaths
= new ArrayList
<String
>();
443 remotePaths
.addAll(mForgottenLocalFiles
.keySet());
444 localPaths
.addAll(mForgottenLocalFiles
.values());
445 explanationIntent
.putExtra(ErrorsWhileCopyingHandlerActivity
.EXTRA_LOCAL_PATHS
, localPaths
);
446 explanationIntent
.putExtra(ErrorsWhileCopyingHandlerActivity
.EXTRA_REMOTE_PATHS
, remotePaths
);
447 explanationIntent
.setFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
449 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), explanationIntent
, 0);
450 notification
.setLatestEventInfo(getContext().getApplicationContext(),
451 getContext().getString(R
.string
.sync_foreign_files_forgotten_ticker
),
452 String
.format(getContext().getString(R
.string
.sync_foreign_files_forgotten_content
), mForgottenLocalFiles
.size(), getContext().getString(R
.string
.app_name
)),
453 notification
.contentIntent
);
454 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_foreign_files_forgotten_ticker
, notification
);