Move Shibboleth WebView to a big Dialog
authormasensio <masensio@solidgear.es>
Tue, 13 Aug 2013 11:28:45 +0000 (13:28 +0200)
committermasensio <masensio@solidgear.es>
Tue, 13 Aug 2013 11:28:45 +0000 (13:28 +0200)
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/ui/dialog/SamlWebViewDialog.java

index 852f1d5..d844011 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
@@ -226,7 +226,6 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
                 mHostUrlInput.setText(mHostBaseUrl);\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
@@ -258,6 +257,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
@@ -288,7 +288,7 @@ implements  OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList
         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 +326,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 +363,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
@@ -452,15 +447,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
@@ -763,7 +749,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 +1405,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 +1500,23 @@ 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
+\r
 }\r
index 2d0ac94..17a1e13 100644 (file)
@@ -1,6 +1,7 @@
 package com.owncloud.android.ui.dialog;
 
 import android.annotation.SuppressLint;
+import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.os.Bundle;
@@ -11,7 +12,6 @@ import android.webkit.WebSettings;
 import android.webkit.WebView;
 
 import com.owncloud.android.Log_OC;
-import com.owncloud.android.authentication.AuthenticatorActivity;
 import com.owncloud.android.authentication.SsoWebViewClient;
 import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;
 
@@ -23,7 +23,7 @@ import eu.alefzero.webdav.WebdavClient;
  * @author Maria Asensio
  */
 public class SamlWebViewDialog extends DialogFragment
-                               implements SsoWebViewClientListener{
+                              {
 
     public final String SAML_DIALOG_TAG = "SamlWebViewDialog";
     
@@ -35,7 +35,9 @@ public class SamlWebViewDialog extends DialogFragment
     private static String mUrl;
     private static String mTargetUrl;
     
-    private static Handler mHandler;
+    private Handler mHandler;
+
+    private SsoWebViewClientListener mSsoWebViewClientListener;
     
 
     /**
@@ -46,9 +48,9 @@ public class SamlWebViewDialog extends DialogFragment
      * @param targetURL     mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
      * @return              New dialog instance, ready to show.
      */
-    public static SamlWebViewDialog newInstance(Handler handler,String url, String targetUrl) {
+    public static SamlWebViewDialog newInstance(String url, String targetUrl) {
         SamlWebViewDialog fragment = new SamlWebViewDialog();
-        mHandler = handler;
+        
         mUrl = url;
         mTargetUrl = targetUrl;
         return fragment;
@@ -63,63 +65,69 @@ public class SamlWebViewDialog extends DialogFragment
         mSsoWebView.saveState(outState);
     }
 
-
-    @Override
+    @SuppressLint("SetJavaScriptEnabled")
+       @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         Log_OC.d(TAG, "On Create Dialog");
 
-        /// load the dialog
-        initWebView(savedInstanceState);
-        setRetainInstance(true);
-        /// build the dialog
-        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
+        mHandler = new Handler();
+        
+        mSsoWebView = new WebView(getActivity()) {
+            @Override
+            public boolean onCheckIsTextEditor() {
+                return true; 
+            }            
+        };
+
+        
+        mWebViewClient = new SsoWebViewClient(mHandler, mSsoWebViewClientListener);
+        mSsoWebView.setWebViewClient(mWebViewClient);
+        mWebViewClient.setTargetUrl(mTargetUrl);
+        
+        mSsoWebView.setFocusable(true);
+        mSsoWebView.setFocusableInTouchMode(true);
+        mSsoWebView.setClickable(true);
+        
+        WebSettings webSettings = mSsoWebView.getSettings();
+        webSettings.setJavaScriptEnabled(true);
+        webSettings.setBuiltInZoomControls(true);
+        webSettings.setLoadWithOverviewMode(false);
+        webSettings.setSavePassword(false);
+        webSettings.setUserAgentString(WebdavClient.USER_AGENT);
+        
+        // load the dialog
+        if (savedInstanceState == null) {            
+            initWebView();
+        }
+        else  {
+            restoreWebView(savedInstanceState);
+        }
         
+        // build the dialog
+        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
         Dialog dialog = builder.setView(mSsoWebView).create();
         
         return dialog;
     }
 
-    
     @SuppressLint("SetJavaScriptEnabled")
-    private void initWebView(Bundle savedInstanceState) {
+    private void initWebView() {
         CookieManager cookieManager = CookieManager.getInstance();
         cookieManager.setAcceptCookie(true);
-        //cookieManager.removeSessionCookie();        
+        cookieManager.removeAllCookie();
 
-        mWebViewClient = new SsoWebViewClient(mHandler, this);
-        mWebViewClient.setTargetUrl(mTargetUrl);
-        if (savedInstanceState == null) {
-            
-            Log_OC.d(TAG, "Saved Instance State NULL");
-            mSsoWebView = new WebView(getActivity()) {
-                @Override
-                public boolean onCheckIsTextEditor() {
-                    return true; 
-                }            
-            };
-            
-            mSsoWebView.setWebViewClient(mWebViewClient);
-            mSsoWebView.setFocusable(true);
-            mSsoWebView.setFocusableInTouchMode(true);
-            mSsoWebView.setClickable(true);
-            
-            WebSettings webSettings = mSsoWebView.getSettings();
-            webSettings.setJavaScriptEnabled(true);
-            webSettings.setBuiltInZoomControls(true);
-            webSettings.setLoadWithOverviewMode(false);
-            webSettings.setSavePassword(false);
-            webSettings.setUserAgentString(WebdavClient.USER_AGENT);
-            
-            mSsoWebView.loadUrl(mUrl);
-        }
-        else {
-            Log_OC.d(TAG, "Saved Instance State NOT NULL");
-            
-            mSsoWebView.restoreState(savedInstanceState);
-        }
-        
+        mSsoWebView.loadUrl(mUrl);
     }
 
+    @SuppressLint("SetJavaScriptEnabled")
+    private void restoreWebView(Bundle savedInstanceState) {
+        mSsoWebView.restoreState(savedInstanceState);
+        
+        CookieManager cookieManager = CookieManager.getInstance();
+        Log_OC.e(TAG, "Accept Cookie: " + cookieManager.acceptCookie());
+    }
+    
+    
     @Override
     public void onDestroyView() {
         Dialog dialog = getDialog();
@@ -133,18 +141,13 @@ public class SamlWebViewDialog extends DialogFragment
 
 
     @Override
-    public void onSsoFinished(String sessionCookie) {
-        //Toast.makeText(this, "got cookies: " + sessionCookie, Toast.LENGTH_LONG).show();
-
-        if (sessionCookie != null && sessionCookie.length() > 0) {
-            Log_OC.d(TAG, "Successful SSO - time to save the account");
-            ((AuthenticatorActivity) getActivity()).onSamlDialogSuccess(sessionCookie);
-            dismiss();
-
-        } else { 
-            // TODO - show fail
-            Log_OC.d(TAG, "SSO failed");
+    public void onAttach(Activity activity) {
+        super.onAttach(activity);
+        Log_OC.e(TAG, "onAttach");
+        try {
+            mSsoWebViewClientListener = (SsoWebViewClientListener) activity;
+        } catch (ClassCastException e) {
+            throw new ClassCastException(activity.toString() + " must implement " + SsoWebViewClientListener.class.getSimpleName());
         }
     }
-    
 }
\ No newline at end of file