Cancellable synchronization; current synchronizations are cancelled when the user...
authorDavid A. Velasco <dvelasco@solidgear.es>
Mon, 16 Jul 2012 13:06:03 +0000 (15:06 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Mon, 16 Jul 2012 13:06:03 +0000 (15:06 +0200)
AndroidManifest.xml
src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java
src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java

index a6539f4..53c014c 100644 (file)
@@ -18,7 +18,7 @@
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
-    android:versionName="0.1.159B" xmlns:android="http://schemas.android.com/apk/res/android">\r
+    android:versionName="0.1.160B" xmlns:android="http://schemas.android.com/apk/res/android">\r
 \r
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />\r
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />\r
index fbee3b0..9f0c0dc 100644 (file)
@@ -19,7 +19,6 @@
 package eu.alefzero.owncloud.syncadapter;\r
 \r
 import java.io.IOException;\r
-import java.io.ObjectInputStream.GetField;\r
 import java.util.List;\r
 import java.util.Vector;\r
 \r
@@ -60,6 +59,8 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
     */\r
     \r
     private long mCurrentSyncTime;\r
+    private boolean mCancellation;\r
+    private Account mAccount;\r
     \r
     public FileSyncAdapter(Context context, boolean autoInitialize) {\r
         super(context, autoInitialize);\r
@@ -70,9 +71,12 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
             String authority, ContentProviderClient provider,\r
             SyncResult syncResult) {\r
 \r
-        this.setAccount(account);\r
+        mCancellation = false;\r
+        mAccount = account;\r
+        \r
+        this.setAccount(mAccount);\r
         this.setContentProvider(provider);\r
-        this.setStorageManager(new FileDataStorageManager(account,\r
+        this.setStorageManager(new FileDataStorageManager(mAccount,\r
                 getContentProvider()));\r
         \r
         /*  Commented code for ugly performance tests\r
@@ -81,7 +85,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         */\r
         \r
         \r
-        Log.d(TAG, "syncing owncloud account " + account.name);\r
+        Log.d(TAG, "syncing owncloud account " + mAccount.name);\r
 \r
         sendStickyBroadcast(true, null);  // message to signal the start to the UI\r
 \r
@@ -98,7 +102,9 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
                 OCFile file = fillOCFile(we);\r
                 file.setParentId(0);\r
                 getStorageManager().saveFile(file);\r
-                fetchData(getUri().toString(), syncResult, file.getFileId(), account);\r
+                if (!mCancellation) {\r
+                    fetchData(getUri().toString(), syncResult, file.getFileId());\r
+                }\r
             }\r
         } catch (OperationCanceledException e) {\r
             e.printStackTrace();\r
@@ -141,7 +147,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         sendStickyBroadcast(false, null);        \r
     }\r
 \r
-    private void fetchData(String uri, SyncResult syncResult, long parentId, Account account) {\r
+    private void fetchData(String uri, SyncResult syncResult, long parentId) {\r
         try {\r
             //Log.v(TAG, "syncing: fetching " + uri);\r
             \r
@@ -210,12 +216,14 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
             sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath());\r
 \r
             // recursive fetch\r
-            for (OCFile newFile : files) {\r
+            for (int i=0; i < files.size() && !mCancellation; i++) {\r
+                OCFile newFile = files.get(i);\r
                 if (newFile.getMimetype().equals("DIR")) {\r
-                    fetchData(getUri().toString() + newFile.getRemotePath(), syncResult, newFile.getFileId(), account);\r
+                    fetchData(getUri().toString() + newFile.getRemotePath(), syncResult, newFile.getFileId());\r
                 }\r
             }\r
-            \r
+            if (mCancellation) Log.d(TAG, "Leaving " + uri + " because cancellation request");\r
+                \r
             /*  Commented code for ugly performance tests\r
             mResponseDelays[mDelaysIndex] = responseDelay;\r
             mSaveDelays[mDelaysIndex] = saveDelay;\r
@@ -240,7 +248,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
             e.printStackTrace();\r
         } catch (Throwable t) {\r
             // TODO update syncResult\r
-            Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t);\r
+            Log.e(TAG, "problem while synchronizing owncloud account " + mAccount.name, t);\r
             t.printStackTrace();\r
         }\r
     }\r
@@ -266,9 +274,16 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         getContext().sendStickyBroadcast(i);\r
     }\r
     \r
+    /**\r
+     * Called by system SyncManager when a synchronization is required to be cancelled.\r
+     * \r
+     * Sets the mCancellation flag to 'true'. THe synchronization will be stopped when before a new folder is fetched. Data of the last folder\r
+     * fetched will be still saved in the database. See onPerformSync implementation.\r
+     */\r
     @Override\r
     public void onSyncCanceled() {\r
-        Log.d(TAG, "sync is being cancelled !! ************************************************");\r
+        Log.d(TAG, "Synchronization of " + mAccount.name + " has been requested to cancell");\r
+        mCancellation = true;\r
         super.onSyncCanceled();\r
     }\r
 \r
index 7e62084..e54913a 100644 (file)
@@ -170,8 +170,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 break;\r
             }\r
             case R.id.startSync: {\r
-                // This could be interesting\r
-                //ContentResolver.cancelSync(null, "org.owncloud");   // cancel the current synchronizations of any other ownCloud account\r
+                ContentResolver.cancelSync(null, "org.owncloud");   // cancel the current synchronizations of any ownCloud account\r
                 Bundle bundle = new Bundle();\r
                 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
                 ContentResolver.requestSync(\r