Refresh authorization token on failed synchronizations when notification error is...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / AuthenticatorActivity.java
index 1185307..8dce3bc 100644 (file)
@@ -19,9 +19,6 @@
 \r
 package com.owncloud.android.ui.activity;\r
 \r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
 import com.owncloud.android.AccountUtils;\r
 import com.owncloud.android.authenticator.AccountAuthenticator;\r
 import com.owncloud.android.authenticator.oauth2.OAuth2Context;\r
@@ -35,6 +32,7 @@ import com.owncloud.android.operations.OAuth2GetAccessToken;
 import com.owncloud.android.operations.OnRemoteOperationListener;\r
 import com.owncloud.android.operations.RemoteOperation;\r
 import com.owncloud.android.operations.RemoteOperationResult;\r
+import com.owncloud.android.operations.RemoteOperationResult.ResultCode;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountAuthenticatorActivity;\r
@@ -60,6 +58,8 @@ import android.widget.EditText;
 import android.widget.Button;\r
 import android.widget.ImageView;\r
 import android.widget.TextView;\r
+import android.widget.Toast;\r
+\r
 import com.owncloud.android.R;\r
 \r
 import eu.alefzero.webdav.WebdavClient;\r
@@ -78,9 +78,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
     public static final String EXTRA_USER_NAME = "USER_NAME";\r
     public static final String EXTRA_HOST_NAME = "HOST_NAME";\r
-\r
+    public static final String EXTRA_ACTION = "ACTION";\r
+    \r
     private static final String KEY_HOST_URL_TEXT = "HOST_URL_TEXT";\r
     private static final String KEY_OC_VERSION = "OC_VERSION";\r
+    private static final String KEY_ACCOUNT = "ACCOUNT";\r
     private static final String KEY_STATUS_TEXT = "STATUS_TEXT";\r
     private static final String KEY_STATUS_ICON = "STATUS_ICON";\r
     private static final String KEY_STATUS_CORRECT = "STATUS_CORRECT";\r
@@ -93,6 +95,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private static final int DIALOG_CERT_NOT_SAVED = 2;\r
     private static final int DIALOG_OAUTH2_LOGIN_PROGRESS = 3;\r
 \r
+    public static final byte ACTION_CREATE = 0;\r
+    public static final byte ACTION_UPDATE_TOKEN = 1;\r
+\r
     \r
     private String mHostBaseUrl;\r
     private OwnCloudVersion mDiscoveredVersion;\r
@@ -114,6 +119,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private Uri mNewCapturedUriFromOAuth2Redirection;\r
     \r
     private AccountManager mAccountMgr;\r
+    private boolean mJustCreated;\r
+    private byte mAction;\r
+    private Account mAccount;\r
     \r
     private ImageView mRefreshButton;\r
     private ImageView mViewPasswordButton;\r
@@ -123,6 +131,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private CheckBox mOAuth2Check;\r
     private String mOAuthAccessToken;\r
     private View mOkButton;\r
+    private TextView mAuthStatusLayout;\r
     \r
     private TextView mOAuthAuthEndpointText;\r
     private TextView mOAuthTokenEndpointText;\r
@@ -149,6 +158,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         mOAuthTokenEndpointText = (TextView)findViewById(R.id.oAuthEntryPoint_2);\r
         mOAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check);\r
         mOkButton = findViewById(R.id.buttonOK);\r
+        mAuthStatusLayout = (TextView) findViewById(R.id.auth_status_text); \r
+        \r
 \r
         /// complete label for 'register account' button\r
         Button b = (Button) findViewById(R.id.account_register);\r
@@ -163,6 +174,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         /// initialization\r
         mAccountMgr = AccountManager.get(this);\r
         mNewCapturedUriFromOAuth2Redirection = null;    // TODO save?\r
+        mAction = getIntent().getByteExtra(EXTRA_ACTION, ACTION_CREATE); \r
+        mAccount = null;\r
 \r
         if (savedInstanceState == null) {\r
             /// connection state and info\r
@@ -173,26 +186,37 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             /// retrieve extras from intent\r
             String tokenType = getIntent().getExtras().getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE);\r
             boolean oAuthRequired = AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(tokenType);\r
-            mOAuth2Check.setChecked(oAuthRequired);\r
-            changeViewByOAuth2Check(oAuthRequired);\r
             \r
-            Account account = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT);\r
-            if (account != null) {\r
-                String ocVersion = mAccountMgr.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);\r
+            mAccount = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT);\r
+            if (mAccount != null) {\r
+                String ocVersion = mAccountMgr.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION);\r
                 if (ocVersion != null) {\r
                     mDiscoveredVersion = new OwnCloudVersion(ocVersion);\r
                 }\r
-                mHostBaseUrl = mAccountMgr.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
+                mHostBaseUrl = mAccountMgr.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
                 mHostUrlInput.setText(mHostBaseUrl);\r
-                String userName = account.name.substring(0, account.name.lastIndexOf('@'));\r
+                String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));\r
                 mUsernameInput.setText(userName);\r
+                oAuthRequired = (mAccountMgr.getUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);\r
             }\r
+            mOAuth2Check.setChecked(oAuthRequired);\r
+            changeViewByOAuth2Check(oAuthRequired);\r
+            \r
 \r
         } else {\r
             loadSavedInstanceState(savedInstanceState);\r
         }\r
         \r
+        if (mAction == ACTION_UPDATE_TOKEN) {\r
+            /// lock things that should not change\r
+            mHostUrlInput.setEnabled(false);\r
+            mUsernameInput.setEnabled(false);\r
+            mOAuth2Check.setVisibility(View.GONE);\r
+            checkOcServer(); \r
+        }\r
+        \r
         mPasswordInput.setText("");     // clean password to avoid social hacking (disadvantage: password in removed if the device is turned aside)\r
+        mJustCreated = true;\r
     }\r
 \r
 \r
@@ -219,6 +243,10 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             outState.putString(KEY_OC_VERSION, mDiscoveredVersion.toString());\r
         outState.putString(KEY_HOST_URL_TEXT, mHostBaseUrl);\r
         \r
+        /// account data, if updating\r
+        if (mAccount != null)\r
+            outState.putParcelable(KEY_ACCOUNT, mAccount);\r
+        \r
         // Saving the state of oAuth2 components.\r
         outState.putInt(KEY_OAUTH2_STATUS_ICON, mOAuth2StatusIcon);\r
         outState.putInt(KEY_OAUTH2_STATUS_TEXT, mOAuth2StatusText);\r
@@ -244,7 +272,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         mIsSslConn = savedInstanceState.getBoolean(KEY_IS_SSL_CONN);\r
         mStatusText = savedInstanceState.getInt(KEY_STATUS_TEXT);\r
         mStatusIcon = savedInstanceState.getInt(KEY_STATUS_ICON);\r
-        updateOcServerCheckIconAndText();\r
+        updateConnStatus();\r
         \r
         /// UI settings depending upon connection\r
         mOkButton.setEnabled(mStatusCorrect);   // TODO really necessary?\r
@@ -259,6 +287,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             mDiscoveredVersion = new OwnCloudVersion(ocVersion);\r
         mHostBaseUrl = savedInstanceState.getString(KEY_HOST_URL_TEXT);\r
         \r
+        // account data, if updating\r
+        mAccount = savedInstanceState.getParcelable(KEY_ACCOUNT);\r
+        \r
         // state of oAuth2 components\r
         mOAuth2StatusIcon = savedInstanceState.getInt(KEY_OAUTH2_STATUS_ICON);\r
         mOAuth2StatusText = savedInstanceState.getInt(KEY_OAUTH2_STATUS_TEXT);\r
@@ -302,8 +333,15 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     @Override\r
     protected void onResume() {\r
         super.onResume();\r
+        // the state of mOAuth2Check is automatically recovered between configuration changes, but not before onCreate() finishes; so keep the next lines here\r
         changeViewByOAuth2Check(mOAuth2Check.isChecked());  \r
-            // the state of mOAuth2Check is automatically recovered between configuration changes, but not before onCreate() finishes\r
+        if (mAction == ACTION_UPDATE_TOKEN && mJustCreated) {\r
+            if (mOAuth2Check.isChecked())\r
+                Toast.makeText(this, R.string.auth_expired_oauth_token_toast, Toast.LENGTH_LONG).show();\r
+            else\r
+                Toast.makeText(this, R.string.auth_expired_basic_auth_toast, Toast.LENGTH_LONG).show();\r
+        }\r
+           \r
         \r
         /* LEAVE OLD OAUTH FLOW ; \r
         // (old oauth code) Registering token receiver. We must listening to the service that is pooling to the oAuth server for a token.\r
@@ -316,6 +354,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         if (mNewCapturedUriFromOAuth2Redirection != null) {\r
             getOAuth2AccessTokenFromCapturedRedirection();            \r
         }\r
+        \r
+        mJustCreated = false;\r
     }\r
     \r
     \r
@@ -342,42 +382,14 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      */\r
     private void getOAuth2AccessTokenFromCapturedRedirection() {\r
         /// Parse data from OAuth redirection\r
-        Map<String, String> responseValues = new HashMap<String, String>();\r
         String queryParameters = mNewCapturedUriFromOAuth2Redirection.getQuery();\r
         mNewCapturedUriFromOAuth2Redirection = null;\r
-        String[] pairs = queryParameters.split("&");\r
-        int i = 0;\r
-        String key = "";\r
-        String value = "";\r
-        StringBuilder sb = new StringBuilder();\r
-        while (pairs.length > i) {\r
-            int j = 0;\r
-            String[] part = pairs[i].split("=");\r
-            while (part.length > j) {\r
-                String p = part[j];\r
-                if (j == 0) {\r
-                    key = p;\r
-                    sb.append(key + " = ");\r
-                } else if (j == 1) {\r
-                    value = p;\r
-                    responseValues.put(key, value);\r
-                    sb.append(value + "\n");\r
-                }\r
-\r
-                Log.v(TAG, "[" + i + "," + j + "] = " + p);\r
-                j++;\r
-            }\r
-            i++;\r
-        }\r
-        \r
-        /// Updating status widget to OK.\r
-        updateOAuth2IconAndText(R.drawable.ic_ok, R.string.auth_connection_established);\r
         \r
         /// Showing the dialog with instructions for the user.\r
         showDialog(DIALOG_OAUTH2_LOGIN_PROGRESS);\r
 \r
         /// GET ACCESS TOKEN to the oAuth server \r
-        RemoteOperation operation = new OAuth2GetAccessToken(responseValues);\r
+        RemoteOperation operation = new OAuth2GetAccessToken(queryParameters);\r
         WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(getString(R.string.oauth_url_endpoint_access)), getApplicationContext());\r
         operation.execute(client, this, mHandler);\r
     }\r
@@ -411,26 +423,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      */\r
     private void onUrlInputFocusChanged(TextView hostInput, boolean hasFocus) {\r
         if (!hasFocus) {\r
-            String uri = hostInput.getText().toString().trim();\r
-            if (uri.length() != 0) {\r
-                mStatusText = R.string.auth_testing_connection;\r
-                mStatusIcon = R.drawable.progress_small;\r
-                updateOcServerCheckIconAndText();\r
-                /** TODO cancel previous connection check if the user tries to ammend a wrong URL  \r
-                if(mConnChkOperation != null) {\r
-                    mConnChkOperation.cancel();\r
-                } */\r
-                mOcServerChkOperation = new  OwnCloudServerCheckOperation(uri, this);\r
-                WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(uri), this);\r
-                mHostBaseUrl = "";\r
-                mDiscoveredVersion = null;\r
-                mOperationThread = mOcServerChkOperation.execute(client, this, mHandler);\r
-            } else {\r
-                mRefreshButton.setVisibility(View.INVISIBLE);\r
-                mStatusText = 0;\r
-                mStatusIcon = 0;\r
-                updateOcServerCheckIconAndText();\r
-            }\r
+            checkOcServer();\r
+            \r
         } else {\r
             // avoids that the 'connect' button can be clicked if the test was previously passed\r
             mOkButton.setEnabled(false); \r
@@ -438,6 +432,30 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     }\r
 \r
 \r
+    private void checkOcServer() {\r
+        String uri = mHostUrlInput.getText().toString().trim();\r
+        if (uri.length() != 0) {\r
+            mStatusText = R.string.auth_testing_connection;\r
+            mStatusIcon = R.drawable.progress_small;\r
+            updateConnStatus();\r
+            /** TODO cancel previous connection check if the user tries to ammend a wrong URL  \r
+            if(mConnChkOperation != null) {\r
+                mConnChkOperation.cancel();\r
+            } */\r
+            mOcServerChkOperation = new  OwnCloudServerCheckOperation(uri, this);\r
+            WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(uri), this);\r
+            mHostBaseUrl = "";\r
+            mDiscoveredVersion = null;\r
+            mOperationThread = mOcServerChkOperation.execute(client, this, mHandler);\r
+        } else {\r
+            mRefreshButton.setVisibility(View.INVISIBLE);\r
+            mStatusText = 0;\r
+            mStatusIcon = 0;\r
+            updateConnStatus();\r
+        }\r
+    }\r
+\r
+\r
     /**\r
      * Handles changes in focus on the text input for the password (basic authorization).\r
      * \r
@@ -495,7 +513,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         if (mDiscoveredVersion == null || !mDiscoveredVersion.isVersionValid()  || mHostBaseUrl == null || mHostBaseUrl.length() == 0) {\r
             mStatusIcon = R.drawable.common_error;\r
             mStatusText = R.string.auth_wtf_reenter_URL;\r
-            updateOcServerCheckIconAndText();\r
+            updateConnStatus();\r
             mOkButton.setEnabled(false);\r
             Log.wtf(TAG,  "The user was allowed to click 'connect' to an unchecked server!!");\r
             return;\r
@@ -539,7 +557,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      */\r
     private void startOauthorization() {\r
         // be gentle with the user\r
-        updateOAuth2IconAndText(R.drawable.progress_small, R.string.oauth_login_connection);\r
+        mStatusIcon = R.drawable.progress_small;\r
+        mStatusText = R.string.oauth_login_connection;\r
+        updateAuthStatus();\r
         \r
         // GET AUTHORIZATION request\r
         /*\r
@@ -594,86 +614,19 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      * @param result        Result of the check.\r
      */\r
     private void onOcServerCheckFinish(OwnCloudServerCheckOperation operation, RemoteOperationResult result) {\r
-        /// update status connection icon and text\r
-        mStatusText = mStatusIcon = 0;\r
-        mStatusCorrect = false;\r
+        /// update status icon and text\r
+        updateStatusIconAndText(result);\r
+        updateConnStatus();\r
+\r
+        /// save result state\r
+        mStatusCorrect = result.isSuccess();\r
+        mIsSslConn = (result.getCode() == ResultCode.OK_SSL);\r
         \r
-        switch (result.getCode()) {\r
-            case OK_SSL:\r
-                mIsSslConn = true;\r
-                mStatusIcon = android.R.drawable.ic_secure;\r
-                mStatusText = R.string.auth_secure_connection;\r
-                mStatusCorrect = true;\r
-                break;\r
-                \r
-            case OK_NO_SSL:\r
-            case OK:\r
-                mIsSslConn = false;\r
-                mStatusCorrect = true;\r
-                if (mHostUrlInput.getText().toString().trim().toLowerCase().startsWith("http://") ) {\r
-                    mStatusText = R.string.auth_connection_established;\r
-                    mStatusIcon = R.drawable.ic_ok;\r
-                } else {\r
-                    mStatusText = R.string.auth_nossl_plain_ok_title;\r
-                    mStatusIcon = android.R.drawable.ic_partial_secure;\r
-                }\r
-                break;\r
-                \r
-            /// very special case (TODO: move to a common place for all the remote operations)\r
-            case SSL_RECOVERABLE_PEER_UNVERIFIED:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_ssl_unverified_server_title;\r
-                mLastSslUntrustedServerResult = result;\r
-                showDialog(DIALOG_SSL_VALIDATOR); \r
-                break;\r
-                    \r
-            case BAD_OC_VERSION:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_bad_oc_version_title;\r
-                break;\r
-            case WRONG_CONNECTION:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_wrong_connection_title;\r
-                break;\r
-            case TIMEOUT:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_timeout_title;\r
-                break;\r
-            case INCORRECT_ADDRESS:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_incorrect_address_title;\r
-                break;\r
-                \r
-            case SSL_ERROR:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_ssl_general_error_title;\r
-                break;\r
-                \r
-            case HOST_NOT_AVAILABLE:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_unknown_host_title;\r
-                break;\r
-            case NO_NETWORK_CONNECTION:\r
-                mStatusIcon = R.drawable.no_network;\r
-                mStatusText = R.string.auth_no_net_conn_title;\r
-                break;\r
-            case INSTANCE_NOT_CONFIGURED:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_not_configured_title;\r
-                break;\r
-            case FILE_NOT_FOUND:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_incorrect_path_title;\r
-                break;\r
-            case UNHANDLED_HTTP_CODE:\r
-            case UNKNOWN_ERROR:\r
-                mStatusIcon = R.drawable.common_error;\r
-                mStatusText = R.string.auth_unknown_error_title;\r
-                break;\r
-            default:\r
-                Log.e(TAG, "Incorrect connection checker result type: " + result.getHttpCode());\r
+        /// very special case (TODO: move to a common place for all the remote operations)\r
+        if (result.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) {\r
+            mLastSslUntrustedServerResult = result;\r
+            showDialog(DIALOG_SSL_VALIDATOR); \r
         }\r
-        updateOcServerCheckIconAndText();\r
         \r
         /// update the visibility of the 'retry connection' button\r
         if (!mStatusCorrect)\r
@@ -703,6 +656,98 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
 \r
     /**\r
+     * Chooses the right icon and text to show to the user for the received operation result.\r
+     * \r
+     * @param result    Result of a remote operation performed in this activity\r
+     */\r
+    private void updateStatusIconAndText(RemoteOperationResult result) {\r
+        mStatusText = mStatusIcon = 0;\r
+\r
+        switch (result.getCode()) {\r
+        case OK_SSL:\r
+            mStatusIcon = android.R.drawable.ic_secure;\r
+            mStatusText = R.string.auth_secure_connection;\r
+            break;\r
+            \r
+        case OK_NO_SSL:\r
+        case OK:\r
+            if (mHostUrlInput.getText().toString().trim().toLowerCase().startsWith("http://") ) {\r
+                mStatusText = R.string.auth_connection_established;\r
+                mStatusIcon = R.drawable.ic_ok;\r
+            } else {\r
+                mStatusText = R.string.auth_nossl_plain_ok_title;\r
+                mStatusIcon = android.R.drawable.ic_partial_secure;\r
+            }\r
+            break;\r
+            \r
+        case SSL_RECOVERABLE_PEER_UNVERIFIED:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_ssl_unverified_server_title;\r
+            break;\r
+                \r
+        case BAD_OC_VERSION:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_bad_oc_version_title;\r
+            break;\r
+        case WRONG_CONNECTION:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_wrong_connection_title;\r
+            break;\r
+        case TIMEOUT:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_timeout_title;\r
+            break;\r
+        case INCORRECT_ADDRESS:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_incorrect_address_title;\r
+            break;\r
+            \r
+        case SSL_ERROR:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_ssl_general_error_title;\r
+            break;\r
+            \r
+        case UNAUTHORIZED:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_unauthorized;\r
+            break;\r
+        case HOST_NOT_AVAILABLE:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_unknown_host_title;\r
+            break;\r
+        case NO_NETWORK_CONNECTION:\r
+            mStatusIcon = R.drawable.no_network;\r
+            mStatusText = R.string.auth_no_net_conn_title;\r
+            break;\r
+        case INSTANCE_NOT_CONFIGURED:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_not_configured_title;\r
+            break;\r
+        case FILE_NOT_FOUND:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_incorrect_path_title;\r
+            break;\r
+        case OAUTH2_ERROR:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_oauth_error;\r
+            break;\r
+        case OAUTH2_ERROR_ACCESS_DENIED:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_oauth_error_access_denied;\r
+            break;\r
+        case UNHANDLED_HTTP_CODE:\r
+        case UNKNOWN_ERROR:\r
+            mStatusIcon = R.drawable.common_error;\r
+            mStatusText = R.string.auth_unknown_error_title;\r
+            break;\r
+            \r
+        default:\r
+            break;\r
+        }\r
+    }\r
+\r
+\r
+    /**\r
      * Processes the result of the request for and access token send \r
      * to an OAuth authorization server.\r
      * \r
@@ -716,7 +761,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens\r
         }\r
 \r
-        String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, false);\r
+        String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, true);\r
         if (result.isSuccess() && webdav_path != null) {\r
             /// be gentle with the user\r
             showDialog(DIALOG_LOGIN_PROGRESS);\r
@@ -730,11 +775,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             mAuthCheckOperation.execute(client, this, mHandler);\r
             \r
         } else {\r
-            if (webdav_path != null) {\r
-                mOAuthAuthEndpointText.setError("A valid authorization could not be obtained");\r
-            } else {\r
-                mOAuthAuthEndpointText.setError(getString(R.string.auth_bad_oc_version_title)); // should never happen \r
-            }\r
+            updateStatusIconAndText(result);\r
+            updateAuthStatus();\r
+            Log.d(TAG, "Access failed: " + result.getLogMessage());\r
         }\r
     }\r
 \r
@@ -754,84 +797,113 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens\r
         }\r
         \r
-        boolean isOAuth = mOAuth2Check.isChecked();\r
-\r
         if (result.isSuccess()) {\r
             Log.d(TAG, "Successful access - time to save the account");\r
 \r
-            /// create and save new ownCloud account\r
-            Uri uri = Uri.parse(mHostBaseUrl);\r
-            String username = isOAuth ?\r
-                                "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong() :\r
-                                 mUsernameInput.getText().toString().trim();\r
-                             // TODO a better way to set an account name\r
-            String accountName = username + "@" + uri.getHost();\r
-            if (uri.getPort() >= 0) {\r
-                accountName += ":" + uri.getPort();\r
-            }\r
-            Account account = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE);\r
-            AccountManager accManager = AccountManager.get(this);\r
-            if (isOAuth) {\r
-                accManager.addAccountExplicitly(account, "", null);  // with our implementation, the password is never input in the app\r
+            if (mAction == ACTION_CREATE) {\r
+                createAccount();\r
+                \r
             } else {\r
-                accManager.addAccountExplicitly(account, mPasswordInput.getText().toString(), null);\r
-            }\r
-\r
-            /// add the new account as default in preferences, if there is none already\r
-            Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);\r
-            if (defaultAccount == null) {\r
-                SharedPreferences.Editor editor = PreferenceManager\r
-                        .getDefaultSharedPreferences(this).edit();\r
-                editor.putString("select_oc_account", accountName);\r
-                editor.commit();\r
-            }\r
-\r
-\r
-            /// prepare result to return to the Authenticator\r
-            //  TODO check again what the Authenticator makes with it; probably has the same effect as addAccountExplicitly, but it's not well done\r
-            final Intent intent = new Intent();    // TODO check if the intent can be retrieved from getIntent(), passed from AccountAuthenticator     \r
-            intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,    AccountAuthenticator.ACCOUNT_TYPE);\r
-            intent.putExtra(AccountManager.KEY_ACCOUNT_NAME,    account.name);\r
-            if (!isOAuth)\r
-                intent.putExtra(AccountManager.KEY_AUTHTOKEN,   AccountAuthenticator.ACCOUNT_TYPE); // TODO check this; not sure it's right; maybe\r
-            intent.putExtra(AccountManager.KEY_USERDATA,        username);\r
-            if (isOAuth) {\r
-                accManager.setAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, mOAuthAccessToken);\r
+                updateToken();\r
             }\r
-            /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA\r
-            accManager.setUserData(account, AccountAuthenticator.KEY_OC_VERSION,    mDiscoveredVersion.toString());\r
-            accManager.setUserData(account, AccountAuthenticator.KEY_OC_BASE_URL,   mHostBaseUrl);\r
-            if (isOAuth)\r
-                accManager.setUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2, "TRUE");  // TODO this flag should be unnecessary\r
-\r
-            setAccountAuthenticatorResult(intent.getExtras());\r
-            setResult(RESULT_OK, intent);\r
             \r
-            /// immediately request for the synchronization of the new account\r
-            Bundle bundle = new Bundle();\r
-            bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
-            ContentResolver.requestSync(account, AccountAuthenticator.AUTHORITY, bundle);\r
-\r
             finish();\r
-                \r
-        } else {\r
-            if (!isOAuth) {\r
-                mUsernameInput.setError(result.getLogMessage() + "        ");  \r
-                                                    // the extra spaces are a workaround for an ugly bug: \r
-                                                    // 1. insert wrong credentials and connect\r
-                                                    // 2. put the focus on the user name field with using hardware controls (don't touch the screen); the error is shown UNDER the field\r
-                                                    // 3. touch the user name field; the software keyboard appears; the error popup is moved OVER the field and SHRINKED in width, losing the last word\r
-                                                    // Seen, at least, in Android 2.x devices            \r
             \r
-            } else {\r
-                mOAuthAuthEndpointText.setError(result.getLogMessage() + "        ");\r
-            }\r
+        } else {\r
+            updateStatusIconAndText(result);\r
+            updateAuthStatus();\r
             Log.d(TAG, "Access failed: " + result.getLogMessage());\r
         }\r
     }\r
 \r
     \r
+    /**\r
+     * Sets the proper response to get that the Account Authenticator that started this activity saves \r
+     * a new authorization token for mAccount.\r
+     */\r
+    private void updateToken() {\r
+        Bundle response = new Bundle();\r
+        response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);\r
+        response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);\r
+        boolean isOAuth = mOAuth2Check.isChecked();\r
+        if (isOAuth) {\r
+            response.putString(AccountManager.KEY_AUTHTOKEN, mOAuthAccessToken);\r
+            // the next line is necessary; by now, notifications are calling directly to the AuthenticatorActivity to update, without AccountManager intervention\r
+            mAccountMgr.setAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, mOAuthAccessToken);\r
+        } else {\r
+            response.putString(AccountManager.KEY_AUTHTOKEN, mPasswordInput.getText().toString());\r
+            mAccountMgr.setPassword(mAccount, mPasswordInput.getText().toString());\r
+        }\r
+        setAccountAuthenticatorResult(response);\r
+    }\r
+\r
+\r
+    /**\r
+     * Creates a new account through the Account Authenticator that started this activity. \r
+     * \r
+     * This makes the account permanent.\r
+     * \r
+     * TODO Decide how to name the OAuth accounts\r
+     * TODO Minimize the direct interactions with the account manager; seems that not all the operations \r
+     * in the current code are really necessary, provided that right extras are returned to the Account\r
+     * Authenticator through setAccountAuthenticatorResult  \r
+     */\r
+    private void createAccount() {\r
+        /// create and save new ownCloud account\r
+        boolean isOAuth = mOAuth2Check.isChecked();\r
+        \r
+        Uri uri = Uri.parse(mHostBaseUrl);\r
+        String username = mUsernameInput.getText().toString().trim();\r
+        if (isOAuth) {\r
+            username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();    // TODO change this to something readable\r
+        }            \r
+        String accountName = username + "@" + uri.getHost();\r
+        if (uri.getPort() >= 0) {\r
+            accountName += ":" + uri.getPort();\r
+        }\r
+        mAccount = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE);\r
+        if (isOAuth) {\r
+            mAccountMgr.addAccountExplicitly(mAccount, "", null);  // with our implementation, the password is never input in the app\r
+        } else {\r
+            mAccountMgr.addAccountExplicitly(mAccount, mPasswordInput.getText().toString(), null);\r
+        }\r
+\r
+        /// add the new account as default in preferences, if there is none already\r
+        Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);\r
+        if (defaultAccount == null) {\r
+            SharedPreferences.Editor editor = PreferenceManager\r
+                    .getDefaultSharedPreferences(this).edit();\r
+            editor.putString("select_oc_account", accountName);\r
+            editor.commit();\r
+        }\r
+\r
+        /// prepare result to return to the Authenticator\r
+        //  TODO check again what the Authenticator makes with it; probably has the same effect as addAccountExplicitly, but it's not well done\r
+        final Intent intent = new Intent();       \r
+        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,    AccountAuthenticator.ACCOUNT_TYPE);\r
+        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME,    mAccount.name);\r
+        if (!isOAuth)\r
+            intent.putExtra(AccountManager.KEY_AUTHTOKEN,   AccountAuthenticator.ACCOUNT_TYPE); // TODO check this; not sure it's right; maybe\r
+        intent.putExtra(AccountManager.KEY_USERDATA,        username);\r
+        if (isOAuth) {\r
+            mAccountMgr.setAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, mOAuthAccessToken);\r
+        }\r
+        /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA\r
+        mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION,    mDiscoveredVersion.toString());\r
+        mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL,   mHostBaseUrl);\r
+        if (isOAuth)\r
+            mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_OAUTH2, "TRUE");  // TODO this flag should be unnecessary\r
     \r
+        setAccountAuthenticatorResult(intent.getExtras());\r
+        setResult(RESULT_OK, intent);\r
+        \r
+        /// immediately request for the synchronization of the new account\r
+        Bundle bundle = new Bundle();\r
+        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
+        ContentResolver.requestSync(mAccount, AccountAuthenticator.AUTHORITY, bundle);\r
+    }\r
+\r
+\r
     /**\r
      * {@inheritDoc}\r
      * \r
@@ -963,7 +1035,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      * Updates the content and visibility state of the icon and text associated\r
      * to the last check on the ownCloud server.\r
      */\r
-    private void updateOcServerCheckIconAndText() {\r
+    private void updateConnStatus() {\r
         ImageView iv = (ImageView) findViewById(R.id.action_indicator);\r
         TextView tv = (TextView) findViewById(R.id.status_text);\r
 \r
@@ -977,6 +1049,30 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             tv.setVisibility(View.VISIBLE);\r
         }\r
     }\r
+    \r
+    \r
+    /**\r
+     * Updates the content and visibility state of the icon and text associated\r
+     * to the interactions with the OAuth authorization server.\r
+     */\r
+    private void updateAuthStatus() {\r
+        /*ImageView iv = (ImageView) findViewById(R.id.auth_status_icon);\r
+        TextView tv = (TextView) findViewById(R.id.auth_status_text);*/\r
+\r
+        if (mStatusIcon == 0 && mStatusText == 0) {\r
+            mAuthStatusLayout.setVisibility(View.INVISIBLE);\r
+            /*iv.setVisibility(View.INVISIBLE);\r
+            tv.setVisibility(View.INVISIBLE);*/\r
+        } else {\r
+            mAuthStatusLayout.setText(mStatusText);\r
+            mAuthStatusLayout.setCompoundDrawablesWithIntrinsicBounds(mStatusIcon, 0, 0, 0);\r
+            /*iv.setImageResource(mStatusIcon);\r
+            tv.setText(mStatusText);\r
+            /*iv.setVisibility(View.VISIBLE);\r
+            tv.setVisibility(View.VISIBLE);^*/\r
+            mAuthStatusLayout.setVisibility(View.VISIBLE);\r
+        }\r
+    }     \r
 \r
     \r
     /**\r
@@ -1035,51 +1131,22 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      */\r
     public void changeViewByOAuth2Check(Boolean checked) {\r
         \r
-        ImageView auth2ActionIndicator = (ImageView) findViewById(R.id.auth2_action_indicator); \r
-        TextView oauth2StatusText = (TextView) findViewById(R.id.oauth2_status_text);         \r
-\r
         if (checked) {\r
             mOAuthAuthEndpointText.setVisibility(View.VISIBLE);\r
             mOAuthTokenEndpointText.setVisibility(View.VISIBLE);\r
             mUsernameInput.setVisibility(View.GONE);\r
             mPasswordInput.setVisibility(View.GONE);\r
             mViewPasswordButton.setVisibility(View.GONE);\r
-            auth2ActionIndicator.setVisibility(View.INVISIBLE);\r
-            oauth2StatusText.setVisibility(View.INVISIBLE);\r
         } else {\r
             mOAuthAuthEndpointText.setVisibility(View.GONE);\r
             mOAuthTokenEndpointText.setVisibility(View.GONE);\r
             mUsernameInput.setVisibility(View.VISIBLE);\r
             mPasswordInput.setVisibility(View.VISIBLE);\r
             mViewPasswordButton.setVisibility(View.INVISIBLE);\r
-            auth2ActionIndicator.setVisibility(View.GONE);\r
-            oauth2StatusText.setVisibility(View.GONE);\r
         }     \r
 \r
     }    \r
     \r
-    /**\r
-     * Updates the content and visibility state of the icon and text associated\r
-     * to the interactions with the OAuth authorization server.\r
-     * \r
-     * @param   drawable_id     Resource id for the icon.\r
-     * @param   text_id         Resource id for the text.\r
-     */\r
-    private void updateOAuth2IconAndText(int drawable_id, int text_id) {\r
-        ImageView iv = (ImageView) findViewById(R.id.auth2_action_indicator);\r
-        TextView tv = (TextView) findViewById(R.id.oauth2_status_text);\r
-\r
-        if (drawable_id == 0 && text_id == 0) {\r
-            iv.setVisibility(View.INVISIBLE);\r
-            tv.setVisibility(View.INVISIBLE);\r
-        } else {\r
-            iv.setImageResource(drawable_id);\r
-            tv.setText(text_id);\r
-            iv.setVisibility(View.VISIBLE);\r
-            tv.setVisibility(View.VISIBLE);\r
-        }\r
-    }     \r
-    \r
     /* Leave the old OAuth flow\r
     // Results from the first call to oAuth2 server : getting the user_code and verification_url.\r
     @Override\r