Merge pull request #294 from owncloud/setup_buttons
[pub/Android/ownCloud.git] / src / com / owncloud / android / syncadapter / FileSyncAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.syncadapter;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.jackrabbit.webdav.DavException;
28
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;
40
41
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
55 /**
56 * Implementation of {@link AbstractThreadedSyncAdapter} responsible for synchronizing
57 * ownCloud files.
58 *
59 * Performs a full synchronization of the account recieved in {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}.
60 *
61 * @author Bartek Przybylski
62 * @author David A. Velasco
63 */
64 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
65
66 private final static String TAG = FileSyncAdapter.class.getSimpleName();
67
68 /** Maximum number of failed folder synchronizations that are supported before finishing the synchronization operation */
69 private static final int MAX_FAILED_RESULTS = 3;
70
71
72 /** Time stamp for the current synchronization process, used to distinguish fresh data */
73 private long mCurrentSyncTime;
74
75 /** Flag made 'true' when a request to cancel the synchronization is received */
76 private boolean mCancellation;
77
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;
80
81 /** Counter for failed operations in the synchronization process */
82 private int mFailedResultsCounter;
83
84 /** Result of the last failed operation */
85 private RemoteOperationResult mLastFailedResult;
86
87 /** Counter of conflicts found between local and remote files */
88 private int mConflictsFound;
89
90 /** Counter of failed operations in synchronization of kept-in-sync files */
91 private int mFailsInFavouritesFound;
92
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;
95
96 /** {@link SyncResult} instance to return to the system when the synchronization finish */
97 private SyncResult mSyncResult;
98
99
100 /**
101 * Creates an {@link FileSyncAdapter}
102 *
103 * {@inheritDoc}
104 */
105 public FileSyncAdapter(Context context, boolean autoInitialize) {
106 super(context, autoInitialize);
107 }
108
109
110 /**
111 * {@inheritDoc}
112 */
113 @Override
114 public synchronized void onPerformSync(Account account, Bundle extras,
115 String authority, ContentProviderClient provider,
116 SyncResult syncResult) {
117
118 mCancellation = false;
119 mIsManualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
120 mFailedResultsCounter = 0;
121 mLastFailedResult = null;
122 mConflictsFound = 0;
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
128
129 this.setAccount(account);
130 this.setContentProvider(provider);
131 this.setStorageManager(new FileDataStorageManager(account, provider));
132 try {
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();
138 return;
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();
143 return;
144 }
145
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
148
149 try {
150 updateOCVersion();
151 mCurrentSyncTime = System.currentTimeMillis();
152 if (!mCancellation) {
153 synchronizeFolder(getStorageManager().getFileByPath(OCFile.ROOT_PATH));
154
155 } else {
156 Log_OC.d(TAG, "Leaving synchronization before synchronizing the root folder because cancelation request");
157 }
158
159
160 } finally {
161 // it's important making this although very unexpected errors occur; that's the reason for the finally
162
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;
167
168 /// notify the user about the failure of MANUAL synchronization
169 notifyFailedSynchronization();
170 }
171 if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
172 notifyFailsInFavourites();
173 }
174 if (mForgottenLocalFiles.size() > 0) {
175 notifyForgottenLocalFiles();
176 }
177 sendStickyBroadcast(false, null, mLastFailedResult); // message to signal the end to the UI
178 }
179
180 }
181
182 /**
183 * Called by system SyncManager when a synchronization is required to be cancelled.
184 *
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
187 * locally saved.
188 *
189 * See {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}
190 * and {@link #synchronizeFolder(String, long)}.
191 */
192 @Override
193 public void onSyncCanceled() {
194 Log_OC.d(TAG, "Synchronization of " + getAccount().name + " has been requested to cancel");
195 mCancellation = true;
196 super.onSyncCanceled();
197 }
198
199
200 /**
201 * Updates the locally stored version value of the ownCloud server
202 */
203 private void updateOCVersion() {
204 UpdateOCVersionOperation update = new UpdateOCVersionOperation(getAccount(), getContext());
205 RemoteOperationResult result = update.execute(getClient());
206 if (!result.isSuccess()) {
207 mLastFailedResult = result;
208 }
209 }
210
211
212 /**
213 * Synchronizes the list of files contained in a folder identified with its remote path.
214 *
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.
217 *
218 * Enters in the child folders to synchronize their contents also, following a recursive
219 * depth first strategy.
220 *
221 * @param folder Folder to synchronize.
222 */
223 private void synchronizeFolder(OCFile folder) {
224
225 if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult))
226 return;
227
228 /*
229 OCFile folder,
230 long currentSyncTime,
231 boolean updateFolderProperties,
232 boolean syncFullAccount,
233 DataStorageManager dataStorageManager,
234 Account account,
235 Context context ) {
236
237 }
238 */
239 // folder synchronization
240 SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation( folder,
241 mCurrentSyncTime,
242 true,
243 getStorageManager(),
244 getAccount(),
245 getContext()
246 );
247 RemoteOperationResult result = synchFolderOp.execute(getClient());
248
249
250 // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
251 sendStickyBroadcast(true, folder.getRemotePath(), null);
252
253 // check the result of synchronizing the folder
254 if (result.isSuccess() || result.getCode() == ResultCode.SYNC_CONFLICT) {
255
256 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
257 mConflictsFound += synchFolderOp.getConflictsFound();
258 mFailsInFavouritesFound += synchFolderOp.getFailsInFavouritesFound();
259 }
260 if (synchFolderOp.getForgottenLocalFiles().size() > 0) {
261 mForgottenLocalFiles.putAll(synchFolderOp.getForgottenLocalFiles());
262 }
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!
267 }
268
269 } else {
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++;
275
276 } else if (result.getException() instanceof DavException) {
277 mSyncResult.stats.numParseExceptions++;
278
279 } else if (result.getException() instanceof IOException) {
280 mSyncResult.stats.numIoExceptions++;
281 }
282 mFailedResultsCounter++;
283 mLastFailedResult = result;
284 }
285
286 }
287
288 /**
289 * Checks if a failed result should terminate the synchronization process immediately, according to
290 * OUR OWN POLICY
291 *
292 * @param failedResult Remote operation result to check.
293 * @return 'True' if the result should immediately finish the synchronization
294 */
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));
302 }
303 return false;
304 }
305
306 /**
307 * Triggers the synchronization of any folder contained in the list of received files.
308 *
309 * @param files Files to recursively synchronize.
310 */
311 private void fetchChildren(OCFile parent, List<OCFile> files, boolean parentEtagChanged) {
312 int i;
313 OCFile newFile = null;
314 String etag = null;
315 boolean syncDown = false;
316 for (i=0; i < files.size() && !mCancellation; i++) {
317 newFile = files.get(i);
318 if (newFile.isFolder()) {
319 /*
320 etag = newFile.getEtag();
321 syncDown = (parentEtagChanged || etag == null || etag.length() == 0);
322 if(syncDown) { */
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
327 //}
328 }
329 }
330
331 if (mCancellation && i <files.size()) Log_OC.d(TAG, "Leaving synchronization before synchronizing " + files.get(i).getRemotePath() + " due to cancelation request");
332 }
333
334
335 /**
336 * Sends a message to any application component interested in the progress of the synchronization.
337 *
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)
340 */
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);
347 }
348 if (result != null) {
349 i.putExtra(FileSyncService.SYNC_RESULT, result);
350 }
351 getContext().sendStickyBroadcast(i);
352 }
353
354
355
356 /**
357 * Notifies the user about a failed synchronization through the status notification bar
358 */
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()))
367 )
368 );
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);
385 } else {
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);
390 }
391 ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_fail_ticker, notification);
392 }
393
394
395 /**
396 * Notifies the user about conflicts and strange fails when trying to synchronize the contents of kept-in-sync files.
397 *
398 * By now, we won't consider a failed synchronization.
399 */
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);
411
412 } else {
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);
422 }
423 }
424
425 /**
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.
428 *
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.
431 *
432 * We won't consider a synchronization as failed when foreign files can not be copied to the ownCloud local directory.
433 */
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;
437
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);
448
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);
455
456 }
457
458
459 }