make confirmation dialog more universal
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / FileSyncAdapter.java
index fbee3b0..d14151a 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
@@ -40,6 +39,7 @@ import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
 import eu.alefzero.owncloud.datamodel.OCFile;\r
 import eu.alefzero.owncloud.files.services.FileDownloader;\r
 import eu.alefzero.webdav.WebdavEntry;\r
+import eu.alefzero.webdav.WebdavUtils;\r
 \r
 /**\r
  * SyncAdapter implementation for syncing sample SyncAdapter contacts to the\r
@@ -60,6 +60,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 +72,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 +86,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 +103,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 +148,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
@@ -170,7 +177,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
                                                                          .getModificationTimestamp()) {\r
                     Intent intent = new Intent(this.getContext(), FileDownloader.class);\r
                     intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount());\r
-                    intent.putExtra(FileDownloader.EXTRA_FILE_PATH, file.getURLDecodedRemotePath());\r
+                    intent.putExtra(FileDownloader.EXTRA_FILE_PATH, file.getRemotePath());\r
                     intent.putExtra(FileDownloader.EXTRA_REMOTE_PATH, file.getRemotePath());\r
                     intent.putExtra(FileDownloader.EXTRA_FILE_SIZE, file.getFileLength());\r
                     file.setKeepInSync(true);\r
@@ -178,6 +185,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
                 }\r
                 if (getStorageManager().getFileByPath(file.getRemotePath()) != null)\r
                     file.setKeepInSync(getStorageManager().getFileByPath(file.getRemotePath()).keepInSync());\r
+                \r
                 //getStorageManager().saveFile(file);\r
                 updatedFiles.add(file);\r
                 if (parentId == 0)\r
@@ -210,12 +218,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() + WebdavUtils.encodePath(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,13 +250,13 @@ 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
 \r
     private OCFile fillOCFile(WebdavEntry we) {\r
-        OCFile file = new OCFile(we.path());\r
+        OCFile file = new OCFile(we.decodedPath());\r
         file.setCreationTimestamp(we.createTimestamp());\r
         file.setFileLength(we.contentLength());\r
         file.setMimetype(we.contentType());\r
@@ -266,9 +276,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