Merge remote-tracking branch 'origin/saml_based_federated_single_sign_on' into saml_b...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / SamlWebViewDialog.java
1 package com.owncloud.android.ui.dialog;
2
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;
13
14 import com.owncloud.android.Log_OC;
15 import com.owncloud.android.authentication.SsoWebViewClient;
16 import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;
17
18 import eu.alefzero.webdav.WebdavClient;
19
20 /**
21 * Dialog to show the WebView for SAML Authentication
22 *
23 * @author Maria Asensio
24 */
25 public class SamlWebViewDialog extends DialogFragment
26 {
27
28 public final String SAML_DIALOG_TAG = "SamlWebViewDialog";
29
30 private final static String TAG = SamlWebViewDialog.class.getSimpleName();
31
32 private WebView mSsoWebView;
33 private SsoWebViewClient mWebViewClient;
34
35 private static String mUrl;
36 private static String mTargetUrl;
37
38 private Handler mHandler;
39
40 private SsoWebViewClientListener mSsoWebViewClientListener;
41
42
43 /**
44 * Public factory method to get dialog instances.
45 *
46 * @param handler
47 * @param Url Url to open at WebView
48 * @param targetURL mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
49 * @return New dialog instance, ready to show.
50 */
51 public static SamlWebViewDialog newInstance(String url, String targetUrl) {
52 SamlWebViewDialog fragment = new SamlWebViewDialog();
53
54 mUrl = url;
55 mTargetUrl = targetUrl;
56 return fragment;
57 }
58
59
60 @Override
61 public void onSaveInstanceState(Bundle outState) {
62 super.onSaveInstanceState(outState);
63
64 // Save the state of the WebView
65 mSsoWebView.saveState(outState);
66 }
67
68 @SuppressLint("SetJavaScriptEnabled")
69 @Override
70 public Dialog onCreateDialog(Bundle savedInstanceState) {
71 Log_OC.d(TAG, "On Create Dialog");
72
73 mHandler = new Handler();
74
75 mSsoWebView = new WebView(getActivity()) {
76 @Override
77 public boolean onCheckIsTextEditor() {
78 return true;
79 }
80 };
81
82
83 mWebViewClient = new SsoWebViewClient(mHandler, mSsoWebViewClientListener);
84 mSsoWebView.setWebViewClient(mWebViewClient);
85 mWebViewClient.setTargetUrl(mTargetUrl);
86
87 mSsoWebView.setFocusable(true);
88 mSsoWebView.setFocusableInTouchMode(true);
89 mSsoWebView.setClickable(true);
90
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);
97
98 // load the dialog
99 if (savedInstanceState == null) {
100 initWebView();
101 }
102 else {
103 restoreWebView(savedInstanceState);
104 }
105
106 // build the dialog
107 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
108 Dialog dialog = builder.setView(mSsoWebView).create();
109
110 return dialog;
111 }
112
113 @SuppressLint("SetJavaScriptEnabled")
114 private void initWebView() {
115 CookieManager cookieManager = CookieManager.getInstance();
116 cookieManager.setAcceptCookie(true);
117 cookieManager.removeAllCookie();
118
119 mSsoWebView.loadUrl(mUrl);
120 }
121
122 @SuppressLint("SetJavaScriptEnabled")
123 private void restoreWebView(Bundle savedInstanceState) {
124 mSsoWebView.restoreState(savedInstanceState);
125
126 CookieManager cookieManager = CookieManager.getInstance();
127 Log_OC.e(TAG, "Accept Cookie: " + cookieManager.acceptCookie());
128 }
129
130
131 @Override
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);
138
139 super.onDestroyView();
140 }
141
142
143 @Override
144 public void onAttach(Activity activity) {
145 super.onAttach(activity);
146 Log_OC.e(TAG, "onAttach");
147 try {
148 mSsoWebViewClientListener = (SsoWebViewClientListener) activity;
149 } catch (ClassCastException e) {
150 throw new ClassCastException(activity.toString() + " must implement " + SsoWebViewClientListener.class.getSimpleName());
151 }
152 }
153 }