Redirect app to login screen when operations in file details view fail due to bad...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / AuthenticatorActivity.java
index 6704bde..fb5c711 100644 (file)
@@ -72,6 +72,7 @@ import android.view.View.OnFocusChangeListener;
 import android.view.Window;\r
 import android.widget.CheckBox;\r
 import android.widget.EditText;\r
+import android.widget.Button;\r
 import android.widget.ImageView;\r
 import android.widget.TextView;\r
 import com.owncloud.android.R;\r
@@ -111,6 +112,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     private boolean mStatusCorrect, mIsSslConn;\r
     private RemoteOperationResult mLastSslUntrustedServerResult;\r
 \r
+    public static final String PARAM_ACCOUNTNAME = "param_Accountname";\r
+    \r
     public static final String PARAM_USERNAME = "param_Username";\r
     public static final String PARAM_HOSTNAME = "param_Hostname";\r
 \r
@@ -195,15 +198,31 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             mStatusText = mStatusIcon = 0;\r
             mStatusCorrect = false;\r
             mIsSslConn = false;\r
+            \r
+            String accountName = getIntent().getExtras().getString(PARAM_ACCOUNTNAME);\r
+            String tokenType = getIntent().getExtras().getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE);\r
+            if (AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(tokenType)) {\r
+                CheckBox oAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check);\r
+                oAuth2Check.setChecked(true);\r
+                changeViewByOAuth2Check(true);\r
+            } \r
+            \r
+            if (accountName != null) {\r
+                ((TextView) findViewById(R.id.account_username)).setText(accountName.substring(0, accountName.lastIndexOf('@')));\r
+                tv.setText(accountName.substring(accountName.lastIndexOf('@') + 1));\r
+            }\r
         }\r
         iv.setOnClickListener(this);\r
         iv2.setOnClickListener(this);\r
         tv.setOnFocusChangeListener(this);\r
         tv2.setOnFocusChangeListener(this);\r
         \r
+        Button b = (Button) findViewById(R.id.account_register);\r
+        if (b != null) {\r
+            b.setText(String.format(getString(R.string.auth_register), getString(R.string.app_name)));\r
+        }\r
+\r
         mNewCapturedUriFromOAuth2Redirection = null;\r
-        \r
-        Log.d(TAG, "onCreate");\r
     }\r
 \r
     \r
@@ -443,7 +462,11 @@ 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
             TextView tv = (TextView) findViewById(R.id.account_username);\r
-            tv.setError(message);\r
+            tv.setError(message + "        ");  // 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
     }\r
     public void onCancelClick(View view) {\r
@@ -484,7 +507,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
     }\r
 \r
     public void onRegisterClick(View view) {\r
-        Intent register = new Intent(Intent.ACTION_VIEW, Uri.parse("https://owncloud.com/mobile/new"));\r
+        Intent register = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.url_account_register)));\r
         setResult(RESULT_CANCELED);\r
         startActivity(register);\r
     }\r
@@ -501,7 +524,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
         URL uri = null;\r
         mDiscoveredVersion = mConnChkRunnable.getDiscoveredVersion();\r
-        String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion);\r
+        String webdav_path = AccountUtils.getWebdavPath(mDiscoveredVersion, false);\r
         \r
         if (webdav_path == null) {\r
             onAuthenticationResult(false, getString(R.string.auth_bad_oc_version_title));\r
@@ -605,7 +628,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);\r
     }\r
 \r
-    @Override\r
     public void onFocusChange(View view, boolean hasFocus) {\r
         if (view.getId() == R.id.host_URL) {\r
             if (!hasFocus) {\r
@@ -666,8 +688,14 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             onFocusChange(findViewById(R.id.host_URL), false);\r
         } else if (v.getId() == R.id.viewPassword) {\r
             TextView view = (TextView) findViewById(R.id.account_password);\r
-            int input_type = InputType.TYPE_CLASS_TEXT\r
-                    | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;\r
+            int input_type = view.getInputType();\r
+            if ((input_type & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {\r
+                input_type = InputType.TYPE_CLASS_TEXT\r
+                        | InputType.TYPE_TEXT_VARIATION_PASSWORD;\r
+            } else {\r
+                input_type = InputType.TYPE_CLASS_TEXT\r
+                        | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;\r
+            }\r
             view.setInputType(input_type);\r
         }\r
     }\r
@@ -1014,7 +1042,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
                        showDialog(DIALOG_LOGIN_PROGRESS);\r
                 String accessToken = ((GetOAuth2AccessToken)operation).getResultTokenMap().get(OAuth2Context.KEY_ACCESS_TOKEN);\r
-                Log.d(TAG, "ACCESS TOKEN: " + accessToken);\r
+                Log.d(TAG, "Got ACCESS TOKEN: " + accessToken);\r
                        mAuthChkOperation = new ExistenceCheckOperation("", this, accessToken);\r
                        WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(uri, getApplicationContext());\r
                        mAuthChkOperation.execute(client, this, mHandler);\r
@@ -1036,9 +1064,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                    \r
                    if (result.isSuccess()) {\r
                 TextView tv = (TextView) findViewById(R.id.oAuth_URL);\r
-                tv.setError("OOOOOKKKKKK");\r
-                       Log.d(TAG, "OOOOK!!!!");\r
-                       /**\r
+                       Log.d(TAG, "Checked access - time to save the account");\r
+                       \r
                        Uri uri = Uri.parse(mBaseUrl);\r
                        String username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong(); \r
                        String accountName = username + "@" + uri.getHost();\r
@@ -1048,8 +1075,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                        // TODO - check that accountName does not exist\r
                        Account account = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE);\r
                        AccountManager accManager = AccountManager.get(this);\r
-                       /// TODO SAVE THE ACCESS TOKEN, HERE OR IN SOME BETTER PLACE\r
-                       //accManager.addAccountExplicitly(account, mAccesToken, null);  //// IS THIS REALLY NEEDED? IS NOT REDUNDANT WITH SETACCOUNTAUTHENTICATORRESULT?\r
+                       accManager.addAccountExplicitly(account, "", null);  // with our implementation, the password is never input in the app\r
 \r
                        // Add this account as default in the preferences, if there is none\r
                        Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);\r
@@ -1063,12 +1089,13 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                        final Intent intent = new Intent();\r
                        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE);\r
                        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
-                       intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE);\r
                        intent.putExtra(AccountManager.KEY_USERDATA, username);\r
-                       intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAccessToken)\r
 \r
+                accManager.setAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, ((ExistenceCheckOperation) operation).getAccessToken());\r
+                \r
                        accManager.setUserData(account, AccountAuthenticator.KEY_OC_VERSION, mConnChkRunnable.getDiscoveredVersion().toString());\r
                        accManager.setUserData(account, AccountAuthenticator.KEY_OC_BASE_URL, mBaseUrl);\r
+                       accManager.setUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2, "TRUE");\r
 \r
                        setAccountAuthenticatorResult(intent.getExtras());\r
                        setResult(RESULT_OK, intent);\r
@@ -1079,12 +1106,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                        ContentResolver.requestSync(account, "org.owncloud", bundle);\r
 \r
                        finish();\r
-                       */\r
                        \r
                    } else {      \r
                        TextView tv = (TextView) findViewById(R.id.oAuth_URL);\r
                        tv.setError(result.getLogMessage());\r
-                Log.d(TAG, "NOOOOO " + result.getLogMessage());\r
+                Log.d(TAG, "Access failed: " + result.getLogMessage());\r
                    }\r
                }\r
        }\r