Browse Source

Updating synchronization for providing SSL warning when necessary; STEP 2: added SSL warning dialog in FileDisplayActivity

David A. Velasco 12 years ago
parent
commit
808d2a74ab

+ 41 - 15
src/com/owncloud/android/syncadapter/FileSyncAdapter.java

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

+ 66 - 7
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -71,7 +71,10 @@ import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.network.OwnCloudClientUtils;
+import com.owncloud.android.operations.RemoteOperationResult;
 import com.owncloud.android.syncadapter.FileSyncService;
+import com.owncloud.android.ui.dialog.SslValidatorDialog;
+import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;
 import com.owncloud.android.ui.fragment.FileDetailFragment;
 import com.owncloud.android.ui.fragment.OCFileListFragment;
 
@@ -86,7 +89,7 @@ import eu.alefzero.webdav.WebdavClient;
  */
 
 public class FileDisplayActivity extends SherlockFragmentActivity implements
-    OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener {
+    OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener {
     
     private ArrayAdapter<String> mDirectories;
     private OCFile mCurrentDir = null;
@@ -99,6 +102,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     private FileDownloaderBinder mDownloaderBinder = null;
     private FileUploaderBinder mUploaderBinder = null;
     private ServiceConnection mDownloadConnection = null, mUploadConnection = null;
+    private RemoteOperationResult mLastSslUntrustedServerResult = null;
     
     private OCFileListFragment mFileList;
     
@@ -109,6 +113,9 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     private static final int DIALOG_ABOUT_APP = 2;
     public static final int DIALOG_SHORT_WAIT = 3;
     private static final int DIALOG_CHOOSE_UPLOAD_SOURCE = 4;
+    private static final int DIALOG_SSL_VALIDATOR = 5;
+    private static final int DIALOG_CERT_NOT_SAVED = 6;
+
     
     private static final int ACTION_SELECT_CONTENT_FROM_APPS = 1;
     private static final int ACTION_SELECT_MULTIPLE_FILES = 2;
@@ -274,12 +281,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 break;
             }
             case R.id.startSync: {
-                ContentResolver.cancelSync(null, AccountAuthenticator.AUTH_TOKEN_TYPE);   // cancel the current synchronizations of any ownCloud account
-                Bundle bundle = new Bundle();
-                bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
-                ContentResolver.requestSync(
-                        AccountUtils.getCurrentOwnCloudAccount(this),
-                        AccountAuthenticator.AUTH_TOKEN_TYPE, bundle);
+                startSynchronization();
                 break;
             }
             case R.id.action_upload: {
@@ -307,6 +309,16 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         return retval;
     }
 
+    private void startSynchronization() {
+        ContentResolver.cancelSync(null, AccountAuthenticator.AUTH_TOKEN_TYPE);   // cancel the current synchronizations of any ownCloud account
+        Bundle bundle = new Bundle();
+        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
+        ContentResolver.requestSync(
+                AccountUtils.getCurrentOwnCloudAccount(this),
+                AccountAuthenticator.AUTH_TOKEN_TYPE, bundle);
+    }
+
+
     @Override
     public boolean onNavigationItemSelected(int itemPosition, long itemId) {
         int i = itemPosition;
@@ -520,6 +532,15 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         Log.d(getClass().toString(), "onPause() end");
     }
 
+    
+    @Override
+    protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
+        if (id == DIALOG_SSL_VALIDATOR && mLastSslUntrustedServerResult != null) {
+            ((SslValidatorDialog)dialog).updateResult(mLastSslUntrustedServerResult);
+        }
+    }
+
+    
     @Override
     protected Dialog onCreateDialog(int id) {
         Dialog dialog = null;
@@ -645,6 +666,23 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             dialog = builder.create();
             break;
         }
+        case DIALOG_SSL_VALIDATOR: {
+            dialog = SslValidatorDialog.newInstance(this, mLastSslUntrustedServerResult, this);
+            break;
+        }
+        case DIALOG_CERT_NOT_SAVED: {
+            builder = new AlertDialog.Builder(this);
+            builder.setMessage(getResources().getString(R.string.ssl_validator_not_saved));
+            builder.setCancelable(false);
+            builder.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialog, int which) {
+                        dialog.dismiss();
+                    };
+                });
+            dialog = builder.create();
+            break;
+        }
         default:
             dialog = null;
         }
@@ -772,6 +810,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }
 
     private class SyncBroadcastReceiver extends BroadcastReceiver {
+
         /**
          * {@link BroadcastReceiver} to enable syncing feedback in UI
          */
@@ -809,6 +848,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 setSupportProgressBarIndeterminateVisibility(inProgress);
                 
             }
+            
+            RemoteOperationResult synchResult = (RemoteOperationResult)intent.getSerializableExtra(FileSyncService.SYNC_RESULT);
+            if (synchResult != null) {
+                if (synchResult.getCode().equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)) {
+                    mLastSslUntrustedServerResult = synchResult;
+                    showDialog(DIALOG_SSL_VALIDATOR); 
+                }
+            }
         }
     }
     
@@ -1009,4 +1056,16 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }
 
 
+    @Override
+    public void onSavedCertificate() {
+        startSynchronization();                
+    }
+
+
+    @Override
+    public void onFailedSavingCertificate() {
+        showDialog(DIALOG_CERT_NOT_SAVED);
+    }
+
+
 }