Updating synchronization for providing SSL warning when necessary; STEP 2: added...
[pub/Android/ownCloud.git] / src / com / owncloud / android / syncadapter / FileSyncAdapter.java
index c5776de..1f0d908 100644 (file)
@@ -55,12 +55,18 @@ import eu.alefzero.webdav.WebdavClient;
  */\r
 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {\r
 \r
-    private final static String TAG = "FileSyncAdapter"; \r
+    private final static String TAG = "FileSyncAdapter";\r
+\r
+    /** \r
+     * Maximum number of failed folder synchronizations that are supported before finishing the synchronization operation\r
+     */\r
+    private static final int MAX_FAILED_RESULTS = 3; \r
     \r
     private long mCurrentSyncTime;\r
     private boolean mCancellation;\r
     private boolean mIsManualSync;\r
-    private boolean mRightSync;\r
+    private int mFailedResultsCounter;    \r
+    private RemoteOperationResult mLastFailedResult;\r
     \r
     public FileSyncAdapter(Context context, boolean autoInitialize) {\r
         super(context, autoInitialize);\r
@@ -73,7 +79,8 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
 \r
         mCancellation = false;\r
         mIsManualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);\r
-        mRightSync = true;\r
+        mFailedResultsCounter = 0;\r
+        mLastFailedResult = null;\r
         \r
         this.setAccount(account);\r
         this.setContentProvider(provider);\r
@@ -81,7 +88,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         \r
         Log.d(TAG, "syncing owncloud account " + account.name);\r
 \r
-        sendStickyBroadcast(true, null);  // message to signal the start to the UI\r
+        sendStickyBroadcast(true, null, null);  // message to signal the start of the synchronization to the UI\r
         \r
         try {\r
             updateOCVersion();\r
@@ -96,8 +103,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         } finally {\r
             // it's important making this although very unexpected errors occur; that's the reason for the finally\r
             \r
-            mRightSync &= (syncResult.stats.numIoExceptions == 0 && syncResult.stats.numAuthExceptions == 0 && syncResult.stats.numParseExceptions == 0);\r
-            if (!mRightSync && mIsManualSync) {\r
+            if (mFailedResultsCounter > 0 && mIsManualSync) {\r
                 /// don't let the system synchronization manager retries MANUAL synchronizations\r
                 //      (be careful: "MANUAL" currently includes the synchronization requested when a new account is created and when the user changes the current account)\r
                 syncResult.tooManyRetries = true;\r
@@ -105,7 +111,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
                 /// notify the user about the failure of MANUAL synchronization\r
                 notifyFailedSynchronization();\r
             }\r
-            sendStickyBroadcast(false, null);        // message to signal the end to the UI\r
+            sendStickyBroadcast(false, null, mLastFailedResult);        // message to signal the end to the UI\r
         }\r
         \r
     }\r
@@ -169,6 +175,9 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
      */\r
     private void fetchData(String remotePath, SyncResult syncResult, long parentId) {\r
         \r
+        if (mFailedResultsCounter > MAX_FAILED_RESULTS && isFinisher(mLastFailedResult))\r
+            return;\r
+        \r
         // get client object to connect to the remote ownCloud server\r
         WebdavClient client = null;\r
         try {\r
@@ -191,7 +200,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         \r
         \r
         // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess\r
-        sendStickyBroadcast(true, remotePath);\r
+        sendStickyBroadcast(true, remotePath, null);\r
         \r
         if (result.isSuccess()) {\r
             // synchronize children folders \r
@@ -208,15 +217,32 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
             } else if (result.getException() instanceof IOException) { \r
                 syncResult.stats.numIoExceptions++;\r
                 \r
-            } else if (result.getException() != null) {\r
-                // TODO maybe something smarter with syncResult\r
-                mRightSync = false;\r
             }\r
+            mFailedResultsCounter++;\r
+            mLastFailedResult = result;\r
         }\r
             \r
     }\r
 \r
     /**\r
+     * Checks if a failed result should terminate the synchronization process immediately, according to\r
+     * OUR OWN POLICY\r
+     * \r
+     * @param   failedResult        Remote operation result to check.\r
+     * @return                      'True' if the result should immediately finish the synchronization\r
+     */\r
+    private boolean isFinisher(RemoteOperationResult failedResult) {\r
+        if  (failedResult != null) {\r
+            RemoteOperationResult.ResultCode code = failedResult.getCode();\r
+            return (code.equals(RemoteOperationResult.ResultCode.SSL_ERROR) ||\r
+                    code.equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) ||\r
+                    code.equals(RemoteOperationResult.ResultCode.BAD_OC_VERSION) ||\r
+                    code.equals(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED));\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /**\r
      * Synchronize data of folders in the list of received files\r
      * \r
      * @param files         Files to recursively fetch \r
@@ -235,21 +261,21 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
 \r
     \r
     /**\r
-     * Sends a message to any app component interested in the progress of the synchronization.\r
+     * Sends a message to any application component interested in the progress of the synchronization.\r
      * \r
      * @param inProgress        'True' when the synchronization progress is not finished.\r
      * @param dirRemotePath     Remote path of a folder that was just synchronized (with or without success)\r
      */\r
-    private void sendStickyBroadcast(boolean inProgress, String dirRemotePath/*, RemoteOperationResult result*/) {\r
+    private void sendStickyBroadcast(boolean inProgress, String dirRemotePath, RemoteOperationResult result) {\r
         Intent i = new Intent(FileSyncService.SYNC_MESSAGE);\r
         i.putExtra(FileSyncService.IN_PROGRESS, inProgress);\r
         i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);\r
         if (dirRemotePath != null) {\r
             i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath);\r
         }\r
-        /*if (result != null) {\r
+        if (result != null) {\r
             i.putExtra(FileSyncService.SYNC_RESULT, result);\r
-        }*/\r
+        }\r
         getContext().sendStickyBroadcast(i);\r
     }\r
 \r