Merge branch 'develop' into refactor_remote_operation_to_create_folder
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / SamlWebViewDialog.java
index 2d0ac94..91c607e 100644 (file)
@@ -1,42 +1,75 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012-2013 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 package com.owncloud.android.ui.dialog;
 
 import android.annotation.SuppressLint;
 package com.owncloud.android.ui.dialog;
 
 import android.annotation.SuppressLint;
-import android.app.AlertDialog;
+import android.app.Activity;
 import android.app.Dialog;
 import android.app.Dialog;
+import android.content.DialogInterface;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Bundle;
 import android.os.Handler;
-import android.support.v4.app.DialogFragment;
+import android.support.v4.app.FragmentTransaction;
+import android.support.v4.app.FragmentManager;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
 import android.webkit.CookieManager;
 import android.webkit.CookieManager;
+import android.webkit.CookieSyncManager;
+import android.webkit.WebBackForwardList;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 
-import com.owncloud.android.Log_OC;
-import com.owncloud.android.authentication.AuthenticatorActivity;
+import com.actionbarsherlock.app.SherlockDialogFragment;
+import com.owncloud.android.R;
 import com.owncloud.android.authentication.SsoWebViewClient;
 import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;
 import com.owncloud.android.authentication.SsoWebViewClient;
 import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;
+import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
+import com.owncloud.android.utils.Log_OC;
 
 
-import eu.alefzero.webdav.WebdavClient;
 
 /**
  * Dialog to show the WebView for SAML Authentication
  * 
  * @author Maria Asensio
 
 /**
  * Dialog to show the WebView for SAML Authentication
  * 
  * @author Maria Asensio
+ * @author David A. Velasco
  */
  */
-public class SamlWebViewDialog extends DialogFragment
-                               implements SsoWebViewClientListener{
+public class SamlWebViewDialog extends SherlockDialogFragment {
 
     public final String SAML_DIALOG_TAG = "SamlWebViewDialog";
     
     private final static String TAG =  SamlWebViewDialog.class.getSimpleName();
 
     public final String SAML_DIALOG_TAG = "SamlWebViewDialog";
     
     private final static String TAG =  SamlWebViewDialog.class.getSimpleName();
+
+    private static final String ARG_INITIAL_URL = "INITIAL_URL";
+    private static final String ARG_TARGET_URL = "TARGET_URL";
+    private static final String KEY_WEBVIEW_STATE = "WEBVIEW_STATE";
     
     private WebView mSsoWebView;
     private SsoWebViewClient mWebViewClient;
     
     
     private WebView mSsoWebView;
     private SsoWebViewClient mWebViewClient;
     
-    private static String mUrl;
-    private static String mTargetUrl;
-    
-    private static Handler mHandler;
+    private String mInitialUrl;
+    private String mTargetUrl;
     
     
+    private Handler mHandler;
+
+    private SsoWebViewClientListener mSsoWebViewClientListener;
+
+    //private View mSsoRootView;
+
 
     /**
      * Public factory method to get dialog instances.
 
     /**
      * Public factory method to get dialog instances.
@@ -46,105 +79,206 @@ public class SamlWebViewDialog extends DialogFragment
      * @param targetURL     mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
      * @return              New dialog instance, ready to show.
      */
      * @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) {
+        Log_OC.d(TAG, "New instance");
         SamlWebViewDialog fragment = new SamlWebViewDialog();
         SamlWebViewDialog fragment = new SamlWebViewDialog();
-        mHandler = handler;
-        mUrl = url;
-        mTargetUrl = targetUrl;
+        Bundle args = new Bundle();
+        args.putString(ARG_INITIAL_URL, url);
+        args.putString(ARG_TARGET_URL, targetUrl);
+        fragment.setArguments(args);
         return fragment;
     }
     
     
         return fragment;
     }
     
     
+    public SamlWebViewDialog() {
+        super();
+        Log_OC.d(TAG, "constructor");
+    }
+    
+    
     @Override
     @Override
-    public void onSaveInstanceState(Bundle outState) {
-        super.onSaveInstanceState(outState);
-        
-        // Save the state of the WebView
-        mSsoWebView.saveState(outState);
+    public void onAttach(Activity activity) {
+        Log_OC.d(TAG, "onAttach");
+        super.onAttach(activity);
+        try {
+            mSsoWebViewClientListener = (SsoWebViewClientListener) activity;
+            mHandler = new Handler();
+            mWebViewClient = new SsoWebViewClient(mHandler, mSsoWebViewClientListener);
+            
+       } catch (ClassCastException e) {
+            throw new ClassCastException(activity.toString() + " must implement " + SsoWebViewClientListener.class.getSimpleName());
+        }
     }
 
     }
 
+    
+    @SuppressLint("SetJavaScriptEnabled")
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        Log_OC.d(TAG, "onCreate");
+        super.onCreate(savedInstanceState);
+        
+        CookieSyncManager.createInstance(getActivity());
 
 
+        if (savedInstanceState == null) {
+            mInitialUrl = getArguments().getString(ARG_INITIAL_URL);
+            mTargetUrl = getArguments().getString(ARG_TARGET_URL);
+        } else {
+            mInitialUrl = savedInstanceState.getString(ARG_INITIAL_URL);
+            mTargetUrl = savedInstanceState.getString(ARG_TARGET_URL);
+        }
+        
+        setStyle(SherlockDialogFragment.STYLE_NO_TITLE, R.style.Theme_ownCloud_Dialog);
+    }
+    
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
-        Log_OC.d(TAG, "On Create Dialog");
+        Log_OC.d(TAG, "onCreateDialog");
 
 
-        /// load the dialog
-        initWebView(savedInstanceState);
-        setRetainInstance(true);
-        /// build the dialog
-        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
-        
-        Dialog dialog = builder.setView(mSsoWebView).create();
+        /*
+        // build the dialog
+        AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
+        if (mSsoRootView.getParent() != null) {
+            ((ViewGroup)(mSsoRootView.getParent())).removeView(mSsoRootView);
+        }
+        builder.setView(mSsoRootView);
+        //builder.setView(mSsoWebView);
+        Dialog dialog = builder.create();
+        */
         
         
-        return dialog;
+        return super.onCreateDialog(savedInstanceState);
     }
 
     }
 
-    
     @SuppressLint("SetJavaScriptEnabled")
     @SuppressLint("SetJavaScriptEnabled")
-    private void initWebView(Bundle savedInstanceState) {
-        CookieManager cookieManager = CookieManager.getInstance();
-        cookieManager.setAcceptCookie(true);
-        //cookieManager.removeSessionCookie();        
-
-        mWebViewClient = new SsoWebViewClient(mHandler, this);
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+        Log_OC.d(TAG, "onCreateView");
+        
+        // Inflate layout of the dialog  
+        View rootView = inflater.inflate(R.layout.sso_dialog, container, false);  // null parent view because it will go in the dialog layout
+        mSsoWebView  = (WebView) rootView.findViewById(R.id.sso_webview);
+            
         mWebViewClient.setTargetUrl(mTargetUrl);
         mWebViewClient.setTargetUrl(mTargetUrl);
+        mSsoWebView.setWebViewClient(mWebViewClient);
+        
         if (savedInstanceState == null) {
         if (savedInstanceState == null) {
+            Log_OC.d(TAG,  "   initWebView start");
+            CookieManager cookieManager = CookieManager.getInstance();
+            cookieManager.setAcceptCookie(true);
+            cookieManager.removeAllCookie();
+            mSsoWebView.loadUrl(mInitialUrl);
             
             
-            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);
+        } else {
+            Log_OC.d(TAG, "   restoreWebView start");
+            WebBackForwardList history = mSsoWebView.restoreState(savedInstanceState.getBundle(KEY_WEBVIEW_STATE));
+            if (history == null) {
+                Log_OC.e(TAG, "Error restoring WebView state ; back to starting URL");
+                mSsoWebView.loadUrl(mInitialUrl);
+            }
         }
         }
+
+        WebSettings webSettings = mSsoWebView.getSettings();
+        webSettings.setJavaScriptEnabled(true);
+        webSettings.setBuiltInZoomControls(true);
+        webSettings.setLoadWithOverviewMode(false);
+        webSettings.setSavePassword(false);
+        webSettings.setUserAgentString(WebdavClient.USER_AGENT);
+        webSettings.setSaveFormData(false);
         
         
+        return rootView;
+    }
+
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        Log_OC.d(SAML_DIALOG_TAG, "onSaveInstanceState being CALLED");
+        super.onSaveInstanceState(outState);
+        
+        // save URLs
+        outState.putString(ARG_INITIAL_URL, mInitialUrl);
+        outState.putString(ARG_TARGET_URL, mTargetUrl);
+        
+        // Save the state of the WebView
+        Bundle webviewState = new Bundle();
+        mSsoWebView.saveState(webviewState);
+        outState.putBundle(KEY_WEBVIEW_STATE, webviewState);
     }
 
     @Override
     public void onDestroyView() {
     }
 
     @Override
     public void onDestroyView() {
-        Dialog dialog = getDialog();
-        Log_OC.d(TAG, "On Destroy");
+        Log_OC.d(TAG, "onDestroyView");
+        
+        mSsoWebView.setWebViewClient(null);
+        
         // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
         // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
-        if ((dialog != null) && getRetainInstance())
-            getDialog().setOnDismissListener(null);
-
+        Dialog dialog = getDialog();
+        if ((dialog != null)) {
+            dialog.setOnDismissListener(null);
+            //dialog.dismiss();
+            //dialog.setDismissMessage(null);
+        }
+        
         super.onDestroyView();
     }
         super.onDestroyView();
     }
+    
+    @Override
+    public void onDestroy() {
+        Log_OC.d(TAG, "onDestroy");
+        super.onDestroy();
+    }
 
 
+    @Override
+    public void onDetach() {
+        Log_OC.d(TAG, "onDetach");
+        mSsoWebViewClientListener = null;
+        mWebViewClient = null;
+        super.onDetach();
+    }
+    
+    @Override
+    public void onCancel (DialogInterface dialog) {
+        Log_OC.d(SAML_DIALOG_TAG, "onCancel");
+        super.onCancel(dialog);
+    }
+    
+    @Override
+    public void onDismiss (DialogInterface dialog) {
+        Log_OC.d(SAML_DIALOG_TAG, "onDismiss");
+        super.onDismiss(dialog);
+    }
+    
+    @Override
+    public void onStart() {
+        Log_OC.d(SAML_DIALOG_TAG, "onStart");
+        super.onStart();
+    }
 
     @Override
 
     @Override
-    public void onSsoFinished(String sessionCookie) {
-        //Toast.makeText(this, "got cookies: " + sessionCookie, Toast.LENGTH_LONG).show();
+    public void onStop() {
+        Log_OC.d(SAML_DIALOG_TAG, "onStop");
+        super.onStop();
+    }
 
 
-        if (sessionCookie != null && sessionCookie.length() > 0) {
-            Log_OC.d(TAG, "Successful SSO - time to save the account");
-            ((AuthenticatorActivity) getActivity()).onSamlDialogSuccess(sessionCookie);
-            dismiss();
+    @Override
+    public void onResume() {
+        Log_OC.d(SAML_DIALOG_TAG, "onResume");
+        super.onResume();
+    }
 
 
-        } else { 
-            // TODO - show fail
-            Log_OC.d(TAG, "SSO failed");
-        }
+    @Override
+    public void onPause() {
+        Log_OC.d(SAML_DIALOG_TAG, "onPause");
+        super.onPause();
     }
     
     }
     
+    @Override
+    public int show (FragmentTransaction transaction, String tag) {
+        Log_OC.d(SAML_DIALOG_TAG, "show (transaction)");
+        return super.show(transaction, tag);
+    }
+
+    @Override
+    public void show (FragmentManager manager, String tag) {
+        Log_OC.d(SAML_DIALOG_TAG, "show (manager)");
+        super.show(manager, tag);
+    }
+
 }
\ No newline at end of file
 }
\ No newline at end of file