Updating synchronization for providing SSL warning when necessary; STEP 2: added...
authorDavid A. Velasco <dvelasco@solidgear.es>
Tue, 23 Oct 2012 13:35:47 +0000 (15:35 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Tue, 23 Oct 2012 13:35:47 +0000 (15:35 +0200)
src/com/owncloud/android/syncadapter/FileSyncAdapter.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.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
index 22ef63b..636252c 100644 (file)
@@ -71,7 +71,10 @@ import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
+import com.owncloud.android.operations.RemoteOperationResult;\r
 import com.owncloud.android.syncadapter.FileSyncService;\r
+import com.owncloud.android.ui.dialog.SslValidatorDialog;\r
+import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;\r
 import com.owncloud.android.ui.fragment.FileDetailFragment;\r
 import com.owncloud.android.ui.fragment.OCFileListFragment;\r
 \r
@@ -86,7 +89,7 @@ import eu.alefzero.webdav.WebdavClient;
  */\r
 \r
 public class FileDisplayActivity extends SherlockFragmentActivity implements\r
-    OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener {\r
+    OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener {\r
     \r
     private ArrayAdapter<String> mDirectories;\r
     private OCFile mCurrentDir = null;\r
@@ -99,6 +102,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     private FileDownloaderBinder mDownloaderBinder = null;\r
     private FileUploaderBinder mUploaderBinder = null;\r
     private ServiceConnection mDownloadConnection = null, mUploadConnection = null;\r
+    private RemoteOperationResult mLastSslUntrustedServerResult = null;\r
     \r
     private OCFileListFragment mFileList;\r
     \r
@@ -109,6 +113,9 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     private static final int DIALOG_ABOUT_APP = 2;\r
     public static final int DIALOG_SHORT_WAIT = 3;\r
     private static final int DIALOG_CHOOSE_UPLOAD_SOURCE = 4;\r
+    private static final int DIALOG_SSL_VALIDATOR = 5;\r
+    private static final int DIALOG_CERT_NOT_SAVED = 6;\r
+\r
     \r
     private static final int ACTION_SELECT_CONTENT_FROM_APPS = 1;\r
     private static final int ACTION_SELECT_MULTIPLE_FILES = 2;\r
@@ -274,12 +281,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 break;\r
             }\r
             case R.id.startSync: {\r
-                ContentResolver.cancelSync(null, AccountAuthenticator.AUTH_TOKEN_TYPE);   // 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
-                        AccountUtils.getCurrentOwnCloudAccount(this),\r
-                        AccountAuthenticator.AUTH_TOKEN_TYPE, bundle);\r
+                startSynchronization();\r
                 break;\r
             }\r
             case R.id.action_upload: {\r
@@ -307,6 +309,16 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         return retval;\r
     }\r
 \r
+    private void startSynchronization() {\r
+        ContentResolver.cancelSync(null, AccountAuthenticator.AUTH_TOKEN_TYPE);   // 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
+                AccountUtils.getCurrentOwnCloudAccount(this),\r
+                AccountAuthenticator.AUTH_TOKEN_TYPE, bundle);\r
+    }\r
+\r
+\r
     @Override\r
     public boolean onNavigationItemSelected(int itemPosition, long itemId) {\r
         int i = itemPosition;\r
@@ -520,6 +532,15 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         Log.d(getClass().toString(), "onPause() end");\r
     }\r
 \r
+    \r
+    @Override\r
+    protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {\r
+        if (id == DIALOG_SSL_VALIDATOR && mLastSslUntrustedServerResult != null) {\r
+            ((SslValidatorDialog)dialog).updateResult(mLastSslUntrustedServerResult);\r
+        }\r
+    }\r
+\r
+    \r
     @Override\r
     protected Dialog onCreateDialog(int id) {\r
         Dialog dialog = null;\r
@@ -645,6 +666,23 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             dialog = builder.create();\r
             break;\r
         }\r
+        case DIALOG_SSL_VALIDATOR: {\r
+            dialog = SslValidatorDialog.newInstance(this, mLastSslUntrustedServerResult, this);\r
+            break;\r
+        }\r
+        case DIALOG_CERT_NOT_SAVED: {\r
+            builder = new AlertDialog.Builder(this);\r
+            builder.setMessage(getResources().getString(R.string.ssl_validator_not_saved));\r
+            builder.setCancelable(false);\r
+            builder.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {\r
+                    @Override\r
+                    public void onClick(DialogInterface dialog, int which) {\r
+                        dialog.dismiss();\r
+                    };\r
+                });\r
+            dialog = builder.create();\r
+            break;\r
+        }\r
         default:\r
             dialog = null;\r
         }\r
@@ -772,6 +810,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }\r
 \r
     private class SyncBroadcastReceiver extends BroadcastReceiver {\r
+\r
         /**\r
          * {@link BroadcastReceiver} to enable syncing feedback in UI\r
          */\r
@@ -809,6 +848,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 setSupportProgressBarIndeterminateVisibility(inProgress);\r
                 \r
             }\r
+            \r
+            RemoteOperationResult synchResult = (RemoteOperationResult)intent.getSerializableExtra(FileSyncService.SYNC_RESULT);\r
+            if (synchResult != null) {\r
+                if (synchResult.getCode().equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)) {\r
+                    mLastSslUntrustedServerResult = synchResult;\r
+                    showDialog(DIALOG_SSL_VALIDATOR); \r
+                }\r
+            }\r
         }\r
     }\r
     \r
@@ -1009,4 +1056,16 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }\r
 \r
 \r
+    @Override\r
+    public void onSavedCertificate() {\r
+        startSynchronization();                \r
+    }\r
+\r
+\r
+    @Override\r
+    public void onFailedSavingCertificate() {\r
+        showDialog(DIALOG_CERT_NOT_SAVED);\r
+    }\r
+\r
+\r
 }\r