Run ExistenceCheckRemoteOperation on OperationsService
authormasensio <masensio@solidgear.es>
Tue, 1 Apr 2014 12:51:53 +0000 (14:51 +0200)
committermasensio <masensio@solidgear.es>
Tue, 1 Apr 2014 12:51:53 +0000 (14:51 +0200)
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/services/OperationsService.java

index 69eea77..3c14404 100644 (file)
@@ -156,8 +156,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private final Handler mHandler = new Handler();\r
     private Thread mOperationThread;\r
     private GetRemoteStatusOperation mOcServerChkOperation;\r
     private final Handler mHandler = new Handler();\r
     private Thread mOperationThread;\r
     private GetRemoteStatusOperation mOcServerChkOperation;\r
-    private ExistenceCheckRemoteOperation mAuthCheckOperation;\r
-\r
+    //private ExistenceCheckRemoteOperation mAuthCheckOperation;\r
+    private int mExistenceCheckOpId = -1;\r
+    \r
     private Uri mNewCapturedUriFromOAuth2Redirection;\r
 \r
     private AccountManager mAccountMgr;\r
     private Uri mNewCapturedUriFromOAuth2Redirection;\r
 \r
     private AccountManager mAccountMgr;\r
@@ -794,12 +795,32 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         showDialog(DIALOG_LOGIN_PROGRESS);\r
 \r
         /// test credentials accessing the root folder\r
         showDialog(DIALOG_LOGIN_PROGRESS);\r
 \r
         /// test credentials accessing the root folder\r
-        mAuthCheckOperation = new  ExistenceCheckRemoteOperation("", this, false);\r
-        OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
-        client.setBasicCredentials(username, password);\r
-        mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
+        String remotePath ="";\r
+        boolean successIfAbsent = false;\r
+        boolean followRedirects = true;\r
+        startExistenceCheckRemoteOperation(remotePath, this, successIfAbsent, webdav_path, username, password, followRedirects);\r
+        \r
     }\r
 \r
     }\r
 \r
+    private void startExistenceCheckRemoteOperation(String remotePath, Context context, boolean successIfAbsent, String webdav_path,\r
+            String username, String password, boolean followRedirects) {\r
+        \r
+        Intent existenceCheckIntent = new Intent();\r
+        existenceCheckIntent.setAction(OperationsService.ACTION_EXISTENCE_CHECK);\r
+        existenceCheckIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mHostBaseUrl);\r
+        existenceCheckIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);\r
+        existenceCheckIntent.putExtra(OperationsService.EXTRA_SUCCESS_IF_ABSENT, successIfAbsent);\r
+        existenceCheckIntent.putExtra(OperationsService.EXTRA_WEBDAV_PATH, webdav_path);\r
+        existenceCheckIntent.putExtra(OperationsService.EXTRA_USERNAME, username);\r
+        existenceCheckIntent.putExtra(OperationsService.EXTRA_PASSWORD, password);\r
+        existenceCheckIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN, mAuthToken);\r
+        existenceCheckIntent.putExtra(OperationsService.EXTRA_FOLLOW_REDIRECTS, followRedirects);\r
+        \r
+        if (mOperationsServiceBinder != null) {\r
+            Log.wtf(TAG, "starting existenceCheckRemoteOperation..." );\r
+            mExistenceCheckOpId = mOperationsServiceBinder.newOperation(existenceCheckIntent);\r
+        }\r
+    }\r
 \r
     /**\r
      * Starts the OAuth 'grant type' flow to get an access token, with \r
 \r
     /**\r
      * Starts the OAuth 'grant type' flow to get an access token, with \r
@@ -843,9 +864,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);\r
 \r
         /// test credentials accessing the root folder\r
         String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);\r
 \r
         /// test credentials accessing the root folder\r
-        mAuthCheckOperation = new  ExistenceCheckRemoteOperation("", this, false);\r
-        OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, false);\r
-        mOperationThread = mAuthCheckOperation.execute(client, this, mHandler);\r
+        String remotePath ="";\r
+        boolean successIfAbsent = false;\r
+        boolean followRedirections = false;\r
+        startExistenceCheckRemoteOperation(remotePath, this, successIfAbsent, webdav_path, "", "", followRedirections);\r
 \r
     }\r
 \r
 \r
     }\r
 \r
@@ -864,11 +886,12 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);\r
 \r
         } else if (operation instanceof ExistenceCheckRemoteOperation)  {\r
             onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);\r
 \r
         } else if (operation instanceof ExistenceCheckRemoteOperation)  {\r
+            Log.wtf(TAG, "received detection response through callback" );\r
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
-                onSamlBasedFederatedSingleSignOnAuthorizationStart(operation, result);\r
+                onSamlBasedFederatedSingleSignOnAuthorizationStart(result);\r
 \r
             } else {\r
 \r
             } else {\r
-                onAuthorizationCheckFinish((ExistenceCheckRemoteOperation)operation, result);\r
+                onAuthorizationCheckFinish(result);\r
             }\r
         } else if (operation instanceof GetRemoteUserNameOperation) {\r
             onGetUserNameFinish((GetRemoteUserNameOperation) operation, result);\r
             }\r
         } else if (operation instanceof GetRemoteUserNameOperation) {\r
             onGetUserNameFinish((GetRemoteUserNameOperation) operation, result);\r
@@ -944,7 +967,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
 \r
     }\r
 \r
 \r
     }\r
 \r
-    private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperation operation, RemoteOperationResult result) {\r
+    private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) {\r
         try {\r
             dismissDialog(DIALOG_LOGIN_PROGRESS);\r
         } catch (IllegalArgumentException e) {\r
         try {\r
             dismissDialog(DIALOG_LOGIN_PROGRESS);\r
         } catch (IllegalArgumentException e) {\r
@@ -1055,8 +1078,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                     url = "http://" + url;\r
                 }\r
             }\r
                     url = "http://" + url;\r
                 }\r
             }\r
-\r
-            // OC-208: Add suffix remote.php/webdav to normalize (OC-34)            \r
+            \r
             url = trimUrlWebdav(url);\r
 \r
             if (url.endsWith("/")) {\r
             url = trimUrlWebdav(url);\r
 \r
             if (url.endsWith("/")) {\r
@@ -1272,10 +1294,11 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             /// time to test the retrieved access token on the ownCloud server\r
             mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
             Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken);\r
             /// time to test the retrieved access token on the ownCloud server\r
             mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
             Log_OC.d(TAG, "Got ACCESS TOKEN: " + mAuthToken);\r
-            mAuthCheckOperation = new ExistenceCheckRemoteOperation("", this, false);\r
-            OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mHostBaseUrl + webdav_path), this, true);\r
-            client.setBearerCredentials(mAuthToken);\r
-            mAuthCheckOperation.execute(client, this, mHandler);\r
+            \r
+            String remotePath ="";\r
+            boolean successIfAbsent = false;\r
+            boolean followRedirects = true;\r
+            startExistenceCheckRemoteOperation(remotePath, this, successIfAbsent, webdav_path, "", "", followRedirects);\r
 \r
         } else {\r
             updateAuthStatusIconAndText(result);\r
 \r
         } else {\r
             updateAuthStatusIconAndText(result);\r
@@ -1293,7 +1316,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * @param operation     Access check performed.\r
      * @param result        Result of the operation.\r
      */\r
      * @param operation     Access check performed.\r
      * @param result        Result of the operation.\r
      */\r
-    private void onAuthorizationCheckFinish(ExistenceCheckRemoteOperation operation, RemoteOperationResult result) {\r
+    private void onAuthorizationCheckFinish(RemoteOperationResult result) {\r
         try {\r
             dismissDialog(DIALOG_LOGIN_PROGRESS);\r
         } catch (IllegalArgumentException e) {\r
         try {\r
             dismissDialog(DIALOG_LOGIN_PROGRESS);\r
         } catch (IllegalArgumentException e) {\r
@@ -1878,6 +1901,20 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 onDetectAuthenticationFinish(result);\r
             }\r
         }\r
                 onDetectAuthenticationFinish(result);\r
             }\r
         }\r
+        \r
+        if (mExistenceCheckOpId != -1) {\r
+            RemoteOperationResult result = \r
+                    mOperationsServiceBinder.getOperationResultIfFinished(mExistenceCheckOpId);\r
+            if (result != null) {\r
+                Log.wtf(TAG, "found result of operation finished while rotating");\r
+                if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
+                    onSamlBasedFederatedSingleSignOnAuthorizationStart(result);\r
+\r
+                } else {\r
+                    onAuthorizationCheckFinish(result);\r
+                }\r
+            }\r
+        }\r
     }\r
     \r
     /** \r
     }\r
     \r
     /** \r
index 0ccd909..d44a3af 100644 (file)
@@ -29,6 +29,7 @@ import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.operations.common.SyncOperation;
 import com.owncloud.android.operations.CreateShareOperation;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.operations.common.SyncOperation;
 import com.owncloud.android.operations.CreateShareOperation;
@@ -60,10 +61,16 @@ public class OperationsService extends Service {
     public static final String EXTRA_SEND_INTENT = "SEND_INTENT";
     public static final String EXTRA_RESULT = "RESULT";
     public static final String EXTRA_WEBDAV_PATH = "WEBDAV_PATH";
     public static final String EXTRA_SEND_INTENT = "SEND_INTENT";
     public static final String EXTRA_RESULT = "RESULT";
     public static final String EXTRA_WEBDAV_PATH = "WEBDAV_PATH";
+    public static final String EXTRA_SUCCESS_IF_ABSENT = "SUCCESS_IF_ABSENT";
+    public static final String EXTRA_USERNAME = "USERNAME";
+    public static final String EXTRA_PASSWORD = "PASSWORD";
+    public static final String EXTRA_AUTH_TOKEN = "AUTH_TOKEN";
+    public static final String EXTRA_FOLLOW_REDIRECTS = "FOLLOW_REDIRECTS";
     
     public static final String ACTION_CREATE_SHARE = "CREATE_SHARE";
     public static final String ACTION_UNSHARE = "UNSHARE";
     public static final String ACTION_DETECT_AUTHENTICATION_METHOD = "DETECT_AUTHENTICATION_METHOD";
     
     public static final String ACTION_CREATE_SHARE = "CREATE_SHARE";
     public static final String ACTION_UNSHARE = "UNSHARE";
     public static final String ACTION_DETECT_AUTHENTICATION_METHOD = "DETECT_AUTHENTICATION_METHOD";
+    public static final String ACTION_EXISTENCE_CHECK = "EXISTENCE_CHECK";
     
     public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED";
     public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED";
     
     public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED";
     public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED";
@@ -77,9 +84,21 @@ public class OperationsService extends Service {
     private static class Target {
         public Uri mServerUrl = null;
         public Account mAccount = null;
     private static class Target {
         public Uri mServerUrl = null;
         public Account mAccount = null;
-        public Target(Account account, Uri serverUrl) {
+        public String mWebDavUrl = "";
+        public String mUsername = "";
+        public String mPassword = "";
+        public String mAuthToken = "";
+        public boolean mFollowRedirects = true;
+        
+        public Target(Account account, Uri serverUrl, String webdavUrl, String username, String password, String authToken,
+                boolean followRedirects) {
             mAccount = account;
             mServerUrl = serverUrl;
             mAccount = account;
             mServerUrl = serverUrl;
+            mWebDavUrl = webdavUrl;
+            mUsername = username;
+            mPassword = password;
+            mAuthToken = authToken;
+            mFollowRedirects = followRedirects;
         }
     }
 
         }
     }
 
@@ -228,9 +247,20 @@ public class OperationsService extends Service {
                 } else {
                     Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT);
                     String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL);
                 } else {
                     Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT);
                     String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL);
+                    String webDavPath = operationIntent.getStringExtra(EXTRA_WEBDAV_PATH);
+                    String webDavUrl = serverUrl + webDavPath;
+                    String username = operationIntent.getStringExtra(EXTRA_USERNAME);
+                    String password = operationIntent.getStringExtra(EXTRA_PASSWORD);
+                    String authToken = operationIntent.getStringExtra(EXTRA_AUTH_TOKEN);
+                    boolean followRedirects = operationIntent.getBooleanExtra(EXTRA_FOLLOW_REDIRECTS, true);
                     target = new Target(
                             account, 
                     target = new Target(
                             account, 
-                            (serverUrl == null) ? null : Uri.parse(serverUrl)
+                            (serverUrl == null) ? null : Uri.parse(serverUrl),
+                            ((webDavPath == null) || (serverUrl == null)) ? "" : webDavUrl,
+                            (username == null) ? "" : username,
+                            (password == null) ? "" : password,
+                            (authToken == null) ? "" : authToken,
+                            followRedirects
                     );
                     
                     String action = operationIntent.getAction();
                     );
                     
                     String action = operationIntent.getAction();
@@ -255,6 +285,12 @@ public class OperationsService extends Service {
                         operation = new DetectAuthenticationMethodOperation(
                                 OperationsService.this, 
                                 webdav_url);
                         operation = new DetectAuthenticationMethodOperation(
                                 OperationsService.this, 
                                 webdav_url);
+                    } else if (action.equals(ACTION_EXISTENCE_CHECK)) {
+                        // Existence Check 
+                        String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
+                        boolean successIfAbsent = operationIntent.getBooleanExtra(EXTRA_SUCCESS_IF_ABSENT, true);
+                        operation = new ExistenceCheckRemoteOperation(remotePath, OperationsService.this, successIfAbsent);
+                        
                     }
                 }
                     
                     }
                 }
                     
@@ -316,7 +352,7 @@ public class OperationsService extends Service {
         synchronized(mPendingOperations) {
             next = mPendingOperations.peek();
         }
         synchronized(mPendingOperations) {
             next = mPendingOperations.peek();
         }
-        
+
         if (next != null) {
             
             mCurrentOperation = next.second;
         if (next != null) {
             
             mCurrentOperation = next.second;
@@ -329,7 +365,16 @@ public class OperationsService extends Service {
                         mOwnCloudClient = OwnCloudClientFactory.createOwnCloudClient(mLastTarget.mAccount, getApplicationContext());
                         mStorageManager = new FileDataStorageManager(mLastTarget.mAccount, getContentResolver());
                     } else {
                         mOwnCloudClient = OwnCloudClientFactory.createOwnCloudClient(mLastTarget.mAccount, getApplicationContext());
                         mStorageManager = new FileDataStorageManager(mLastTarget.mAccount, getContentResolver());
                     } else {
-                        mOwnCloudClient = OwnCloudClientFactory.createOwnCloudClient(mLastTarget.mServerUrl, getApplicationContext(), true);    // this is not good enough
+                        mOwnCloudClient = OwnCloudClientFactory.createOwnCloudClient(mLastTarget.mServerUrl, getApplicationContext(), 
+                                mLastTarget.mFollowRedirects);    // this is not good enough
+                        if (mLastTarget.mWebDavUrl != "") {
+                            mOwnCloudClient.setWebdavUri(Uri.parse(mLastTarget.mWebDavUrl));
+                        }
+                        if (mLastTarget.mUsername != "" && mLastTarget.mPassword != "") {
+                            mOwnCloudClient.setBasicCredentials(mLastTarget.mUsername, mLastTarget.mPassword);
+                        } else if (mLastTarget.mAuthToken != "") {
+                            mOwnCloudClient.setBearerCredentials(mLastTarget.mAuthToken);
+                        }
                         mStorageManager = null;
                     }
                 }
                         mStorageManager = null;
                     }
                 }