Fixed initialization of database tracking pending instant uploads
[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 a {@link FileSyncAdapter}
102 *
103 * {@inheritDoc}
104 */
105 public FileSyncAdapter(Context context, boolean autoInitialize) {
106 super(context, autoInitialize);
107 }
108
109
110 /**
111 * Creates a {@link FileSyncAdapter}
112 *
113 * {@inheritDoc}
114 */
115 public FileSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
116 super(context, autoInitialize, allowParallelSyncs);
117 }
118
119
120 /**
121 * {@inheritDoc}
122 */
123 @Override
124 public synchronized void onPerformSync(Account account, Bundle extras,
125 String authority, ContentProviderClient providerClient,
126 SyncResult syncResult) {
127
128 mCancellation = false;
129 mIsManualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
130 mFailedResultsCounter = 0;
131 mLastFailedResult = null;
132 mConflictsFound = 0;
133 mFailsInFavouritesFound = 0;
134 mForgottenLocalFiles = new HashMap<String, String>();
135 mSyncResult = syncResult;
136 mSyncResult.fullSyncRequested = false;
137 mSyncResult.delayUntil = 60*60*24; // avoid too many automatic synchronizations
138
139 this.setAccount(account);
140 this.setContentProviderClient(providerClient);
141 this.setStorageManager(new FileDataStorageManager(account, providerClient));
142 try {
143 this.initClientForCurrentAccount();
144 } catch (IOException e) {
145 /// the account is unknown for the Synchronization Manager, unreachable this context, or can not be authenticated; don't try this again
146 mSyncResult.tooManyRetries = true;
147 notifyFailedSynchronization();
148 return;
149 } catch (AccountsException e) {
150 /// the account is unknown for the Synchronization Manager, unreachable this context, or can not be authenticated; don't try this again
151 mSyncResult.tooManyRetries = true;
152 notifyFailedSynchronization();
153 return;
154 }
155
156 Log_OC.d(TAG, "Synchronization of ownCloud account " + account.name + " starting");
157 sendStickyBroadcast(true, null, null); // message to signal the start of the synchronization to the UI
158
159 try {
160 updateOCVersion();
161 mCurrentSyncTime = System.currentTimeMillis();
162 if (!mCancellation) {
163 synchronizeFolder(getStorageManager().getFileByPath(OCFile.ROOT_PATH));
164
165 } else {
166 Log_OC.d(TAG, "Leaving synchronization before synchronizing the root folder because cancelation request");
167 }
168
169
170 } finally {
171 // it's important making this although very unexpected errors occur; that's the reason for the finally
172
173 if (mFailedResultsCounter > 0 && mIsManualSync) {
174 /// don't let the system synchronization manager retries MANUAL synchronizations
175 // (be careful: "MANUAL" currently includes the synchronization requested when a new account is created and when the user changes the current account)
176 mSyncResult.tooManyRetries = true;
177
178 /// notify the user about the failure of MANUAL synchronization
179 notifyFailedSynchronization();
180 }
181 if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
182 notifyFailsInFavourites();
183 }
184 if (mForgottenLocalFiles.size() > 0) {
185 notifyForgottenLocalFiles();
186 }
187 sendStickyBroadcast(false, null, mLastFailedResult); // message to signal the end to the UI
188 }
189
190 }
191
192 /**
193 * Called by system SyncManager when a synchronization is required to be cancelled.
194 *
195 * Sets the mCancellation flag to 'true'. THe synchronization will be stopped later,
196 * before a new folder is fetched. Data of the last folder synchronized will be still
197 * locally saved.
198 *
199 * See {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}
200 * and {@link #synchronizeFolder(String, long)}.
201 */
202 @Override
203 public void onSyncCanceled() {
204 Log_OC.d(TAG, "Synchronization of " + getAccount().name + " has been requested to cancel");
205 mCancellation = true;
206 super.onSyncCanceled();
207 }
208
209
210 /**
211 * Updates the locally stored version value of the ownCloud server
212 */
213 private void updateOCVersion() {
214 UpdateOCVersionOperation update = new UpdateOCVersionOperation(getAccount(), getContext());
215 RemoteOperationResult result = update.execute(getClient());
216 if (!result.isSuccess()) {
217 mLastFailedResult = result;
218 }
219 }
220
221
222 /**
223 * Synchronizes the list of files contained in a folder identified with its remote path.
224 *
225 * Fetches the list and properties of the files contained in the given folder, including their
226 * properties, and updates the local database with them.
227 *
228 * Enters in the child folders to synchronize their contents also, following a recursive
229 * depth first strategy.
230 *
231 * @param folder Folder to synchronize.
232 */
233 private void synchronizeFolder(OCFile folder) {
234
235 if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult))
236 return;
237
238 /*
239 OCFile folder,
240 long currentSyncTime,
241 boolean updateFolderProperties,
242 boolean syncFullAccount,
243 DataStorageManager dataStorageManager,
244 Account account,
245 Context context ) {
246
247 }
248 */
249 // folder synchronization
250 SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation( folder,
251 mCurrentSyncTime,
252 true,
253 getStorageManager(),
254 getAccount(),
255 getContext()
256 );
257 RemoteOperationResult result = synchFolderOp.execute(getClient());
258
259
260 // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
261 sendStickyBroadcast(true, folder.getRemotePath(), null);
262
263 // check the result of synchronizing the folder
264 if (result.isSuccess() || result.getCode() == ResultCode.SYNC_CONFLICT) {
265
266 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
267 mConflictsFound += synchFolderOp.getConflictsFound();
268 mFailsInFavouritesFound += synchFolderOp.getFailsInFavouritesFound();
269 }
270 if (synchFolderOp.getForgottenLocalFiles().size() > 0) {
271 mForgottenLocalFiles.putAll(synchFolderOp.getForgottenLocalFiles());
272 }
273 if (result.isSuccess()) {
274 // synchronize children folders
275 List<OCFile> children = synchFolderOp.getChildren();
276 fetchChildren(folder, children, synchFolderOp.getRemoteFolderChanged()); // beware of the 'hidden' recursion here!
277 }
278
279 } else {
280 // in failures, the statistics for the global result are updated
281 if (result.getCode() == RemoteOperationResult.ResultCode.UNAUTHORIZED ||
282 ( result.isIdPRedirection() &&
283 MainApp.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))) {
284 mSyncResult.stats.numAuthExceptions++;
285
286 } else if (result.getException() instanceof DavException) {
287 mSyncResult.stats.numParseExceptions++;
288
289 } else if (result.getException() instanceof IOException) {
290 mSyncResult.stats.numIoExceptions++;
291 }
292 mFailedResultsCounter++;
293 mLastFailedResult = result;
294 }
295
296 }
297
298 /**
299 * Checks if a failed result should terminate the synchronization process immediately, according to
300 * OUR OWN POLICY
301 *
302 * @param failedResult Remote operation result to check.
303 * @return 'True' if the result should immediately finish the synchronization
304 */
305 private boolean isFinisher(RemoteOperationResult failedResult) {
306 if (failedResult != null) {
307 RemoteOperationResult.ResultCode code = failedResult.getCode();
308 return (code.equals(RemoteOperationResult.ResultCode.SSL_ERROR) ||
309 code.equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) ||
310 code.equals(RemoteOperationResult.ResultCode.BAD_OC_VERSION) ||
311 code.equals(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED));
312 }
313 return false;
314 }
315
316 /**
317 * Triggers the synchronization of any folder contained in the list of received files.
318 *
319 * @param files Files to recursively synchronize.
320 */
321 private void fetchChildren(OCFile parent, List<OCFile> files, boolean parentEtagChanged) {
322 int i;
323 OCFile newFile = null;
324 String etag = null;
325 boolean syncDown = false;
326 for (i=0; i < files.size() && !mCancellation; i++) {
327 newFile = files.get(i);
328 if (newFile.isFolder()) {
329 /*
330 etag = newFile.getEtag();
331 syncDown = (parentEtagChanged || etag == null || etag.length() == 0);
332 if(syncDown) { */
333 synchronizeFolder(newFile);
334 // update the size of the parent folder again after recursive synchronization
335 //getStorageManager().updateFolderSize(parent.getFileId());
336 sendStickyBroadcast(true, parent.getRemotePath(), null); // notify again to refresh size in UI
337 //}
338 }
339 }
340
341 if (mCancellation && i <files.size()) Log_OC.d(TAG, "Leaving synchronization before synchronizing " + files.get(i).getRemotePath() + " due to cancelation request");
342 }
343
344
345 /**
346 * Sends a message to any application component interested in the progress of the synchronization.
347 *
348 * @param inProgress 'True' when the synchronization progress is not finished.
349 * @param dirRemotePath Remote path of a folder that was just synchronized (with or without success)
350 */
351 private void sendStickyBroadcast(boolean inProgress, String dirRemotePath, RemoteOperationResult result) {
352 Intent i = new Intent(FileSyncService.getSyncMessage());
353 i.putExtra(FileSyncService.IN_PROGRESS, inProgress);
354 i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);
355 if (dirRemotePath != null) {
356 i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath);
357 }
358 if (result != null) {
359 i.putExtra(FileSyncService.SYNC_RESULT, result);
360 }
361 getContext().sendStickyBroadcast(i);
362 }
363
364
365
366 /**
367 * Notifies the user about a failed synchronization through the status notification bar
368 */
369 private void notifyFailedSynchronization() {
370 Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_fail_ticker), System.currentTimeMillis());
371 notification.flags |= Notification.FLAG_AUTO_CANCEL;
372 boolean needsToUpdateCredentials = (mLastFailedResult != null &&
373 ( mLastFailedResult.getCode() == ResultCode.UNAUTHORIZED ||
374 // (mLastFailedResult.isTemporalRedirection() && mLastFailedResult.isIdPRedirection() &&
375 ( mLastFailedResult.isIdPRedirection() &&
376 MainApp.getAuthTokenTypeSamlSessionCookie().equals(getClient().getAuthTokenType()))
377 )
378 );
379 // TODO put something smart in the contentIntent below for all the possible errors
380 notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0);
381 if (needsToUpdateCredentials) {
382 // let the user update credentials with one click
383 Intent updateAccountCredentials = new Intent(getContext(), AuthenticatorActivity.class);
384 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
385 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ENFORCED_UPDATE, true);
386 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);
387 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
388 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
389 updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
390 notification.contentIntent = PendingIntent.getActivity(getContext(), (int)System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT);
391 notification.setLatestEventInfo(getContext().getApplicationContext(),
392 getContext().getString(R.string.sync_fail_ticker),
393 String.format(getContext().getString(R.string.sync_fail_content_unauthorized), getAccount().name),
394 notification.contentIntent);
395 } else {
396 notification.setLatestEventInfo(getContext().getApplicationContext(),
397 getContext().getString(R.string.sync_fail_ticker),
398 String.format(getContext().getString(R.string.sync_fail_content), getAccount().name),
399 notification.contentIntent);
400 }
401 ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_fail_ticker, notification);
402 }
403
404
405 /**
406 * Notifies the user about conflicts and strange fails when trying to synchronize the contents of kept-in-sync files.
407 *
408 * By now, we won't consider a failed synchronization.
409 */
410 private void notifyFailsInFavourites() {
411 if (mFailedResultsCounter > 0) {
412 Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_fail_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_fail_in_favourites_ticker),
418 String.format(getContext().getString(R.string.sync_fail_in_favourites_content), mFailedResultsCounter + mConflictsFound, mConflictsFound),
419 notification.contentIntent);
420 ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_fail_in_favourites_ticker, notification);
421
422 } else {
423 Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_conflicts_in_favourites_ticker), System.currentTimeMillis());
424 notification.flags |= Notification.FLAG_AUTO_CANCEL;
425 // TODO put something smart in the contentIntent below
426 notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0);
427 notification.setLatestEventInfo(getContext().getApplicationContext(),
428 getContext().getString(R.string.sync_conflicts_in_favourites_ticker),
429 String.format(getContext().getString(R.string.sync_conflicts_in_favourites_content), mConflictsFound),
430 notification.contentIntent);
431 ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_conflicts_in_favourites_ticker, notification);
432 }
433 }
434
435 /**
436 * Notifies the user about local copies of files out of the ownCloud local directory that were 'forgotten' because
437 * copying them inside the ownCloud local directory was not possible.
438 *
439 * We don't want links to files out of the ownCloud local directory (foreign files) anymore. It's easy to have
440 * synchronization problems if a local file is linked to more than one remote file.
441 *
442 * We won't consider a synchronization as failed when foreign files can not be copied to the ownCloud local directory.
443 */
444 private void notifyForgottenLocalFiles() {
445 Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_foreign_files_forgotten_ticker), System.currentTimeMillis());
446 notification.flags |= Notification.FLAG_AUTO_CANCEL;
447
448 /// includes a pending intent in the notification showing a more detailed explanation
449 Intent explanationIntent = new Intent(getContext(), ErrorsWhileCopyingHandlerActivity.class);
450 explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_ACCOUNT, getAccount());
451 ArrayList<String> remotePaths = new ArrayList<String>();
452 ArrayList<String> localPaths = new ArrayList<String>();
453 remotePaths.addAll(mForgottenLocalFiles.keySet());
454 localPaths.addAll(mForgottenLocalFiles.values());
455 explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_LOCAL_PATHS, localPaths);
456 explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_REMOTE_PATHS, remotePaths);
457 explanationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
458
459 notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), explanationIntent, 0);
460 notification.setLatestEventInfo(getContext().getApplicationContext(),
461 getContext().getString(R.string.sync_foreign_files_forgotten_ticker),
462 String.format(getContext().getString(R.string.sync_foreign_files_forgotten_content), mForgottenLocalFiles.size(), getContext().getString(R.string.app_name)),
463 notification.contentIntent);
464 ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_foreign_files_forgotten_ticker, notification);
465
466 }
467
468
469 }