Moved execution of GetServerInfoOperation into OperationsService
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index dd20d48..d9e1bea 100644 (file)
@@ -45,7 +45,6 @@ import android.support.v4.app.FragmentTransaction;
 import android.text.Editable;\r
 import android.text.InputType;\r
 import android.text.TextWatcher;\r
-import android.util.Log;\r
 import android.view.KeyEvent;\r
 import android.view.MotionEvent;\r
 import android.view.View;\r
@@ -69,7 +68,6 @@ import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;\r
 import com.owncloud.android.lib.common.OwnCloudClientFactory;\r
 import com.owncloud.android.lib.common.OwnCloudClient;\r
-import com.owncloud.android.operations.DetectAuthenticationMethodOperation;\r
 import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod;\r
 import com.owncloud.android.operations.GetServerInfoOperation;\r
 import com.owncloud.android.operations.OAuth2GetAccessToken;\r
@@ -149,7 +147,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private int mAuthMessageVisibility, mServerStatusText, mServerStatusIcon;\r
     private boolean mServerIsChecked, mServerIsValid, mIsSslConn;\r
     private AuthenticationMethod mServerAuthMethod = AuthenticationMethod.UNKNOWN;\r
-    private int mDetectAuthOpId = -1;\r
+    private int mGetServerInfoOpId = -1;\r
 \r
     private int mAuthStatusText, mAuthStatusIcon;    \r
     private TextView mAuthStatusLayout;\r
@@ -341,7 +339,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
 \r
             mServerAuthMethod = AuthenticationMethod.valueOf(\r
                     savedInstanceState.getString(KEY_SERVER_AUTH_METHOD));\r
-            mDetectAuthOpId = savedInstanceState.getInt(KEY_DETECT_AUTH_OP_ID);\r
+            mGetServerInfoOpId = savedInstanceState.getInt(KEY_DETECT_AUTH_OP_ID);\r
 \r
         }\r
 \r
@@ -364,7 +362,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             mOAuth2Check.setVisibility(View.GONE);\r
         }\r
 \r
-        //showRefreshButton(mServerIsChecked && !mServerIsValid && mRefreshButtonEnabled);\r
         showRefreshButton(mServerIsChecked && !mServerIsValid && refreshButtonEnabled);\r
         mOkButton.setEnabled(mServerIsValid); // state not automatically recovered in configuration changes\r
 \r
@@ -514,7 +511,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         outState.putBoolean(KEY_REFRESH_BUTTON_ENABLED, (mRefreshButton.getVisibility() == View.VISIBLE));\r
         \r
         outState.putString(KEY_SERVER_AUTH_METHOD, mServerAuthMethod.name());\r
-        outState.putInt(KEY_DETECT_AUTH_OP_ID, mDetectAuthOpId);\r
+        outState.putInt(KEY_DETECT_AUTH_OP_ID, mGetServerInfoOpId);\r
         //Log.wtf(TAG, "onSaveInstanceState end" );\r
     }\r
 \r
@@ -674,21 +671,21 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             mServerStatusText = R.string.auth_testing_connection;\r
             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
-            /*\r
-             * TODO start joint operation in OperationsService\r
-            Intent detectAuthIntent = new Intent();\r
-            detectAuthIntent.setAction(OperationsService.ACTION_DETECT_AUTHENTICATION_METHOD);\r
-            detectAuthIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mBaseUrl);\r
-            detectAuthIntent.putExtra(OperationsService.EXTRA_WEBDAV_PATH, webdav_path);\r
+            Intent getServerInfoIntent = new Intent();\r
+            getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO);\r
+            getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL, uri);\r
+            getServerInfoIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN_TYPE, mAuthTokenType);\r
             if (mOperationsServiceBinder != null) {\r
-                //Log.wtf(TAG, "starting detection..." );\r
-                mDetectAuthOpId = mOperationsServiceBinder.newOperation(detectAuthIntent);\r
+                //Log.wtf(TAG, "checking server..." );\r
+                mGetServerInfoOpId = mOperationsServiceBinder.newOperation(getServerInfoIntent);\r
             }\r
-            */\r
             \r
         } else {\r
             mServerStatusText = 0;\r
@@ -878,11 +875,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {\r
 \r
         if (operation instanceof GetServerInfoOperation) {\r
-            if (operation.equals(mServerInfoOperation)) {\r
+            if (operation.hashCode() == mGetServerInfoOpId) {\r
                 onGetServerInfoFinish(result);\r
             }   // else nothing ; only the last check operation is considered; \r
-                // multiple can be triggered if the user amends a URL before a previous check \r
-                // can be triggered\r
+                // multiple can be started if the user amends a URL quickly\r
 \r
         } else if (operation instanceof OAuth2GetAccessToken) {\r
             onGetOAuthAccessTokenFinish((OAuth2GetAccessToken)operation, result);\r
@@ -897,44 +893,10 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         } else if (operation instanceof GetRemoteUserNameOperation) {\r
             onGetUserNameFinish((GetRemoteUserNameOperation) operation, result);\r
 \r
-        } else if (operation instanceof DetectAuthenticationMethodOperation) {\r
-            Log.wtf(TAG, "received detection response through callback" );\r
-            onDetectAuthenticationFinish(result);\r
         }\r
 \r
     }\r
 \r
-    private void onDetectAuthenticationFinish(RemoteOperationResult result) {\r
-        // Read authentication method\r
-        mDetectAuthOpId = -1;\r
-        if (result.getData().size() > 0) {\r
-            AuthenticationMethod authMethod = (AuthenticationMethod) result.getData().get(0);\r
-            String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType());\r
-            String oAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType());\r
-            String saml =  AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType());\r
-\r
-            if ( ( mAuthTokenType.equals(basic) && !authMethod.equals(AuthenticationMethod.BASIC_HTTP_AUTH) ) ||\r
-                    ( mAuthTokenType.equals(oAuth) && !authMethod.equals(AuthenticationMethod.BEARER_TOKEN) ) || \r
-                    ( mAuthTokenType.equals(saml)  && !authMethod.equals(AuthenticationMethod.SAML_WEB_SSO) ) ) {\r
-\r
-                mOkButton.setEnabled(false);\r
-                mServerIsValid = false;\r
-                //show an alert message ( Server Status )\r
-                updateServerStatusIconNoRegularAuth();\r
-                showServerStatus();\r
-\r
-            } else {\r
-                mOkButton.setEnabled(true);\r
-\r
-                // Show server status\r
-                showServerStatus();\r
-            }\r
-\r
-        }\r
-    }\r
-\r
-\r
-\r
     private void onGetUserNameFinish(GetRemoteUserNameOperation operation, RemoteOperationResult result) {\r
 \r
         if (result.isSuccess()) {\r
@@ -1008,7 +970,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         mServerIsChecked = true;\r
         mIsSslConn = (result.getCode() == ResultCode.OK_SSL);\r
         mServerInfoOperation = null;\r
-        mDetectAuthOpId = -1;\r
+        mGetServerInfoOpId = -1;\r
         \r
         // update server status, but don't show it yet\r
         updateServerStatusIconAndText(result);\r
@@ -1022,8 +984,9 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             mServerInfo = (GetServerInfoOperation.ServerInfo) (result.getData().get(0));\r
             mDiscoveredVersion = mServerInfo.mVersion;\r
             mHostBaseUrl = mServerInfo.mBaseUrl;\r
+            mServerAuthMethod = mServerInfo.mAuthMethod;\r
             \r
-            if (!authSupported(mServerInfo.mAuthMethod)) {\r
+            if (!authSupported(mServerAuthMethod)) {\r
                 \r
                 updateServerStatusIconNoRegularAuth();  // overrides updateServerStatusIconAndText()  \r
                 mServerIsValid = false;\r
@@ -1063,32 +1026,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     }\r
 \r
 \r
-    /**\r
-     *  Try to access with  user/pass ""/"", to know if it is a regular server\r
-     */\r
-    private void detectAuthorizationMethod() {\r
-\r
-        Log_OC.d(TAG, "Trying empty authorization to detect authentication method");\r
-        \r
-        String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, mAuthTokenType);\r
-        \r
-        /// test credentials \r
-        //Intent detectAuthIntent = new Intent(this, OperationsService.class);\r
-        Intent detectAuthIntent = new Intent();\r
-        detectAuthIntent.setAction(OperationsService.ACTION_DETECT_AUTHENTICATION_METHOD);\r
-        detectAuthIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mHostBaseUrl);\r
-        detectAuthIntent.putExtra(OperationsService.EXTRA_WEBDAV_PATH, webdav_path);\r
-        \r
-        //if (mOperationsBinder != null) {  // let's let it crash to detect if is really possible\r
-        mServerAuthMethod = AuthenticationMethod.UNKNOWN;\r
-        if (mOperationsServiceBinder != null) {\r
-            //Log.wtf(TAG, "starting detection..." );\r
-            mDetectAuthOpId = mOperationsServiceBinder.newOperation(detectAuthIntent);\r
-        }\r
-        //}\r
-    }\r
-\r
-\r
     // TODO remove, if possible\r
     private String normalizeUrl(String url) {\r
         if (url != null && url.length() > 0) {\r
@@ -1918,12 +1855,12 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         //Log.wtf(TAG, "registering to listen for operation callbacks" );\r
         mOperationsServiceBinder.addOperationListener(AuthenticatorActivity.this, mHandler);\r
         \r
-        if (mDetectAuthOpId != -1) {\r
+        if (mGetServerInfoOpId != -1) {\r
             RemoteOperationResult result = \r
-                    mOperationsServiceBinder.getOperationResultIfFinished(mDetectAuthOpId);\r
+                    mOperationsServiceBinder.getOperationResultIfFinished(mGetServerInfoOpId);\r
             if (result != null) {\r
                 //Log.wtf(TAG, "found result of operation finished while rotating");\r
-                onDetectAuthenticationFinish(result);\r
+                onGetServerInfoFinish(result);\r
             }\r
         }\r
     }\r