瀏覽代碼

Cancellable synchronization; current synchronizations are cancelled when the user request for a synchronization in the options menu

David A. Velasco 12 年之前
父節點
當前提交
400a17d3e3

+ 1 - 1
AndroidManifest.xml

@@ -18,7 +18,7 @@
  -->
 <manifest package="eu.alefzero.owncloud"
     android:versionCode="1"
-    android:versionName="0.1.159B" xmlns:android="http://schemas.android.com/apk/res/android">
+    android:versionName="0.1.160B" xmlns:android="http://schemas.android.com/apk/res/android">
 
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />

+ 26 - 11
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();
     }
 

+ 1 - 2
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(