X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/808d2a74ab55c45cd0dd603964da775897474101..e4bc3bdadc47c01e88dd1de94a4666d3753ee90e:/src/com/owncloud/android/syncadapter/FileSyncAdapter.java diff --git a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java index 1f0d908d..e977e41c 100644 --- a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java +++ b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java @@ -19,20 +19,19 @@ package com.owncloud.android.syncadapter; import java.io.IOException; +import java.net.UnknownHostException; import java.util.List; import org.apache.jackrabbit.webdav.DavException; -import org.json.JSONObject; -import com.owncloud.android.AccountUtils; import com.owncloud.android.R; -import com.owncloud.android.authenticator.AccountAuthenticator; 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.utils.OwnCloudVersion; +import com.owncloud.android.operations.UpdateOCVersionOperation; +import com.owncloud.android.operations.RemoteOperationResult.ResultCode; import android.accounts.Account; import android.app.Notification; @@ -45,7 +44,6 @@ import android.content.Intent; import android.content.SyncResult; import android.os.Bundle; import android.util.Log; -import eu.alefzero.webdav.WebdavClient; /** * SyncAdapter implementation for syncing sample SyncAdapter contacts to the @@ -67,11 +65,17 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { private boolean mIsManualSync; private int mFailedResultsCounter; private RemoteOperationResult mLastFailedResult; + private SyncResult mSyncResult; + private int mConflictsFound; + private int mFailsInFavouritesFound; public FileSyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); } + /** + * {@inheritDoc} + */ @Override public synchronized void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, @@ -81,42 +85,57 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { mIsManualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false); mFailedResultsCounter = 0; mLastFailedResult = null; - + mConflictsFound = 0; + mFailsInFavouritesFound = 0; + mSyncResult = syncResult; + mSyncResult.fullSyncRequested = false; + mSyncResult.delayUntil = 60*60*24; // sync after 24h + this.setAccount(account); this.setContentProvider(provider); this.setStorageManager(new FileDataStorageManager(account, getContentProvider())); + try { + this.initClientForCurrentAccount(); + } catch (UnknownHostException e) { + /// the account is unknown for the Synchronization Manager, or unreachable for this context; don't try this again + mSyncResult.tooManyRetries = true; + notifyFailedSynchronization(); + return; + } - Log.d(TAG, "syncing owncloud account " + account.name); - + Log.d(TAG, "Synchronization of ownCloud account " + account.name + " starting"); sendStickyBroadcast(true, null, null); // message to signal the start of the synchronization to the UI try { updateOCVersion(); mCurrentSyncTime = System.currentTimeMillis(); if (!mCancellation) { - fetchData(OCFile.PATH_SEPARATOR, syncResult, DataStorageManager.ROOT_PARENT_ID); + fetchData(OCFile.PATH_SEPARATOR, DataStorageManager.ROOT_PARENT_ID); } else { Log.d(TAG, "Leaving synchronization before any remote request due to cancellation was requested"); } + } finally { // it's important making this although very unexpected errors occur; that's the reason for the finally if (mFailedResultsCounter > 0 && mIsManualSync) { /// don't let the system synchronization manager retries MANUAL synchronizations // (be careful: "MANUAL" currently includes the synchronization requested when a new account is created and when the user changes the current account) - syncResult.tooManyRetries = true; + mSyncResult.tooManyRetries = true; /// notify the user about the failure of MANUAL synchronization notifyFailedSynchronization(); + + } else if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) { + notifyFailsInFavourites(); } sendStickyBroadcast(false, null, mLastFailedResult); // message to signal the end to the UI } } - - + /** * Called by system SyncManager when a synchronization is required to be cancelled. @@ -136,31 +155,10 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { * Updates the locally stored version value of the ownCloud server */ private void updateOCVersion() { - String statUrl = getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_BASE_URL); - statUrl += AccountUtils.STATUS_PATH; - - try { - String result = getClient().getResultAsString(statUrl); - if (result != null) { - try { - JSONObject json = new JSONObject(result); - if (json != null && json.getString("version") != null) { - OwnCloudVersion ocver = new OwnCloudVersion(json.getString("version")); - if (ocver.isVersionValid()) { - getAccountManager().setUserData(getAccount(), AccountAuthenticator.KEY_OC_VERSION, ocver.toString()); - Log.d(TAG, "Got new OC version " + ocver.toString()); - } else { - Log.w(TAG, "Invalid version number received from server: " + json.getString("version")); - } - } - } catch (Throwable e) { - Log.w(TAG, "Couldn't parse version response", e); - } - } else { - Log.w(TAG, "Problem while getting ocversion from server"); - } - } catch (Exception e) { - Log.e(TAG, "Problem getting response from server", e); + UpdateOCVersionOperation update = new UpdateOCVersionOperation(getAccount(), getContext()); + RemoteOperationResult result = update.execute(getClient()); + if (!result.isSuccess()) { + mLastFailedResult = result; } } @@ -171,22 +169,11 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { * * @param remotePath Remote path to the folder to synchronize. * @param parentId Database Id of the folder to synchronize. - * @param syncResult Object to update for communicate results to the system's synchronization manager. */ - private void fetchData(String remotePath, SyncResult syncResult, long parentId) { - - if (mFailedResultsCounter > MAX_FAILED_RESULTS && isFinisher(mLastFailedResult)) - return; + private void fetchData(String remotePath, long parentId) { - // get client object to connect to the remote ownCloud server - WebdavClient client = null; - try { - client = getClient(); - } catch (IOException e) { - syncResult.stats.numIoExceptions++; - Log.d(TAG, "Could not get client object while trying to synchronize - impossible to continue"); + if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult)) return; - } // perform folder synchronization SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation( remotePath, @@ -196,27 +183,31 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { getAccount(), getContext() ); - RemoteOperationResult result = synchFolderOp.execute(client); + RemoteOperationResult result = synchFolderOp.execute(getClient()); // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess sendStickyBroadcast(true, remotePath, null); - if (result.isSuccess()) { + if (result.isSuccess() || result.getCode() == ResultCode.SYNC_CONFLICT) { + + if (result.getCode() == ResultCode.SYNC_CONFLICT) { + mConflictsFound += synchFolderOp.getConflictsFound(); + mFailsInFavouritesFound += synchFolderOp.getFailsInFavouritesFound(); + } // synchronize children folders List children = synchFolderOp.getChildren(); - fetchChildren(children, syncResult); // beware of the 'hidden' recursion here! + fetchChildren(children); // beware of the 'hidden' recursion here! } else { if (result.getCode() == RemoteOperationResult.ResultCode.UNAUTHORIZED) { - syncResult.stats.numAuthExceptions++; + mSyncResult.stats.numAuthExceptions++; } else if (result.getException() instanceof DavException) { - syncResult.stats.numParseExceptions++; + mSyncResult.stats.numParseExceptions++; } else if (result.getException() instanceof IOException) { - syncResult.stats.numIoExceptions++; - + mSyncResult.stats.numIoExceptions++; } mFailedResultsCounter++; mLastFailedResult = result; @@ -246,14 +237,13 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { * Synchronize data of folders in the list of received files * * @param files Files to recursively fetch - * @param syncResult Updated object to provide results to the Synchronization Manager */ - private void fetchChildren(List files, SyncResult syncResult) { + private void fetchChildren(List files) { int i; for (i=0; i < files.size() && !mCancellation; i++) { OCFile newFile = files.get(i); if (newFile.isDirectory()) { - fetchData(newFile.getRemotePath(), syncResult, newFile.getFileId()); + fetchData(newFile.getRemotePath(), newFile.getFileId()); } } if (mCancellation && i 0) { + Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_fail_in_favourites_ticker), System.currentTimeMillis()); + notification.flags |= Notification.FLAG_AUTO_CANCEL; + // TODO put something smart in the contentIntent below + notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0); + notification.setLatestEventInfo(getContext().getApplicationContext(), + getContext().getString(R.string.sync_fail_in_favourites_ticker), + String.format(getContext().getString(R.string.sync_fail_in_favourites_content), mFailedResultsCounter + mConflictsFound, mConflictsFound), + notification.contentIntent); + ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_fail_in_favourites_ticker, notification); + + } else { + Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_conflicts_in_favourites_ticker), System.currentTimeMillis()); + notification.flags |= Notification.FLAG_AUTO_CANCEL; + // TODO put something smart in the contentIntent below + notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0); + notification.setLatestEventInfo(getContext().getApplicationContext(), + getContext().getString(R.string.sync_conflicts_in_favourites_ticker), + String.format(getContext().getString(R.string.sync_conflicts_in_favourites_content), mConflictsFound), + notification.contentIntent); + ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_conflicts_in_favourites_ticker, notification); + } + } }