Run ExistenceCheckRemoteOperation on OperationsService
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.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 ExistenceCheckRemoteOperation mAuthCheckOperation;\r
-\r
+    //private ExistenceCheckRemoteOperation mAuthCheckOperation;\r
+    private int mExistenceCheckOpId = -1;\r
+    \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
-        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
+    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
@@ -843,9 +864,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         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
@@ -864,11 +886,12 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             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
-                onSamlBasedFederatedSingleSignOnAuthorizationStart(operation, result);\r
+                onSamlBasedFederatedSingleSignOnAuthorizationStart(result);\r
 \r
             } else {\r
-                onAuthorizationCheckFinish((ExistenceCheckRemoteOperation)operation, result);\r
+                onAuthorizationCheckFinish(result);\r
             }\r
         } else if (operation instanceof GetRemoteUserNameOperation) {\r
             onGetUserNameFinish((GetRemoteUserNameOperation) operation, result);\r
@@ -944,7 +967,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
 \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
@@ -1055,8 +1078,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                     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
@@ -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
-            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
@@ -1293,7 +1316,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * @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
@@ -1878,6 +1901,20 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 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