message 'Session expires' in TextView instead of Toast
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorActivity.java
index 852f1d5..a768821 100644 (file)
@@ -33,7 +33,6 @@ import android.net.Uri;
 import android.os.Bundle;\r
 import android.os.Handler;\r
 import android.preference.PreferenceManager;\r
-import android.support.v4.app.FragmentManager;\r
 import android.text.Editable;\r
 import android.text.InputType;\r
 import android.text.TextWatcher;\r
@@ -53,6 +52,7 @@ import android.widget.Toast;
 \r
 import com.owncloud.android.Log_OC;\r
 import com.owncloud.android.R;\r
+import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
 import com.owncloud.android.operations.ExistenceCheckOperation;\r
 import com.owncloud.android.operations.OAuth2GetAccessToken;\r
@@ -75,7 +75,7 @@ import eu.alefzero.webdav.WebdavClient;
  * @author David A. Velasco\r
  */\r
 public class AuthenticatorActivity extends AccountAuthenticatorActivity\r
-implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeListener, OnEditorActionListener {\r
+implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeListener, OnEditorActionListener, SsoWebViewClientListener{\r
 \r
     private static final String TAG = AuthenticatorActivity.class.getSimpleName();\r
 \r
@@ -85,6 +85,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     public static final String EXTRA_ACTION = "ACTION";\r
     public static final String EXTRA_ENFORCED_UPDATE = "ENFORCE_UPDATE";\r
 \r
+    private static final String KEY_AUTH_MESSAGE_VISIBILITY = "AUTH_MESSAGE_VISIBILITY";\r
+    private static final String KEY_AUTH_MESSAGE_TEXT = "AUTH_MESSAGE_TEXT";\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
@@ -116,7 +118,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     private String mHostBaseUrl;\r
     private OwnCloudVersion mDiscoveredVersion;\r
 \r
-    private int mServerStatusText, mServerStatusIcon;\r
+    private String mAuthMessageText;\r
+    private int mAuthMessageVisibility, mServerStatusText, mServerStatusIcon;\r
     private boolean mServerIsChecked, mServerIsValid, mIsSslConn;\r
     private int mAuthStatusText, mAuthStatusIcon;    \r
     private TextView mAuthStatusLayout;\r
@@ -134,6 +137,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     private byte mAction;\r
     private Account mAccount;\r
 \r
+    private TextView mAuthMessage;\r
+    \r
     private EditText mHostUrlInput;\r
     private boolean mHostUrlInputEnabled;\r
     private View mRefreshButton;\r
@@ -168,6 +173,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
         /// set view and get references to view elements\r
         setContentView(R.layout.account_setup);\r
+        mAuthMessage = (TextView) findViewById(R.id.auth_message);\r
         mHostUrlInput = (EditText) findViewById(R.id.hostUrlInput);\r
         mHostUrlInput.setText(getString(R.string.server_url));  // valid although R.string.server_url is an empty string\r
         mUsernameInput = (EditText) findViewById(R.id.account_username);\r
@@ -209,6 +215,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
         if (savedInstanceState == null) {\r
             /// connection state and info\r
+            mAuthMessageVisibility = View.GONE;\r
             mServerStatusText = mServerStatusIcon = 0;\r
             mServerIsValid = false;\r
             mServerIsChecked = false;\r
@@ -224,9 +231,11 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
                 }\r
                 mHostBaseUrl = normalizeUrl(mAccountMgr.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL));\r
                 mHostUrlInput.setText(mHostBaseUrl);\r
+                String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));\r
+                mUsernameInput.setText(userName);\r
+                mAccountNameInput.setText(userName);\r
             }\r
             initAuthorizationMethod();  // checks intent and setup.xml to determine mCurrentAuthorizationMethod\r
-            mOAuth2Check.setChecked(mCurrentAuthTokenType == AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN);\r
             mJustCreated = true;\r
             \r
             if (mAction == ACTION_UPDATE_TOKEN || !mHostUrlInputEnabled) {\r
@@ -235,6 +244,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             \r
         } else {\r
             /// connection state and info\r
+            mAuthMessageVisibility = savedInstanceState.getInt(KEY_AUTH_MESSAGE_VISIBILITY);\r
+            mAuthMessageText = savedInstanceState.getString(KEY_AUTH_MESSAGE_TEXT);\r
             mServerIsValid = savedInstanceState.getBoolean(KEY_SERVER_VALID);\r
             mServerIsChecked = savedInstanceState.getBoolean(KEY_SERVER_CHECKED);\r
             mServerStatusText = savedInstanceState.getInt(KEY_SERVER_STATUS_TEXT);\r
@@ -258,6 +269,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             mCurrentAuthTokenType = savedInstanceState.getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE);\r
             if (mCurrentAuthTokenType == null) {\r
                 mCurrentAuthTokenType =  AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD;\r
+                \r
             }\r
 \r
             // check if server check was interrupted by a configuration change\r
@@ -271,6 +283,12 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
         }\r
 \r
+        if (mAuthMessageVisibility== View.VISIBLE) {\r
+            showAuthMessage(mAuthMessageText);\r
+        }\r
+        else {\r
+            hideAuthMessage();\r
+        }\r
         adaptViewAccordingToAuthenticationMethod();\r
         showServerStatus();\r
         showAuthStatus();\r
@@ -282,13 +300,15 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             mUsernameInput.setEnabled(false);\r
             mUsernameInput.setFocusable(false);\r
             mOAuth2Check.setVisibility(View.GONE);\r
+            mAccountNameInput.setEnabled(false);\r
+            mAccountNameInput.setFocusable(false);\r
         }\r
         \r
         //if (mServerIsChecked && !mServerIsValid && mRefreshButtonEnabled) showRefreshButton();\r
         if (mServerIsChecked && !mServerIsValid && refreshButtonEnabled) showRefreshButton();\r
         mOkButton.setEnabled(mServerIsValid); // state not automatically recovered in configuration changes\r
 \r
-        if (mCurrentAuthTokenType == AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE || \r
+        if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType) || \r
                 !AUTH_OPTIONAL.equals(getString(R.string.auth_method_oauth2))) {\r
             mOAuth2Check.setVisibility(View.GONE);\r
         }\r
@@ -326,17 +346,9 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             }\r
         });\r
         \r
-//        FragmentManager fm = getSupportFragmentManager();\r
-//\r
-//     // try to find searching by tag name\r
-//      mSamlDialog = (SamlWebViewDialog) fm.findFragmentByTag(TAG_SAML_DIALOG);\r
-//\r
-//     if (mSamlDialog != null) {\r
-//         mSamlDialog.show(fm, TAG_SAML_DIALOG);\r
-//         Log_OC.d(TAG_SAML_DIALOG,  "mSamlDialog not null");\r
-//     }\r
     }\r
     \r
+   \r
 \r
     private void initAuthorizationMethod() {\r
         boolean oAuthRequired = false;\r
@@ -371,6 +383,9 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));\r
             mUsernameInput.setText(userName);\r
         }\r
+        \r
+        mOAuth2Check.setChecked(AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(mCurrentAuthTokenType));\r
+        \r
     }\r
 \r
     /**\r
@@ -386,6 +401,8 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         super.onSaveInstanceState(outState);\r
 \r
         /// connection state and info\r
+        outState.putInt(KEY_AUTH_MESSAGE_VISIBILITY, mAuthMessage.getVisibility());\r
+        outState.putString(KEY_AUTH_MESSAGE_TEXT, mAuthMessage.getText().toString());\r
         outState.putInt(KEY_SERVER_STATUS_TEXT, mServerStatusText);\r
         outState.putInt(KEY_SERVER_STATUS_ICON, mServerStatusIcon);\r
         outState.putBoolean(KEY_SERVER_VALID, mServerIsValid);\r
@@ -440,10 +457,16 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
     protected void onResume() {\r
         super.onResume();\r
         if (mAction == ACTION_UPDATE_TOKEN && mJustCreated && getIntent().getBooleanExtra(EXTRA_ENFORCED_UPDATE, false)) {\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
+            if (AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(mCurrentAuthTokenType)) {\r
+                //Toast.makeText(this, R.string.auth_expired_oauth_token_toast, Toast.LENGTH_LONG).show();\r
+                showAuthMessage(getString(R.string.auth_expired_oauth_token_toast));\r
+            } else if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) {\r
+                //Toast.makeText(this, R.string.auth_expired_saml_sso_token_toast, Toast.LENGTH_LONG).show();\r
+                showAuthMessage(getString(R.string.auth_expired_saml_sso_token_toast));\r
+            } else {\r
+                //Toast.makeText(this, R.string.auth_expired_basic_auth_toast, Toast.LENGTH_LONG).show();\r
+                showAuthMessage(getString(R.string.auth_expired_basic_auth_toast));\r
+            }\r
         }\r
 \r
         if (mNewCapturedUriFromOAuth2Redirection != null) {\r
@@ -452,15 +475,6 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
 \r
         mJustCreated = false;\r
         \r
-\r
-        // try to find searching by tag name\r
-//        FragmentManager fm = getSupportFragmentManager();\r
-//         mSamlDialog = (SamlWebViewDialog) fm.findFragmentByTag(TAG_SAML_DIALOG);\r
-//\r
-//        if (mSamlDialog != null) {\r
-//         //   mSamlDialog.show(fm, TAG_SAML_DIALOG);\r
-//            mSamlDialog.setRetainInstance(true);\r
-//        }\r
     }\r
 \r
 \r
@@ -646,6 +660,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             return;\r
         }\r
 \r
+        hideAuthMessage();\r
         if (AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(mCurrentAuthTokenType)) {\r
             startOauthorization();\r
         } else if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) { \r
@@ -763,7 +778,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             String targetUrl = mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType);\r
             \r
             // Show dialog\r
-            mSamlDialog = SamlWebViewDialog.newInstance(mHandler, url, targetUrl);            \r
+            mSamlDialog = SamlWebViewDialog.newInstance(url, targetUrl);            \r
             mSamlDialog.show(getSupportFragmentManager(), TAG_SAML_DIALOG);\r
             \r
             mAuthStatusIcon = android.R.drawable.ic_secure;\r
@@ -1419,7 +1434,6 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
             mUsernameInput.setVisibility(View.GONE);\r
             mPasswordInput.setVisibility(View.GONE);\r
             mAccountNameInput.setVisibility(View.VISIBLE);\r
-            \r
         } else {\r
             // basic HTTP authorization\r
             mOAuthAuthEndpointText.setVisibility(View.GONE);\r
@@ -1515,4 +1529,35 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
       }\r
     }\r
 \r
+\r
+\r
+    @Override\r
+    public void onSsoFinished(String sessionCookie) {\r
+        //Toast.makeText(this, "got cookies: " + sessionCookie, Toast.LENGTH_LONG).show();\r
+\r
+        if (sessionCookie != null && sessionCookie.length() > 0) {\r
+            Log_OC.d(TAG, "Successful SSO - time to save the account");\r
+            onSamlDialogSuccess(sessionCookie);\r
+            finish();\r
+\r
+        } else { \r
+            // TODO - show fail\r
+            Log_OC.d(TAG, "SSO failed");\r
+        }\r
+    }\r
+    \r
+    \r
+    /** Show auth_message \r
+     * \r
+     * @param message\r
+     */\r
+    private void showAuthMessage(String message) {\r
+       mAuthMessage.setVisibility(View.VISIBLE);\r
+       mAuthMessage.setText(message);\r
+    }\r
+    \r
+    private void hideAuthMessage() {\r
+        mAuthMessage.setVisibility(View.GONE);\r
+    }\r
+\r
 }\r