Moved OAuth2GetAccessToken to be run in OperationsService
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index d9e1bea..84362ba 100644 (file)
@@ -19,6 +19,7 @@
 package com.owncloud.android.authentication;\r
 \r
 import java.security.cert.X509Certificate;\r
+import java.util.Map;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
@@ -147,7 +148,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private int mAuthMessageVisibility, mServerStatusText, mServerStatusIcon;\r
     private boolean mServerIsChecked, mServerIsValid, mIsSslConn;\r
     private AuthenticationMethod mServerAuthMethod = AuthenticationMethod.UNKNOWN;\r
+\r
     private int mGetServerInfoOpId = -1;\r
+    private int mOauth2GetAccessTokenOpId = -1;\r
 \r
     private int mAuthStatusText, mAuthStatusIcon;    \r
     private TextView mAuthStatusLayout;\r
@@ -602,7 +605,24 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         /// Showing the dialog with instructions for the user.\r
         showDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);\r
 \r
-        /// GET ACCESS TOKEN to the oAuth server \r
+        /// GET ACCESS TOKEN to the oAuth server\r
+        Intent getServerInfoIntent = new Intent();\r
+        getServerInfoIntent.setAction(OperationsService.ACTION_OAUTH2_GET_ACCESS_TOKEN);\r
+        \r
+        getServerInfoIntent.putExtra(\r
+                OperationsService.EXTRA_SERVER_URL, \r
+                mOAuthTokenEndpointText.getText().toString().trim());\r
+        \r
+        getServerInfoIntent.putExtra(\r
+                OperationsService.EXTRA_OAUTH2_QUERY_PARAMETERS, \r
+                queryParameters);\r
+        \r
+        if (mOperationsServiceBinder != null) {\r
+            //Log.wtf(TAG, "getting access token..." );\r
+            mOauth2GetAccessTokenOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent);\r
+        }\r
+        \r
+        /*\r
         RemoteOperation operation = new OAuth2GetAccessToken(   getString(R.string.oauth2_client_id), \r
                 getString(R.string.oauth2_redirect_uri),       \r
                 getString(R.string.oauth2_grant_type),\r
@@ -610,6 +630,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         //OwnCloudClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(getString(R.string.oauth2_url_endpoint_access)), getApplicationContext());\r
         OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(mOAuthTokenEndpointText.getText().toString().trim()), getApplicationContext(), true);\r
         operation.execute(client, this, mHandler);\r
+        */\r
+        \r
     }\r
 \r
 \r
@@ -672,12 +694,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             mServerStatusIcon = R.drawable.progress_small;\r
             showServerStatus();\r
             \r
-            /*\r
-            mServerInfoOperation = new GetServerInfoOperation(uri, mAuthTokenType, this);\r
-            OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(uri), this, true);\r
-            mServerInfoOperation.execute(client, this, mHandler);\r
-            */\r
-            \r
             Intent getServerInfoIntent = new Intent();\r
             getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO);\r
             getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL, uri);\r
@@ -828,7 +844,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         mAuthStatusText = R.string.oauth_login_connection;\r
         showAuthStatus();\r
 \r
-\r
         // GET AUTHORIZATION request\r
         //Uri uri = Uri.parse(getString(R.string.oauth2_url_endpoint_auth));\r
         Uri uri = Uri.parse(mOAuthAuthEndpointText.getText().toString().trim());\r
@@ -881,7 +896,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 // multiple can be started if the user amends a URL quickly\r
 \r
         } else if (operation instanceof OAuth2GetAccessToken) {\r
-            onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);\r
+            onGetOAuthAccessTokenFinish(result);\r
 \r
         } else if (operation instanceof ExistenceCheckRemoteOperation)  {\r
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {\r
@@ -1238,10 +1253,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * Processes the result of the request for and access token send \r
      * to an OAuth authorization server.\r
      * \r
-     * @param operation     Operation performed requesting the access token.\r
      * @param result        Result of the operation.\r
      */\r
-    private void onGetOAuthAccessTokenFinish(OAuth2GetAccessToken operation, RemoteOperationResult result) {\r
+    private void onGetOAuthAccessTokenFinish(RemoteOperationResult result) {\r
+        mOauth2GetAccessTokenOpId = -1;\r
         try {\r
             dismissDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);\r
         } catch (IllegalArgumentException e) {\r
@@ -1254,7 +1269,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             showDialog(DIALOG_LOGIN_PROGRESS);\r
 \r
             /// time to test the retrieved access token on the ownCloud server\r
-            mAuthToken = ((OAuth2GetAccessToken)operation).getResultTokenMap().get(OAuth2Constants.KEY_ACCESS_TOKEN);\r
+            @SuppressWarnings("unchecked")\r
+            Map<String, String> tokens = (Map<String, String>)(result.getData().get(0));\r
+            mAuthToken = tokens.get(OAuth2Constants.KEY_ACCESS_TOKEN);\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
@@ -1862,6 +1880,15 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 //Log.wtf(TAG, "found result of operation finished while rotating");\r
                 onGetServerInfoFinish(result);\r
             }\r
+            \r
+        } else if (mOauth2GetAccessTokenOpId != -1) {\r
+            RemoteOperationResult result = \r
+                    mOperationsServiceBinder.getOperationResultIfFinished(\r
+                            mOauth2GetAccessTokenOpId);\r
+            if (result != null) {\r
+                //Log.wtf(TAG, "found result of operation finished while rotating");\r
+                onGetOAuthAccessTokenFinish(result);\r
+            }\r
         }\r
     }\r
     \r