From: David A. Velasco Date: Mon, 16 Jul 2012 13:06:03 +0000 (+0200) Subject: Cancellable synchronization; current synchronizations are cancelled when the user... X-Git-Tag: oc-android-1.4.3~271 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/400a17d3e32952fbec63902084ff1d32ee346f4c Cancellable synchronization; current synchronizations are cancelled when the user request for a synchronization in the options menu --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index a6539f4c..53c014c9 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -18,7 +18,7 @@ --> + android:versionName="0.1.160B" xmlns:android="http://schemas.android.com/apk/res/android"> diff --git a/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java b/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java index fbee3b00..9f0c0dc5 100644 --- a/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java +++ b/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java @@ -19,7 +19,6 @@ package eu.alefzero.owncloud.syncadapter; import java.io.IOException; -import java.io.ObjectInputStream.GetField; import java.util.List; import java.util.Vector; @@ -60,6 +59,8 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { */ private long mCurrentSyncTime; + private boolean mCancellation; + private Account mAccount; public FileSyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); @@ -70,9 +71,12 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { String authority, ContentProviderClient provider, SyncResult syncResult) { - this.setAccount(account); + mCancellation = false; + mAccount = account; + + this.setAccount(mAccount); this.setContentProvider(provider); - this.setStorageManager(new FileDataStorageManager(account, + this.setStorageManager(new FileDataStorageManager(mAccount, getContentProvider())); /* Commented code for ugly performance tests @@ -81,7 +85,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { */ - Log.d(TAG, "syncing owncloud account " + account.name); + Log.d(TAG, "syncing owncloud account " + mAccount.name); sendStickyBroadcast(true, null); // message to signal the start to the UI @@ -98,7 +102,9 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { OCFile file = fillOCFile(we); file.setParentId(0); getStorageManager().saveFile(file); - fetchData(getUri().toString(), syncResult, file.getFileId(), account); + if (!mCancellation) { + fetchData(getUri().toString(), syncResult, file.getFileId()); + } } } catch (OperationCanceledException e) { e.printStackTrace(); @@ -141,7 +147,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { sendStickyBroadcast(false, null); } - private void fetchData(String uri, SyncResult syncResult, long parentId, Account account) { + private void fetchData(String uri, SyncResult syncResult, long parentId) { try { //Log.v(TAG, "syncing: fetching " + uri); @@ -210,12 +216,14 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath()); // recursive fetch - for (OCFile newFile : files) { + for (int i=0; i < files.size() && !mCancellation; i++) { + OCFile newFile = files.get(i); if (newFile.getMimetype().equals("DIR")) { - fetchData(getUri().toString() + newFile.getRemotePath(), syncResult, newFile.getFileId(), account); + fetchData(getUri().toString() + newFile.getRemotePath(), syncResult, newFile.getFileId()); } } - + if (mCancellation) Log.d(TAG, "Leaving " + uri + " because cancellation request"); + /* Commented code for ugly performance tests mResponseDelays[mDelaysIndex] = responseDelay; mSaveDelays[mDelaysIndex] = saveDelay; @@ -240,7 +248,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { e.printStackTrace(); } catch (Throwable t) { // TODO update syncResult - Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t); + Log.e(TAG, "problem while synchronizing owncloud account " + mAccount.name, t); t.printStackTrace(); } } @@ -266,9 +274,16 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { getContext().sendStickyBroadcast(i); } + /** + * Called by system SyncManager when a synchronization is required to be cancelled. + * + * Sets the mCancellation flag to 'true'. THe synchronization will be stopped when before a new folder is fetched. Data of the last folder + * fetched will be still saved in the database. See onPerformSync implementation. + */ @Override public void onSyncCanceled() { - Log.d(TAG, "sync is being cancelled !! ************************************************"); + Log.d(TAG, "Synchronization of " + mAccount.name + " has been requested to cancell"); + mCancellation = true; super.onSyncCanceled(); } diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java index 7e620840..e54913a1 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java @@ -170,8 +170,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements break; } case R.id.startSync: { - // This could be interesting - //ContentResolver.cancelSync(null, "org.owncloud"); // cancel the current synchronizations of any other ownCloud account + ContentResolver.cancelSync(null, "org.owncloud"); // cancel the current synchronizations of any ownCloud account Bundle bundle = new Bundle(); bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); ContentResolver.requestSync(