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
.CookieSyncManager
;
33 import android
.webkit
.WebBackForwardList
;
34 import android
.webkit
.WebSettings
;
35 import android
.webkit
.WebView
;
37 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
38 import com
.owncloud
.android
.Log_OC
;
39 import com
.owncloud
.android
.R
;
40 import com
.owncloud
.android
.authentication
.SsoWebViewClient
;
41 import com
.owncloud
.android
.authentication
.SsoWebViewClient
.SsoWebViewClientListener
;
44 import eu
.alefzero
.webdav
.WebdavClient
;
47 * Dialog to show the WebView for SAML Authentication
49 * @author Maria Asensio
50 * @author David A. Velasco
52 public class SamlWebViewDialog
extends SherlockDialogFragment
{
54 public final String SAML_DIALOG_TAG
= "SamlWebViewDialog";
56 private final static String TAG
= SamlWebViewDialog
.class.getSimpleName();
58 private static final String ARG_INITIAL_URL
= "INITIAL_URL";
59 private static final String ARG_TARGET_URL
= "TARGET_URL";
60 private static final String KEY_WEBVIEW_STATE
= "WEBVIEW_STATE";
62 private WebView mSsoWebView
;
63 private SsoWebViewClient mWebViewClient
;
65 private String mInitialUrl
;
66 private String mTargetUrl
;
68 private Handler mHandler
;
70 private SsoWebViewClientListener mSsoWebViewClientListener
;
72 //private View mSsoRootView;
76 * Public factory method to get dialog instances.
79 * @param Url Url to open at WebView
80 * @param targetURL mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
81 * @return New dialog instance, ready to show.
83 public static SamlWebViewDialog
newInstance(String url
, String targetUrl
) {
84 Log_OC
.d(TAG
, "New instance");
85 SamlWebViewDialog fragment
= new SamlWebViewDialog();
86 Bundle args
= new Bundle();
87 args
.putString(ARG_INITIAL_URL
, url
);
88 args
.putString(ARG_TARGET_URL
, targetUrl
);
89 fragment
.setArguments(args
);
94 public SamlWebViewDialog() {
96 Log_OC
.d(TAG
, "constructor");
101 public void onAttach(Activity activity
) {
102 Log_OC
.d(TAG
, "onAttach");
103 super.onAttach(activity
);
105 mSsoWebViewClientListener
= (SsoWebViewClientListener
) activity
;
106 mHandler
= new Handler();
107 mWebViewClient
= new SsoWebViewClient(mHandler
, mSsoWebViewClientListener
);
109 } catch (ClassCastException e
) {
110 throw new ClassCastException(activity
.toString() + " must implement " + SsoWebViewClientListener
.class.getSimpleName());
115 @SuppressLint("SetJavaScriptEnabled")
117 public void onCreate(Bundle savedInstanceState
) {
118 Log_OC
.d(TAG
, "onCreate");
119 super.onCreate(savedInstanceState
);
121 CookieSyncManager
.createInstance(getActivity());
123 if (savedInstanceState
== null
) {
124 mInitialUrl
= getArguments().getString(ARG_INITIAL_URL
);
125 mTargetUrl
= getArguments().getString(ARG_TARGET_URL
);
127 mInitialUrl
= savedInstanceState
.getString(ARG_INITIAL_URL
);
128 mTargetUrl
= savedInstanceState
.getString(ARG_TARGET_URL
);
131 setStyle(SherlockDialogFragment
.STYLE_NO_TITLE
, R
.style
.Theme_ownCloud_Dialog
);
135 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
136 Log_OC
.d(TAG
, "onCreateDialog");
140 AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
141 if (mSsoRootView.getParent() != null) {
142 ((ViewGroup)(mSsoRootView.getParent())).removeView(mSsoRootView);
144 builder.setView(mSsoRootView);
145 //builder.setView(mSsoWebView);
146 Dialog dialog = builder.create();
149 return super.onCreateDialog(savedInstanceState
);
152 @SuppressLint("SetJavaScriptEnabled")
154 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
155 Log_OC
.d(TAG
, "onCreateView");
157 // Inflate layout of the dialog
158 View rootView
= inflater
.inflate(R
.layout
.sso_dialog
, container
, false
); // null parent view because it will go in the dialog layout
159 mSsoWebView
= (WebView
) rootView
.findViewById(R
.id
.sso_webview
);
161 mWebViewClient
.setTargetUrl(mTargetUrl
);
162 mSsoWebView
.setWebViewClient(mWebViewClient
);
164 if (savedInstanceState
== null
) {
165 Log_OC
.d(TAG
, " initWebView start");
166 CookieManager cookieManager
= CookieManager
.getInstance();
167 cookieManager
.setAcceptCookie(true
);
168 cookieManager
.removeAllCookie();
169 mSsoWebView
.loadUrl(mInitialUrl
);
172 Log_OC
.d(TAG
, " restoreWebView start");
173 WebBackForwardList history
= mSsoWebView
.restoreState(savedInstanceState
.getBundle(KEY_WEBVIEW_STATE
));
174 if (history
== null
) {
175 Log_OC
.e(TAG
, "Error restoring WebView state ; back to starting URL");
176 mSsoWebView
.loadUrl(mInitialUrl
);
180 WebSettings webSettings
= mSsoWebView
.getSettings();
181 webSettings
.setJavaScriptEnabled(true
);
182 webSettings
.setBuiltInZoomControls(true
);
183 webSettings
.setLoadWithOverviewMode(false
);
184 webSettings
.setSavePassword(false
);
185 webSettings
.setUserAgentString(WebdavClient
.USER_AGENT
);
186 webSettings
.setSaveFormData(false
);
192 public void onSaveInstanceState(Bundle outState
) {
193 Log_OC
.d(SAML_DIALOG_TAG
, "onSaveInstanceState being CALLED");
194 super.onSaveInstanceState(outState
);
197 outState
.putString(ARG_INITIAL_URL
, mInitialUrl
);
198 outState
.putString(ARG_TARGET_URL
, mTargetUrl
);
200 // Save the state of the WebView
201 Bundle webviewState
= new Bundle();
202 mSsoWebView
.saveState(webviewState
);
203 outState
.putBundle(KEY_WEBVIEW_STATE
, webviewState
);
207 public void onDestroyView() {
208 Log_OC
.d(TAG
, "onDestroyView");
210 mSsoWebView
.setWebViewClient(null
);
212 // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
213 Dialog dialog
= getDialog();
214 if ((dialog
!= null
)) {
215 dialog
.setOnDismissListener(null
);
217 //dialog.setDismissMessage(null);
220 super.onDestroyView();
224 public void onDestroy() {
225 Log_OC
.d(TAG
, "onDestroy");
230 public void onDetach() {
231 Log_OC
.d(TAG
, "onDetach");
232 mSsoWebViewClientListener
= null
;
233 mWebViewClient
= null
;
238 public void onCancel (DialogInterface dialog
) {
239 Log_OC
.d(SAML_DIALOG_TAG
, "onCancel");
240 super.onCancel(dialog
);
244 public void onDismiss (DialogInterface dialog
) {
245 Log_OC
.d(SAML_DIALOG_TAG
, "onDismiss");
246 super.onDismiss(dialog
);
250 public void onStart() {
251 Log_OC
.d(SAML_DIALOG_TAG
, "onStart");
256 public void onStop() {
257 Log_OC
.d(SAML_DIALOG_TAG
, "onStop");
262 public void onResume() {
263 Log_OC
.d(SAML_DIALOG_TAG
, "onResume");
268 public void onPause() {
269 Log_OC
.d(SAML_DIALOG_TAG
, "onPause");
274 public int show (FragmentTransaction transaction
, String tag
) {
275 Log_OC
.d(SAML_DIALOG_TAG
, "show (transaction)");
276 return super.show(transaction
, tag
);
280 public void show (FragmentManager manager
, String tag
) {
281 Log_OC
.d(SAML_DIALOG_TAG
, "show (manager)");
282 super.show(manager
, tag
);