Convert accessRootFolderRemoteOperation method in AsyncTask
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index 8d7182d..a7774ff 100644 (file)
 \r
 package com.owncloud.android.authentication;\r
 \r
 \r
 package com.owncloud.android.authentication;\r
 \r
+import java.io.IOException;\r
 import java.security.cert.X509Certificate;\r
 import java.util.Map;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
 import java.security.cert.X509Certificate;\r
 import java.util.Map;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
+import android.accounts.AuthenticatorException;\r
+import android.accounts.OperationCanceledException;\r
 import android.app.Dialog;\r
 import android.content.ComponentName;\r
 import android.content.Context;\r
 import android.app.Dialog;\r
 import android.content.ComponentName;\r
 import android.content.Context;\r
@@ -64,6 +67,9 @@ import com.actionbarsherlock.app.SherlockDialogFragment;
 import com.owncloud.android.MainApp;\r
 import com.owncloud.android.R;\r
 import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;\r
 import com.owncloud.android.MainApp;\r
 import com.owncloud.android.R;\r
 import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;\r
+import com.owncloud.android.lib.common.OwnCloudAccount;\r
+import com.owncloud.android.lib.common.OwnCloudClient;\r
+import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;\r
 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;\r
 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;\r
 import com.owncloud.android.lib.common.network.CertificateCombinedException;\r
 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;\r
 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;\r
 import com.owncloud.android.lib.common.network.CertificateCombinedException;\r
@@ -95,8 +101,9 @@ import com.owncloud.android.utils.DisplayUtils;
  * @author masensio\r
  */\r
 public class AuthenticatorActivity extends AccountAuthenticatorActivity\r
  * @author masensio\r
  */\r
 public class AuthenticatorActivity extends AccountAuthenticatorActivity\r
-implements  OnRemoteOperationListener, OnFocusChangeListener, OnEditorActionListener, \r
-SsoWebViewClientListener, OnSslUntrustedCertListener {\r
+        implements  OnRemoteOperationListener, OnFocusChangeListener, OnEditorActionListener,\r
+        SsoWebViewClientListener, OnSslUntrustedCertListener,\r
+        AuthenticatorAsyncTask.OnAuthenticatorTaskListener {\r
 \r
     private static final String TAG = AuthenticatorActivity.class.getSimpleName();\r
 \r
 \r
     private static final String TAG = AuthenticatorActivity.class.getSimpleName();\r
 \r
@@ -222,6 +229,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             mWaitingForOpId = savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID);\r
             mIsFirstAuthAttempt = savedInstanceState.getBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG);\r
         }\r
             mWaitingForOpId = savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID);\r
             mIsFirstAuthAttempt = savedInstanceState.getBoolean(KEY_AUTH_IS_FIRST_ATTEMPT_TAG);\r
         }\r
+\r
+\r
         \r
         /// load user interface\r
         setContentView(R.layout.account_setup);\r
         \r
         /// load user interface\r
         setContentView(R.layout.account_setup);\r
@@ -549,7 +558,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * intended to defer the processing of the redirection caught in \r
      * {@link #onNewIntent(Intent)} until {@link #onResume()} \r
      * \r
      * intended to defer the processing of the redirection caught in \r
      * {@link #onNewIntent(Intent)} until {@link #onResume()} \r
      * \r
-     * See {@link #loadSavedInstanceState(Bundle)}\r
+     * See {@link #onSaveInstanceState(Bundle)}\r
      */\r
     @Override\r
     protected void onSaveInstanceState(Bundle outState) {\r
      */\r
     @Override\r
     protected void onSaveInstanceState(Bundle outState) {\r
@@ -878,18 +887,16 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     }\r
 \r
     private void accessRootFolderRemoteOperation(String username, String password) {\r
     }\r
 \r
     private void accessRootFolderRemoteOperation(String username, String password) {\r
-        Intent existenceCheckIntent = new Intent();\r
-        existenceCheckIntent.setAction(OperationsService.ACTION_EXISTENCE_CHECK);\r
-        existenceCheckIntent.putExtra(OperationsService.EXTRA_SERVER_URL, mServerInfo.mBaseUrl);\r
-        existenceCheckIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, "/");\r
-        existenceCheckIntent.putExtra(OperationsService.EXTRA_USERNAME, username);\r
-        existenceCheckIntent.putExtra(OperationsService.EXTRA_PASSWORD, password);\r
-        existenceCheckIntent.putExtra(OperationsService.EXTRA_AUTH_TOKEN, mAuthToken);\r
-        \r
-        if (mOperationsServiceBinder != null) {\r
-            //Log_OC.wtf(TAG, "starting existenceCheckRemoteOperation..." );\r
-            mWaitingForOpId = mOperationsServiceBinder.queueNewOperation(existenceCheckIntent);\r
+        // delete the account if the token has changed\r
+        if (mAction == ACTION_UPDATE_TOKEN || mAction == ACTION_UPDATE_EXPIRED_TOKEN) {\r
+            // Remove the cookies in AccountManager\r
+            mAccountMgr.setUserData(mAccount, Constants.KEY_COOKIES, null);\r
         }\r
         }\r
+\r
+        AuthenticatorAsyncTask asyncTask = new AuthenticatorAsyncTask(this);\r
+        String[] params = { mServerInfo.mBaseUrl, username, password, mAuthToken, mAuthTokenType};\r
+        asyncTask.execute(params);\r
+\r
     }\r
 \r
     /**\r
     }\r
 \r
     /**\r
@@ -960,6 +967,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             onGetOAuthAccessTokenFinish(result);\r
 \r
         } else if (operation instanceof ExistenceCheckRemoteOperation)  {\r
             onGetOAuthAccessTokenFinish(result);\r
 \r
         } else if (operation instanceof ExistenceCheckRemoteOperation)  {\r
+            // TODO : remove this response??\r
             //Log_OC.wtf(TAG, "received detection response through callback" );\r
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).\r
                     equals(mAuthTokenType)) {\r
             //Log_OC.wtf(TAG, "received detection response through callback" );\r
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).\r
                     equals(mAuthTokenType)) {\r
@@ -1018,8 +1026,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) {\r
         mWaitingForOpId = Long.MAX_VALUE;\r
         dismissDialog(WAIT_DIALOG_TAG);\r
     private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) {\r
         mWaitingForOpId = Long.MAX_VALUE;\r
         dismissDialog(WAIT_DIALOG_TAG);\r
-
-        if (result.isIdPRedirection()) {
+\r
+        if (result.isIdPRedirection()) {\r
             String targetUrl = mServerInfo.mBaseUrl \r
                     + AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType);\r
 \r
             String targetUrl = mServerInfo.mBaseUrl \r
                     + AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType);\r
 \r
@@ -1042,8 +1050,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     /**\r
      * Processes the result of the server check performed when the user finishes the enter of the\r
      * server URL.\r
     /**\r
      * Processes the result of the server check performed when the user finishes the enter of the\r
      * server URL.\r
-     * \r
-     * @param operation     Server check performed.\r
+     *\r
      * @param result        Result of the check.\r
      */\r
     private void onGetServerInfoFinish(RemoteOperationResult result) {\r
      * @param result        Result of the check.\r
      */\r
     private void onGetServerInfoFinish(RemoteOperationResult result) {\r
@@ -1353,8 +1360,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * Processes the result of the access check performed to try the user credentials.\r
      * \r
      * Creates a new account through the AccountManager.\r
      * Processes the result of the access check performed to try the user credentials.\r
      * \r
      * Creates a new account through the AccountManager.\r
-     * \r
-     * @param operation     Access check performed.\r
+     *\r
      * @param result        Result of the operation.\r
      */\r
     private void onAuthorizationCheckFinish(RemoteOperationResult result) {\r
      * @param result        Result of the operation.\r
      */\r
     private void onAuthorizationCheckFinish(RemoteOperationResult result) {\r
@@ -1377,7 +1383,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 finish();\r
             }\r
             \r
                 finish();\r
             }\r
             \r
-        } else if (result.isServerFail() || result.isException()) {
+        } else if (result.isServerFail() || result.isException()) {\r
             /// server errors or exceptions in authorization take to requiring a new check of \r
             /// the server\r
             mServerIsChecked = true;\r
             /// server errors or exceptions in authorization take to requiring a new check of \r
             /// the server\r
             mServerIsChecked = true;\r
@@ -1507,14 +1513,14 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
                 mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);\r
             }\r
             /// add user data to the new account; TODO probably can be done in the last parameter \r
                 mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);\r
             }\r
             /// add user data to the new account; TODO probably can be done in the last parameter \r
-            //      addAccountExplicitly, or in KEY_USERDATA
+            //      addAccountExplicitly, or in KEY_USERDATA\r
             mAccountMgr.setUserData(\r
                     mAccount, Constants.KEY_OC_VERSION,    mServerInfo.mVersion.getVersion()\r
             );\r
             mAccountMgr.setUserData(\r
                     mAccount, Constants.KEY_OC_BASE_URL,   mServerInfo.mBaseUrl\r
             );\r
             mAccountMgr.setUserData(\r
                     mAccount, Constants.KEY_OC_VERSION,    mServerInfo.mVersion.getVersion()\r
             );\r
             mAccountMgr.setUserData(\r
                     mAccount, Constants.KEY_OC_BASE_URL,   mServerInfo.mBaseUrl\r
             );\r
-
+\r
             if (isSaml) {\r
                 mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); \r
             } else if (isOAuth) {\r
             if (isSaml) {\r
                 mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); \r
             } else if (isOAuth) {\r
@@ -1546,9 +1552,6 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     /**\r
      * Updates the content and visibility state of the icon and text associated\r
      * to the last check on the ownCloud server.\r
     /**\r
      * Updates the content and visibility state of the icon and text associated\r
      * to the last check on the ownCloud server.\r
-     * \r
-     * @param serverStatusText      Resource identifier of the text to show.\r
-     * @param serverStatusIcon      Resource identifier of the icon to show.\r
      */\r
     private void showServerStatus() {\r
         if (mServerStatusIcon == 0 && mServerStatusText == 0) {\r
      */\r
     private void showServerStatus() {\r
         if (mServerStatusIcon == 0 && mServerStatusText == 0) {\r
@@ -1891,4 +1894,17 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     public void doNegativeAuthenticatioDialogClick(){\r
         mIsFirstAuthAttempt = true;\r
     }\r
     public void doNegativeAuthenticatioDialogClick(){\r
         mIsFirstAuthAttempt = true;\r
     }\r
+\r
+\r
+    @Override\r
+    public void onAuthenticatorTaskCallback(RemoteOperationResult result) {\r
+        //Log_OC.wtf(TAG, "received detection response through callback" );\r
+        if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).\r
+                equals(mAuthTokenType)) {\r
+            onSamlBasedFederatedSingleSignOnAuthorizationStart(result);\r
+\r
+        } else {\r
+            onAuthorizationCheckFinish(result);\r
+        }\r
+    }\r
 }\r
 }\r