1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.dialog
;
20 import android
.annotation
.SuppressLint
;
21 import android
.app
.Activity
;
22 import android
.app
.Dialog
;
23 import android
.content
.DialogInterface
;
24 import android
.os
.Bundle
;
25 import android
.os
.Handler
;
26 import android
.support
.v4
.app
.FragmentTransaction
;
27 import android
.support
.v4
.app
.FragmentManager
;
28 import android
.view
.LayoutInflater
;
29 import android
.view
.View
;
30 import android
.view
.ViewGroup
;
31 import android
.webkit
.CookieManager
;
32 import android
.webkit
.WebBackForwardList
;
33 import android
.webkit
.WebSettings
;
34 import android
.webkit
.WebView
;
36 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
37 import com
.owncloud
.android
.Log_OC
;
38 import com
.owncloud
.android
.R
;
39 import com
.owncloud
.android
.authentication
.SsoWebViewClient
;
40 import com
.owncloud
.android
.authentication
.SsoWebViewClient
.SsoWebViewClientListener
;
42 import eu
.alefzero
.webdav
.WebdavClient
;
45 * Dialog to show the WebView for SAML Authentication
47 * @author Maria Asensio
48 * @author David A. Velasco
50 public class SamlWebViewDialog
extends SherlockDialogFragment
{
52 public final String SAML_DIALOG_TAG
= "SamlWebViewDialog";
54 private final static String TAG
= SamlWebViewDialog
.class.getSimpleName();
56 private static final String ARG_INITIAL_URL
= "INITIAL_URL";
57 private static final String ARG_TARGET_URL
= "TARGET_URL";
58 private static final String KEY_WEBVIEW_STATE
= "WEBVIEW_STATE";
60 private WebView mSsoWebView
;
61 private SsoWebViewClient mWebViewClient
;
63 private String mInitialUrl
;
64 private String mTargetUrl
;
66 private Handler mHandler
;
68 private SsoWebViewClientListener mSsoWebViewClientListener
;
70 //private View mSsoRootView;
74 * Public factory method to get dialog instances.
77 * @param Url Url to open at WebView
78 * @param targetURL mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
79 * @return New dialog instance, ready to show.
81 public static SamlWebViewDialog
newInstance(String url
, String targetUrl
) {
82 Log_OC
.d(TAG
, "New instance");
83 SamlWebViewDialog fragment
= new SamlWebViewDialog();
84 Bundle args
= new Bundle();
85 args
.putString(ARG_INITIAL_URL
, url
);
86 args
.putString(ARG_TARGET_URL
, targetUrl
);
87 fragment
.setArguments(args
);
92 public SamlWebViewDialog() {
94 Log_OC
.d(TAG
, "constructor");
99 public void onAttach(Activity activity
) {
100 Log_OC
.d(TAG
, "onAttach");
101 super.onAttach(activity
);
103 mSsoWebViewClientListener
= (SsoWebViewClientListener
) activity
;
104 mHandler
= new Handler();
105 mWebViewClient
= new SsoWebViewClient(mHandler
, mSsoWebViewClientListener
);
107 } catch (ClassCastException e
) {
108 throw new ClassCastException(activity
.toString() + " must implement " + SsoWebViewClientListener
.class.getSimpleName());
113 @SuppressLint("SetJavaScriptEnabled")
115 public void onCreate(Bundle savedInstanceState
) {
116 Log_OC
.d(TAG
, "onCreate");
117 super.onCreate(savedInstanceState
);
119 if (savedInstanceState
== null
) {
120 mInitialUrl
= getArguments().getString(ARG_INITIAL_URL
);
121 mTargetUrl
= getArguments().getString(ARG_TARGET_URL
);
123 mInitialUrl
= savedInstanceState
.getString(ARG_INITIAL_URL
);
124 mTargetUrl
= savedInstanceState
.getString(ARG_TARGET_URL
);
127 setStyle(SherlockDialogFragment
.STYLE_NO_TITLE
, R
.style
.Theme_ownCloud_Dialog
);
131 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
132 Log_OC
.d(TAG
, "onCreateDialog");
136 AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
137 if (mSsoRootView.getParent() != null) {
138 ((ViewGroup)(mSsoRootView.getParent())).removeView(mSsoRootView);
140 builder.setView(mSsoRootView);
141 //builder.setView(mSsoWebView);
142 Dialog dialog = builder.create();
145 return super.onCreateDialog(savedInstanceState
);
148 @SuppressLint("SetJavaScriptEnabled")
150 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
151 Log_OC
.d(TAG
, "onCreateView");
153 // Inflate layout of the dialog
154 View rootView
= inflater
.inflate(R
.layout
.sso_dialog
, container
, false
); // null parent view because it will go in the dialog layout
155 mSsoWebView
= (WebView
) rootView
.findViewById(R
.id
.sso_webview
);
157 mWebViewClient
.setTargetUrl(mTargetUrl
);
158 mSsoWebView
.setWebViewClient(mWebViewClient
);
160 if (savedInstanceState
== null
) {
161 Log_OC
.d(TAG
, " initWebView start");
162 CookieManager cookieManager
= CookieManager
.getInstance();
163 cookieManager
.setAcceptCookie(true
);
164 cookieManager
.removeAllCookie();
165 mSsoWebView
.loadUrl(mInitialUrl
);
168 Log_OC
.d(TAG
, " restoreWebView start");
169 WebBackForwardList history
= mSsoWebView
.restoreState(savedInstanceState
.getBundle(KEY_WEBVIEW_STATE
));
170 if (history
== null
) {
171 Log_OC
.e(TAG
, "Error restoring WebView state ; back to starting URL");
172 mSsoWebView
.loadUrl(mInitialUrl
);
176 WebSettings webSettings
= mSsoWebView
.getSettings();
177 webSettings
.setJavaScriptEnabled(true
);
178 webSettings
.setBuiltInZoomControls(true
);
179 webSettings
.setLoadWithOverviewMode(false
);
180 webSettings
.setSavePassword(false
);
181 webSettings
.setUserAgentString(WebdavClient
.USER_AGENT
);
182 webSettings
.setSaveFormData(false
);
188 public void onSaveInstanceState(Bundle outState
) {
189 Log_OC
.d(SAML_DIALOG_TAG
, "onSaveInstanceState being CALLED");
190 super.onSaveInstanceState(outState
);
193 outState
.putString(ARG_INITIAL_URL
, mInitialUrl
);
194 outState
.putString(ARG_TARGET_URL
, mTargetUrl
);
196 // Save the state of the WebView
197 Bundle webviewState
= new Bundle();
198 mSsoWebView
.saveState(webviewState
);
199 outState
.putBundle(KEY_WEBVIEW_STATE
, webviewState
);
203 public void onDestroyView() {
204 Log_OC
.d(TAG
, "onDestroyView");
206 mSsoWebView
.setWebViewClient(null
);
208 // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
209 Dialog dialog
= getDialog();
210 if ((dialog
!= null
)) {
211 dialog
.setOnDismissListener(null
);
213 //dialog.setDismissMessage(null);
216 super.onDestroyView();
220 public void onDestroy() {
221 Log_OC
.d(TAG
, "onDestroy");
226 public void onDetach() {
227 Log_OC
.d(TAG
, "onDetach");
228 mSsoWebViewClientListener
= null
;
229 mWebViewClient
= null
;
234 public void onCancel (DialogInterface dialog
) {
235 Log_OC
.d(SAML_DIALOG_TAG
, "onCancel");
236 super.onCancel(dialog
);
240 public void onDismiss (DialogInterface dialog
) {
241 Log_OC
.d(SAML_DIALOG_TAG
, "onDismiss");
242 super.onDismiss(dialog
);
246 public void onStart() {
247 Log_OC
.d(SAML_DIALOG_TAG
, "onStart");
252 public void onStop() {
253 Log_OC
.d(SAML_DIALOG_TAG
, "onStop");
258 public void onResume() {
259 Log_OC
.d(SAML_DIALOG_TAG
, "onResume");
264 public void onPause() {
265 Log_OC
.d(SAML_DIALOG_TAG
, "onPause");
270 public int show (FragmentTransaction transaction
, String tag
) {
271 Log_OC
.d(SAML_DIALOG_TAG
, "show (transaction)");
272 return super.show(transaction
, tag
);
276 public void show (FragmentManager manager
, String tag
) {
277 Log_OC
.d(SAML_DIALOG_TAG
, "show (manager)");
278 super.show(manager
, tag
);