1 package com
.owncloud
.android
.ui
.dialog
;
3 import android
.annotation
.SuppressLint
;
4 import android
.app
.Activity
;
5 import android
.app
.AlertDialog
;
6 import android
.app
.Dialog
;
7 import android
.os
.Bundle
;
8 import android
.os
.Handler
;
9 import android
.support
.v4
.app
.DialogFragment
;
10 import android
.webkit
.CookieManager
;
11 import android
.webkit
.WebSettings
;
12 import android
.webkit
.WebView
;
14 import com
.owncloud
.android
.Log_OC
;
15 import com
.owncloud
.android
.authentication
.SsoWebViewClient
;
16 import com
.owncloud
.android
.authentication
.SsoWebViewClient
.SsoWebViewClientListener
;
18 import eu
.alefzero
.webdav
.WebdavClient
;
21 * Dialog to show the WebView for SAML Authentication
23 * @author Maria Asensio
25 public class SamlWebViewDialog
extends DialogFragment
28 public final String SAML_DIALOG_TAG
= "SamlWebViewDialog";
30 private final static String TAG
= SamlWebViewDialog
.class.getSimpleName();
32 private WebView mSsoWebView
;
33 private SsoWebViewClient mWebViewClient
;
35 private static String mUrl
;
36 private static String mTargetUrl
;
38 private Handler mHandler
;
40 private SsoWebViewClientListener mSsoWebViewClientListener
;
44 * Public factory method to get dialog instances.
47 * @param Url Url to open at WebView
48 * @param targetURL mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
49 * @return New dialog instance, ready to show.
51 public static SamlWebViewDialog
newInstance(String url
, String targetUrl
) {
52 SamlWebViewDialog fragment
= new SamlWebViewDialog();
55 mTargetUrl
= targetUrl
;
61 public void onSaveInstanceState(Bundle outState
) {
62 super.onSaveInstanceState(outState
);
64 // Save the state of the WebView
65 mSsoWebView
.saveState(outState
);
68 @SuppressLint("SetJavaScriptEnabled")
70 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
71 Log_OC
.d(TAG
, "On Create Dialog");
73 mHandler
= new Handler();
75 mSsoWebView
= new WebView(getActivity()) {
77 public boolean onCheckIsTextEditor() {
83 mWebViewClient
= new SsoWebViewClient(mHandler
, mSsoWebViewClientListener
);
84 mSsoWebView
.setWebViewClient(mWebViewClient
);
85 mWebViewClient
.setTargetUrl(mTargetUrl
);
87 mSsoWebView
.setFocusable(true
);
88 mSsoWebView
.setFocusableInTouchMode(true
);
89 mSsoWebView
.setClickable(true
);
91 WebSettings webSettings
= mSsoWebView
.getSettings();
92 webSettings
.setJavaScriptEnabled(true
);
93 webSettings
.setBuiltInZoomControls(true
);
94 webSettings
.setLoadWithOverviewMode(false
);
95 webSettings
.setSavePassword(false
);
96 webSettings
.setUserAgentString(WebdavClient
.USER_AGENT
);
99 if (savedInstanceState
== null
) {
103 restoreWebView(savedInstanceState
);
107 AlertDialog
.Builder builder
= new AlertDialog
.Builder(getActivity());
108 Dialog dialog
= builder
.setView(mSsoWebView
).create();
113 @SuppressLint("SetJavaScriptEnabled")
114 private void initWebView() {
115 CookieManager cookieManager
= CookieManager
.getInstance();
116 cookieManager
.setAcceptCookie(true
);
117 cookieManager
.removeAllCookie();
119 mSsoWebView
.loadUrl(mUrl
);
122 @SuppressLint("SetJavaScriptEnabled")
123 private void restoreWebView(Bundle savedInstanceState
) {
124 mSsoWebView
.restoreState(savedInstanceState
);
126 CookieManager cookieManager
= CookieManager
.getInstance();
127 Log_OC
.e(TAG
, "Accept Cookie: " + cookieManager
.acceptCookie());
132 public void onDestroyView() {
133 Dialog dialog
= getDialog();
134 Log_OC
.d(TAG
, "On Destroy");
135 // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
136 if ((dialog
!= null
) && getRetainInstance())
137 getDialog().setOnDismissListener(null
);
139 super.onDestroyView();
144 public void onAttach(Activity activity
) {
145 super.onAttach(activity
);
146 Log_OC
.e(TAG
, "onAttach");
148 mSsoWebViewClientListener
= (SsoWebViewClientListener
) activity
;
149 } catch (ClassCastException e
) {
150 throw new ClassCastException(activity
.toString() + " must implement " + SsoWebViewClientListener
.class.getSimpleName());