123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- package com.owncloud.android.syncadapter;
- import java.io.IOException;
- import java.net.UnknownHostException;
- import java.util.List;
- import org.apache.jackrabbit.webdav.DavException;
- import com.owncloud.android.R;
- import com.owncloud.android.datamodel.DataStorageManager;
- import com.owncloud.android.datamodel.FileDataStorageManager;
- import com.owncloud.android.datamodel.OCFile;
- import com.owncloud.android.operations.RemoteOperationResult;
- import com.owncloud.android.operations.SynchronizeFolderOperation;
- import com.owncloud.android.operations.UpdateOCVersionOperation;
- import android.accounts.Account;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.ContentProviderClient;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SyncResult;
- import android.os.Bundle;
- import android.util.Log;
- public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
- private final static String TAG = "FileSyncAdapter";
-
- private static final int MAX_FAILED_RESULTS = 3;
-
- private long mCurrentSyncTime;
- private boolean mCancellation;
- private boolean mIsManualSync;
- private int mFailedResultsCounter;
- private RemoteOperationResult mLastFailedResult;
- private SyncResult mSyncResult;
-
- public FileSyncAdapter(Context context, boolean autoInitialize) {
- super(context, autoInitialize);
- }
-
- @Override
- public synchronized void onPerformSync(Account account, Bundle extras,
- String authority, ContentProviderClient provider,
- SyncResult syncResult) {
- mCancellation = false;
- mIsManualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
- mFailedResultsCounter = 0;
- mLastFailedResult = null;
- mSyncResult = syncResult;
-
- this.setAccount(account);
- this.setContentProvider(provider);
- this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));
- try {
- this.initClientForCurrentAccount();
- } catch (UnknownHostException e) {
-
- mSyncResult.tooManyRetries = true;
- notifyFailedSynchronization();
- return;
- }
-
- Log.d(TAG, "Synchronization of ownCloud account " + account.name + " starting");
- sendStickyBroadcast(true, null, null);
-
- try {
- updateOCVersion();
- mCurrentSyncTime = System.currentTimeMillis();
- if (!mCancellation) {
- fetchData(OCFile.PATH_SEPARATOR, DataStorageManager.ROOT_PARENT_ID);
-
- } else {
- Log.d(TAG, "Leaving synchronization before any remote request due to cancellation was requested");
- }
-
-
- } finally {
-
-
- if (mFailedResultsCounter > 0 && mIsManualSync) {
-
-
- mSyncResult.tooManyRetries = true;
-
-
- notifyFailedSynchronization();
- }
- sendStickyBroadcast(false, null, mLastFailedResult);
- }
-
- }
-
-
-
-
- @Override
- public void onSyncCanceled() {
- Log.d(TAG, "Synchronization of " + getAccount().name + " has been requested to cancel");
- mCancellation = true;
- super.onSyncCanceled();
- }
-
-
-
- private void updateOCVersion() {
- UpdateOCVersionOperation update = new UpdateOCVersionOperation(getAccount(), getContext());
- RemoteOperationResult result = update.execute(getClient());
- if (!result.isSuccess()) {
- mLastFailedResult = result;
- }
- }
-
-
-
- private void fetchData(String remotePath, long parentId) {
-
- if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult))
- return;
-
-
- SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation( remotePath,
- mCurrentSyncTime,
- parentId,
- getStorageManager(),
- getAccount(),
- getContext()
- );
- RemoteOperationResult result = synchFolderOp.execute(getClient());
-
-
-
- sendStickyBroadcast(true, remotePath, null);
-
- if (result.isSuccess()) {
-
- List<OCFile> children = synchFolderOp.getChildren();
- fetchChildren(children);
-
- } else {
- if (result.getCode() == RemoteOperationResult.ResultCode.UNAUTHORIZED) {
- mSyncResult.stats.numAuthExceptions++;
-
- } else if (result.getException() instanceof DavException) {
- mSyncResult.stats.numParseExceptions++;
-
- } else if (result.getException() instanceof IOException) {
- mSyncResult.stats.numIoExceptions++;
-
- }
- mFailedResultsCounter++;
- mLastFailedResult = result;
- }
-
- }
-
- private boolean isFinisher(RemoteOperationResult failedResult) {
- if (failedResult != null) {
- RemoteOperationResult.ResultCode code = failedResult.getCode();
- return (code.equals(RemoteOperationResult.ResultCode.SSL_ERROR) ||
- code.equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) ||
- code.equals(RemoteOperationResult.ResultCode.BAD_OC_VERSION) ||
- code.equals(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED));
- }
- return false;
- }
-
- private void fetchChildren(List<OCFile> files) {
- int i;
- for (i=0; i < files.size() && !mCancellation; i++) {
- OCFile newFile = files.get(i);
- if (newFile.isDirectory()) {
- fetchData(newFile.getRemotePath(), newFile.getFileId());
- }
- }
- if (mCancellation && i <files.size()) Log.d(TAG, "Leaving synchronization before synchronizing " + files.get(i).getRemotePath() + " because cancelation request");
- }
-
-
- private void sendStickyBroadcast(boolean inProgress, String dirRemotePath, RemoteOperationResult result) {
- Intent i = new Intent(FileSyncService.SYNC_MESSAGE);
- i.putExtra(FileSyncService.IN_PROGRESS, inProgress);
- i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);
- if (dirRemotePath != null) {
- i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath);
- }
- if (result != null) {
- i.putExtra(FileSyncService.SYNC_RESULT, result);
- }
- getContext().sendStickyBroadcast(i);
- }
-
-
-
- private void notifyFailedSynchronization() {
- Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_fail_ticker), System.currentTimeMillis());
- notification.flags |= Notification.FLAG_AUTO_CANCEL;
-
- notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0);
- notification.setLatestEventInfo(getContext().getApplicationContext(),
- getContext().getString(R.string.sync_fail_ticker),
- String.format(getContext().getString(R.string.sync_fail_content), getAccount().name),
- notification.contentIntent);
- ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_fail_ticker, notification);
- }
-
- }
|