Fixed behaviour when authorization is denied by user; added toast message to explain...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / AuthenticatorActivity.java
index 6500ce5..c39f540 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
@@ -61,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
@@ -119,6 +118,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private Uri mNewCapturedUriFromOAuth2Redirection;\r
     \r
     private AccountManager mAccountMgr;\r
+    private boolean mJustCreated;\r
     \r
     private ImageView mRefreshButton;\r
     private ImageView mViewPasswordButton;\r
@@ -209,6 +209,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         }\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
@@ -318,8 +319,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 (getIntent().getByteExtra(EXTRA_ACTION, ACTION_CREATE) == 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
@@ -332,6 +340,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         if (mNewCapturedUriFromOAuth2Redirection != null) {\r
             getOAuth2AccessTokenFromCapturedRedirection();            \r
         }\r
+        \r
+        mJustCreated = false;\r
     }\r
     \r
     \r
@@ -358,46 +368,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. -- TODO REMOVE, UNNECESSARY\r
-        /*\r
-        mStatusIcon = R.drawable.ic_ok;\r
-        mStatusText = R.string.auth_connection_established;\r
-        updateAuthStatus();\r
-        */\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
@@ -737,7 +715,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             break;\r
         case OAUTH2_ERROR:\r
             mStatusIcon = R.drawable.common_error;\r
-            mStatusText = R.string.auth_bad_oauth_token;\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
@@ -779,11 +761,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