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
.R
;
39 import com
.owncloud
.android
.authentication
.SsoWebViewClient
;
40 import com
.owncloud
.android
.authentication
.SsoWebViewClient
.SsoWebViewClientListener
;
41 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
42 import com
.owncloud
.android
.utils
.Log_OC
;
46 * Dialog to show the WebView for SAML Authentication
48 * @author Maria Asensio
49 * @author David A. Velasco
51 public class SamlWebViewDialog
extends SherlockDialogFragment
{
53 public final String SAML_DIALOG_TAG
= "SamlWebViewDialog";
55 private final static String TAG
= SamlWebViewDialog
.class.getSimpleName();
57 private static final String ARG_INITIAL_URL
= "INITIAL_URL";
58 private static final String ARG_TARGET_URL
= "TARGET_URL";
59 private static final String KEY_WEBVIEW_STATE
= "WEBVIEW_STATE";
61 private WebView mSsoWebView
;
62 private SsoWebViewClient mWebViewClient
;
64 private String mInitialUrl
;
65 private String mTargetUrl
;
67 private Handler mHandler
;
69 private SsoWebViewClientListener mSsoWebViewClientListener
;
71 //private View mSsoRootView;
75 * Public factory method to get dialog instances.
78 * @param Url Url to open at WebView
79 * @param targetURL mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
80 * @return New dialog instance, ready to show.
82 public static SamlWebViewDialog
newInstance(String url
, String targetUrl
) {
83 Log_OC
.d(TAG
, "New instance");
84 SamlWebViewDialog fragment
= new SamlWebViewDialog();
85 Bundle args
= new Bundle();
86 args
.putString(ARG_INITIAL_URL
, url
);
87 args
.putString(ARG_TARGET_URL
, targetUrl
);
88 fragment
.setArguments(args
);
93 public SamlWebViewDialog() {
95 Log_OC
.d(TAG
, "constructor");
100 public void onAttach(Activity activity
) {
101 Log_OC
.d(TAG
, "onAttach");
102 super.onAttach(activity
);
104 mSsoWebViewClientListener
= (SsoWebViewClientListener
) activity
;
105 mHandler
= new Handler();
106 mWebViewClient
= new SsoWebViewClient(mHandler
, mSsoWebViewClientListener
);
108 } catch (ClassCastException e
) {
109 throw new ClassCastException(activity
.toString() + " must implement " + SsoWebViewClientListener
.class.getSimpleName());
114 @SuppressLint("SetJavaScriptEnabled")
116 public void onCreate(Bundle savedInstanceState
) {
117 Log_OC
.d(TAG
, "onCreate");
118 super.onCreate(savedInstanceState
);
120 CookieSyncManager
.createInstance(getActivity());
122 if (savedInstanceState
== null
) {
123 mInitialUrl
= getArguments().getString(ARG_INITIAL_URL
);
124 mTargetUrl
= getArguments().getString(ARG_TARGET_URL
);
126 mInitialUrl
= savedInstanceState
.getString(ARG_INITIAL_URL
);
127 mTargetUrl
= savedInstanceState
.getString(ARG_TARGET_URL
);
130 setStyle(SherlockDialogFragment
.STYLE_NO_TITLE
, R
.style
.Theme_ownCloud_Dialog
);
134 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
135 Log_OC
.d(TAG
, "onCreateDialog");
139 AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
140 if (mSsoRootView.getParent() != null) {
141 ((ViewGroup)(mSsoRootView.getParent())).removeView(mSsoRootView);
143 builder.setView(mSsoRootView);
144 //builder.setView(mSsoWebView);
145 Dialog dialog = builder.create();
148 return super.onCreateDialog(savedInstanceState
);
151 @SuppressLint("SetJavaScriptEnabled")
153 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
154 Log_OC
.d(TAG
, "onCreateView");
156 // Inflate layout of the dialog
157 View rootView
= inflater
.inflate(R
.layout
.sso_dialog
, container
, false
); // null parent view because it will go in the dialog layout
158 mSsoWebView
= (WebView
) rootView
.findViewById(R
.id
.sso_webview
);
160 mWebViewClient
.setTargetUrl(mTargetUrl
);
161 mSsoWebView
.setWebViewClient(mWebViewClient
);
163 if (savedInstanceState
== null
) {
164 Log_OC
.d(TAG
, " initWebView start");
165 CookieManager cookieManager
= CookieManager
.getInstance();
166 cookieManager
.setAcceptCookie(true
);
167 cookieManager
.removeAllCookie();
168 mSsoWebView
.loadUrl(mInitialUrl
);
171 Log_OC
.d(TAG
, " restoreWebView start");
172 WebBackForwardList history
= mSsoWebView
.restoreState(savedInstanceState
.getBundle(KEY_WEBVIEW_STATE
));
173 if (history
== null
) {
174 Log_OC
.e(TAG
, "Error restoring WebView state ; back to starting URL");
175 mSsoWebView
.loadUrl(mInitialUrl
);
179 WebSettings webSettings
= mSsoWebView
.getSettings();
180 webSettings
.setJavaScriptEnabled(true
);
181 webSettings
.setBuiltInZoomControls(true
);
182 webSettings
.setLoadWithOverviewMode(false
);
183 webSettings
.setSavePassword(false
);
184 webSettings
.setUserAgentString(WebdavClient
.USER_AGENT
);
185 webSettings
.setSaveFormData(false
);
191 public void onSaveInstanceState(Bundle outState
) {
192 Log_OC
.d(SAML_DIALOG_TAG
, "onSaveInstanceState being CALLED");
193 super.onSaveInstanceState(outState
);
196 outState
.putString(ARG_INITIAL_URL
, mInitialUrl
);
197 outState
.putString(ARG_TARGET_URL
, mTargetUrl
);
199 // Save the state of the WebView
200 Bundle webviewState
= new Bundle();
201 mSsoWebView
.saveState(webviewState
);
202 outState
.putBundle(KEY_WEBVIEW_STATE
, webviewState
);
206 public void onDestroyView() {
207 Log_OC
.d(TAG
, "onDestroyView");
209 mSsoWebView
.setWebViewClient(null
);
211 // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
212 Dialog dialog
= getDialog();
213 if ((dialog
!= null
)) {
214 dialog
.setOnDismissListener(null
);
216 //dialog.setDismissMessage(null);
219 super.onDestroyView();
223 public void onDestroy() {
224 Log_OC
.d(TAG
, "onDestroy");
229 public void onDetach() {
230 Log_OC
.d(TAG
, "onDetach");
231 mSsoWebViewClientListener
= null
;
232 mWebViewClient
= null
;
237 public void onCancel (DialogInterface dialog
) {
238 Log_OC
.d(SAML_DIALOG_TAG
, "onCancel");
239 super.onCancel(dialog
);
243 public void onDismiss (DialogInterface dialog
) {
244 Log_OC
.d(SAML_DIALOG_TAG
, "onDismiss");
245 super.onDismiss(dialog
);
249 public void onStart() {
250 Log_OC
.d(SAML_DIALOG_TAG
, "onStart");
255 public void onStop() {
256 Log_OC
.d(SAML_DIALOG_TAG
, "onStop");
261 public void onResume() {
262 Log_OC
.d(SAML_DIALOG_TAG
, "onResume");
267 public void onPause() {
268 Log_OC
.d(SAML_DIALOG_TAG
, "onPause");
273 public int show (FragmentTransaction transaction
, String tag
) {
274 Log_OC
.d(SAML_DIALOG_TAG
, "show (transaction)");
275 return super.show(transaction
, tag
);
279 public void show (FragmentManager manager
, String tag
) {
280 Log_OC
.d(SAML_DIALOG_TAG
, "show (manager)");
281 super.show(manager
, tag
);