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
.common
.operations
.RemoteOperationResult
;
34 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
35 import com
.owncloud
.android
.operations
.UpdateOCVersionOperation
;
36 import com
.owncloud
.android
.lib
.common
.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
;
54 //import android.support.v4.content.LocalBroadcastManager;
57 * Implementation of {@link AbstractThreadedSyncAdapter} responsible for synchronizing
60 * Performs a full synchronization of the account recieved in {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}.
62 * @author Bartek Przybylski
63 * @author David A. Velasco
65 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
67 private final static String TAG
= FileSyncAdapter
.class.getSimpleName();
69 /** Maximum number of failed folder synchronizations that are supported before finishing the synchronization operation */
70 private static final int MAX_FAILED_RESULTS
= 3;
73 public static final String EVENT_FULL_SYNC_START
= FileSyncAdapter
.class.getName() + ".EVENT_FULL_SYNC_START";
74 public static final String EVENT_FULL_SYNC_END
= FileSyncAdapter
.class.getName() + ".EVENT_FULL_SYNC_END";
75 public static final String EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
= FileSyncAdapter
.class.getName() + ".EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED";
76 //public static final String EVENT_FULL_SYNC_FOLDER_SIZE_SYNCED = FileSyncAdapter.class.getName() + ".EVENT_FULL_SYNC_FOLDER_SIZE_SYNCED";
78 public static final String EXTRA_ACCOUNT_NAME
= FileSyncAdapter
.class.getName() + ".EXTRA_ACCOUNT_NAME";
79 public static final String EXTRA_FOLDER_PATH
= FileSyncAdapter
.class.getName() + ".EXTRA_FOLDER_PATH";
80 public static final String EXTRA_RESULT
= FileSyncAdapter
.class.getName() + ".EXTRA_RESULT";
83 /** Time stamp for the current synchronization process, used to distinguish fresh data */
84 private long mCurrentSyncTime
;
86 /** Flag made 'true' when a request to cancel the synchronization is received */
87 private boolean mCancellation
;
89 /** When 'true' the process was requested by the user through the user interface; when 'false', it was requested automatically by the system */
90 private boolean mIsManualSync
;
92 /** Counter for failed operations in the synchronization process */
93 private int mFailedResultsCounter
;
95 /** Result of the last failed operation */
96 private RemoteOperationResult mLastFailedResult
;
98 /** Counter of conflicts found between local and remote files */
99 private int mConflictsFound
;
101 /** Counter of failed operations in synchronization of kept-in-sync files */
102 private int mFailsInFavouritesFound
;
104 /** 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 */
105 private Map
<String
, String
> mForgottenLocalFiles
;
107 /** {@link SyncResult} instance to return to the system when the synchronization finish */
108 private SyncResult mSyncResult
;
110 /** 'True' means that the server supports the share API */
111 private boolean mIsShareSupported
;
115 * Creates a {@link FileSyncAdapter}
119 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
120 super(context
, autoInitialize
);
125 * Creates a {@link FileSyncAdapter}
129 public FileSyncAdapter(Context context
, boolean autoInitialize
, boolean allowParallelSyncs
) {
130 super(context
, autoInitialize
, allowParallelSyncs
);
138 public synchronized void onPerformSync(Account account
, Bundle extras
,
139 String authority
, ContentProviderClient providerClient
,
140 SyncResult syncResult
) {
142 mCancellation
= false
;
143 mIsManualSync
= extras
.getBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, false
);
144 mFailedResultsCounter
= 0;
145 mLastFailedResult
= null
;
147 mFailsInFavouritesFound
= 0;
148 mForgottenLocalFiles
= new HashMap
<String
, String
>();
149 mSyncResult
= syncResult
;
150 mSyncResult
.fullSyncRequested
= false
;
151 mSyncResult
.delayUntil
= 60*60*24; // avoid too many automatic synchronizations
153 this.setAccount(account
);
154 this.setContentProviderClient(providerClient
);
155 this.setStorageManager(new FileDataStorageManager(account
, providerClient
));
158 this.initClientForCurrentAccount();
159 } catch (IOException e
) {
160 /// the account is unknown for the Synchronization Manager, unreachable this context, or can not be authenticated; don't try this again
161 mSyncResult
.tooManyRetries
= true
;
162 notifyFailedSynchronization();
164 } catch (AccountsException 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();
171 Log_OC
.d(TAG
, "Synchronization of ownCloud account " + account
.name
+ " starting");
172 sendLocalBroadcast(EVENT_FULL_SYNC_START
, null
, null
); // message to signal the start of the synchronization to the UI
176 mCurrentSyncTime
= System
.currentTimeMillis();
177 if (!mCancellation
) {
178 synchronizeFolder(getStorageManager().getFileByPath(OCFile
.ROOT_PATH
));
181 Log_OC
.d(TAG
, "Leaving synchronization before synchronizing the root folder because cancelation request");
186 // it's important making this although very unexpected errors occur; that's the reason for the finally
188 if (mFailedResultsCounter
> 0 && mIsManualSync
) {
189 /// don't let the system synchronization manager retries MANUAL synchronizations
190 // (be careful: "MANUAL" currently includes the synchronization requested when a new account is created and when the user changes the current account)
191 mSyncResult
.tooManyRetries
= true
;
193 /// notify the user about the failure of MANUAL synchronization
194 notifyFailedSynchronization();
196 if (mConflictsFound
> 0 || mFailsInFavouritesFound
> 0) {
197 notifyFailsInFavourites();
199 if (mForgottenLocalFiles
.size() > 0) {
200 notifyForgottenLocalFiles();
202 sendLocalBroadcast(EVENT_FULL_SYNC_END
, null
, mLastFailedResult
); // message to signal the end to the UI
208 * Called by system SyncManager when a synchronization is required to be cancelled.
210 * Sets the mCancellation flag to 'true'. THe synchronization will be stopped later,
211 * before a new folder is fetched. Data of the last folder synchronized will be still
214 * See {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}
215 * and {@link #synchronizeFolder(String, long)}.
218 public void onSyncCanceled() {
219 Log_OC
.d(TAG
, "Synchronization of " + getAccount().name
+ " has been requested to cancel");
220 mCancellation
= true
;
221 super.onSyncCanceled();
226 * Updates the locally stored version value of the ownCloud server
228 private void updateOCVersion() {
229 UpdateOCVersionOperation update
= new UpdateOCVersionOperation(getAccount(), getContext());
230 RemoteOperationResult result
= update
.execute(getClient());
231 if (!result
.isSuccess()) {
232 mLastFailedResult
= result
;
234 mIsShareSupported
= update
.getOCVersion().isSharedSupported();
240 * Synchronizes the list of files contained in a folder identified with its remote path.
242 * Fetches the list and properties of the files contained in the given folder, including their
243 * properties, and updates the local database with them.
245 * Enters in the child folders to synchronize their contents also, following a recursive
246 * depth first strategy.
248 * @param folder Folder to synchronize.
250 private void synchronizeFolder(OCFile folder
) {
252 if (mFailedResultsCounter
> MAX_FAILED_RESULTS
|| isFinisher(mLastFailedResult
))
257 long currentSyncTime,
258 boolean updateFolderProperties,
259 boolean syncFullAccount,
260 DataStorageManager dataStorageManager,
265 // folder synchronization
266 SynchronizeFolderOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
274 RemoteOperationResult result
= synchFolderOp
.execute(getClient());
277 // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
278 sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
, folder
.getRemotePath(), result
);
280 // check the result of synchronizing the folder
281 if (result
.isSuccess() || result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
283 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
284 mConflictsFound
+= synchFolderOp
.getConflictsFound();
285 mFailsInFavouritesFound
+= synchFolderOp
.getFailsInFavouritesFound();
287 if (synchFolderOp
.getForgottenLocalFiles().size() > 0) {
288 mForgottenLocalFiles
.putAll(synchFolderOp
.getForgottenLocalFiles());
290 if (result
.isSuccess()) {
291 // synchronize children folders
292 List
<OCFile
> children
= synchFolderOp
.getChildren();
293 fetchChildren(folder
, children
, synchFolderOp
.getRemoteFolderChanged()); // beware of the 'hidden' recursion here!
297 // in failures, the statistics for the global result are updated
298 if (result
.getCode() == RemoteOperationResult
.ResultCode
.UNAUTHORIZED
||
299 ( result
.isIdPRedirection() &&
300 getClient().getCredentials() == null
)) {
301 //MainApp.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))) {
302 mSyncResult
.stats
.numAuthExceptions
++;
304 } else if (result
.getException() instanceof DavException
) {
305 mSyncResult
.stats
.numParseExceptions
++;
307 } else if (result
.getException() instanceof IOException
) {
308 mSyncResult
.stats
.numIoExceptions
++;
310 mFailedResultsCounter
++;
311 mLastFailedResult
= result
;
317 * Checks if a failed result should terminate the synchronization process immediately, according to
320 * @param failedResult Remote operation result to check.
321 * @return 'True' if the result should immediately finish the synchronization
323 private boolean isFinisher(RemoteOperationResult failedResult
) {
324 if (failedResult
!= null
) {
325 RemoteOperationResult
.ResultCode code
= failedResult
.getCode();
326 return (code
.equals(RemoteOperationResult
.ResultCode
.SSL_ERROR
) ||
327 code
.equals(RemoteOperationResult
.ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
) ||
328 code
.equals(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
) ||
329 code
.equals(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
));
335 * Triggers the synchronization of any folder contained in the list of received files.
337 * @param files Files to recursively synchronize.
339 private void fetchChildren(OCFile parent
, List
<OCFile
> files
, boolean parentEtagChanged
) {
341 OCFile newFile
= null
;
342 //String etag = null;
343 //boolean syncDown = false;
344 for (i
=0; i
< files
.size() && !mCancellation
; i
++) {
345 newFile
= files
.get(i
);
346 if (newFile
.isFolder()) {
348 etag = newFile.getEtag();
349 syncDown = (parentEtagChanged || etag == null || etag.length() == 0);
351 synchronizeFolder(newFile
);
352 //sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_SIZE_SYNCED, parent.getRemotePath(), null);
357 if (mCancellation
&& i
<files
.size()) Log_OC
.d(TAG
, "Leaving synchronization before synchronizing " + files
.get(i
).getRemotePath() + " due to cancelation request");
362 * Sends a message to any application component interested in the progress of the synchronization.
364 * @param event Event in the process of synchronization to be notified.
365 * @param dirRemotePath Remote path of the folder target of the event occurred.
366 * @param result Result of an individual {@ SynchronizeFolderOperation}, if completed; may be null.
368 private void sendLocalBroadcast(String event
, String dirRemotePath
, RemoteOperationResult result
) {
369 Log_OC
.d(TAG
, "Send broadcast " + event
);
370 Intent intent
= new Intent(event
);
371 intent
.putExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
, getAccount().name
);
372 if (dirRemotePath
!= null
) {
373 intent
.putExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
, dirRemotePath
);
375 if (result
!= null
) {
376 intent
.putExtra(FileSyncAdapter
.EXTRA_RESULT
, result
);
378 getContext().sendStickyBroadcast(intent
);
379 //LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
385 * Notifies the user about a failed synchronization through the status notification bar
387 private void notifyFailedSynchronization() {
388 Notification notification
= new Notification(DisplayUtils
.getSeasonalIconId(), getContext().getString(R
.string
.sync_fail_ticker
), System
.currentTimeMillis());
389 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
390 boolean needsToUpdateCredentials
= (mLastFailedResult
!= null
&&
391 ( mLastFailedResult
.getCode() == ResultCode
.UNAUTHORIZED
||
392 ( mLastFailedResult
.isIdPRedirection() &&
393 getClient().getCredentials() == null
)
394 //MainApp.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))
397 // TODO put something smart in the contentIntent below for all the possible errors
398 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), new Intent(), 0);
399 if (needsToUpdateCredentials
) {
400 // let the user update credentials with one click
401 Intent updateAccountCredentials
= new Intent(getContext(), AuthenticatorActivity
.class);
402 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, getAccount());
403 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ENFORCED_UPDATE
, true
);
404 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, AuthenticatorActivity
.ACTION_UPDATE_TOKEN
);
405 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
406 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
407 updateAccountCredentials
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
408 notification
.contentIntent
= PendingIntent
.getActivity(getContext(), (int)System
.currentTimeMillis(), updateAccountCredentials
, PendingIntent
.FLAG_ONE_SHOT
);
409 notification
.setLatestEventInfo(getContext().getApplicationContext(),
410 getContext().getString(R
.string
.sync_fail_ticker
),
411 String
.format(getContext().getString(R
.string
.sync_fail_content_unauthorized
), getAccount().name
),
412 notification
.contentIntent
);
414 notification
.setLatestEventInfo(getContext().getApplicationContext(),
415 getContext().getString(R
.string
.sync_fail_ticker
),
416 String
.format(getContext().getString(R
.string
.sync_fail_content
), getAccount().name
),
417 notification
.contentIntent
);
419 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_fail_ticker
, notification
);
424 * Notifies the user about conflicts and strange fails when trying to synchronize the contents of kept-in-sync files.
426 * By now, we won't consider a failed synchronization.
428 private void notifyFailsInFavourites() {
429 if (mFailedResultsCounter
> 0) {
430 Notification notification
= new Notification(DisplayUtils
.getSeasonalIconId(), getContext().getString(R
.string
.sync_fail_in_favourites_ticker
), System
.currentTimeMillis());
431 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
432 // TODO put something smart in the contentIntent below
433 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), new Intent(), 0);
434 notification
.setLatestEventInfo(getContext().getApplicationContext(),
435 getContext().getString(R
.string
.sync_fail_in_favourites_ticker
),
436 String
.format(getContext().getString(R
.string
.sync_fail_in_favourites_content
), mFailedResultsCounter
+ mConflictsFound
, mConflictsFound
),
437 notification
.contentIntent
);
438 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_fail_in_favourites_ticker
, notification
);
441 Notification notification
= new Notification(DisplayUtils
.getSeasonalIconId(), getContext().getString(R
.string
.sync_conflicts_in_favourites_ticker
), System
.currentTimeMillis());
442 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
443 // TODO put something smart in the contentIntent below
444 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), new Intent(), 0);
445 notification
.setLatestEventInfo(getContext().getApplicationContext(),
446 getContext().getString(R
.string
.sync_conflicts_in_favourites_ticker
),
447 String
.format(getContext().getString(R
.string
.sync_conflicts_in_favourites_content
), mConflictsFound
),
448 notification
.contentIntent
);
449 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_conflicts_in_favourites_ticker
, notification
);
454 * Notifies the user about local copies of files out of the ownCloud local directory that were 'forgotten' because
455 * copying them inside the ownCloud local directory was not possible.
457 * We don't want links to files out of the ownCloud local directory (foreign files) anymore. It's easy to have
458 * synchronization problems if a local file is linked to more than one remote file.
460 * We won't consider a synchronization as failed when foreign files can not be copied to the ownCloud local directory.
462 private void notifyForgottenLocalFiles() {
463 Notification notification
= new Notification(DisplayUtils
.getSeasonalIconId(), getContext().getString(R
.string
.sync_foreign_files_forgotten_ticker
), System
.currentTimeMillis());
464 notification
.flags
|= Notification
.FLAG_AUTO_CANCEL
;
466 /// includes a pending intent in the notification showing a more detailed explanation
467 Intent explanationIntent
= new Intent(getContext(), ErrorsWhileCopyingHandlerActivity
.class);
468 explanationIntent
.putExtra(ErrorsWhileCopyingHandlerActivity
.EXTRA_ACCOUNT
, getAccount());
469 ArrayList
<String
> remotePaths
= new ArrayList
<String
>();
470 ArrayList
<String
> localPaths
= new ArrayList
<String
>();
471 remotePaths
.addAll(mForgottenLocalFiles
.keySet());
472 localPaths
.addAll(mForgottenLocalFiles
.values());
473 explanationIntent
.putExtra(ErrorsWhileCopyingHandlerActivity
.EXTRA_LOCAL_PATHS
, localPaths
);
474 explanationIntent
.putExtra(ErrorsWhileCopyingHandlerActivity
.EXTRA_REMOTE_PATHS
, remotePaths
);
475 explanationIntent
.setFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
477 notification
.contentIntent
= PendingIntent
.getActivity(getContext().getApplicationContext(), (int)System
.currentTimeMillis(), explanationIntent
, 0);
478 notification
.setLatestEventInfo(getContext().getApplicationContext(),
479 getContext().getString(R
.string
.sync_foreign_files_forgotten_ticker
),
480 String
.format(getContext().getString(R
.string
.sync_foreign_files_forgotten_content
), mForgottenLocalFiles
.size(), getContext().getString(R
.string
.app_name
)),
481 notification
.contentIntent
);
482 ((NotificationManager
) getContext().getSystemService(Context
.NOTIFICATION_SERVICE
)).notify(R
.string
.sync_foreign_files_forgotten_ticker
, notification
);