1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 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 org
.apache
.commons
.httpclient
.methods
.PostMethod
;
22 import android
.annotation
.SuppressLint
;
23 import android
.app
.Activity
;
24 import android
.app
.Dialog
;
25 import android
.content
.DialogInterface
;
26 import android
.os
.Bundle
;
27 import android
.os
.Handler
;
28 import android
.support
.v4
.app
.FragmentTransaction
;
29 import android
.support
.v4
.app
.FragmentManager
;
30 import android
.view
.LayoutInflater
;
31 import android
.view
.View
;
32 import android
.view
.ViewGroup
;
33 import android
.webkit
.CookieManager
;
34 import android
.webkit
.CookieSyncManager
;
35 import android
.webkit
.WebSettings
;
36 import android
.webkit
.WebView
;
37 import android
.widget
.RelativeLayout
;
39 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
40 import com
.owncloud
.android
.R
;
41 import com
.owncloud
.android
.authentication
.SsoWebViewClient
;
42 import com
.owncloud
.android
.authentication
.SsoWebViewClient
.SsoWebViewClientListener
;
43 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
44 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
48 * Dialog to show the WebView for SAML Authentication
50 * @author Maria Asensio
51 * @author David A. Velasco
53 public class SamlWebViewDialog
extends SherlockDialogFragment
{
55 public final String SAML_DIALOG_TAG
= "SamlWebViewDialog";
57 private final static String TAG
= SamlWebViewDialog
.class.getSimpleName();
59 private static final String ARG_INITIAL_URL
= "INITIAL_URL";
60 private static final String ARG_TARGET_URL
= "TARGET_URL";
62 private WebView mSsoWebView
;
63 private SsoWebViewClient mWebViewClient
;
65 private String mInitialUrl
;
66 private String mTargetUrl
;
68 private Handler mHandler
;
70 private SsoWebViewClientListener mSsoWebViewClientListener
;
73 * Public factory method to get dialog instances.
76 * @param Url Url to open at WebView
77 * @param targetURL mBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
78 * @return New dialog instance, ready to show.
80 public static SamlWebViewDialog
newInstance(String url
, String targetUrl
) {
81 Log_OC
.d(TAG
, "New instance");
82 SamlWebViewDialog fragment
= new SamlWebViewDialog();
83 Bundle args
= new Bundle();
84 args
.putString(ARG_INITIAL_URL
, url
);
85 args
.putString(ARG_TARGET_URL
, targetUrl
);
86 fragment
.setArguments(args
);
91 public SamlWebViewDialog() {
93 Log_OC
.d(TAG
, "constructor");
98 public void onAttach(Activity activity
) {
99 Log_OC
.d(TAG
, "onAttach");
100 super.onAttach(activity
);
102 mSsoWebViewClientListener
= (SsoWebViewClientListener
) activity
;
103 mHandler
= new Handler();
104 mWebViewClient
= new SsoWebViewClient(activity
, mHandler
, mSsoWebViewClientListener
);
106 } catch (ClassCastException e
) {
107 throw new ClassCastException(activity
.toString() + " must implement " + SsoWebViewClientListener
.class.getSimpleName());
112 @SuppressLint("SetJavaScriptEnabled")
114 public void onCreate(Bundle savedInstanceState
) {
115 Log_OC
.d(TAG
, "onCreate, savedInstanceState is " + savedInstanceState
);
116 super.onCreate(savedInstanceState
);
118 setRetainInstance(true
);
120 CookieSyncManager
.createInstance(getSherlockActivity().getApplicationContext());
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
);
133 @SuppressWarnings("deprecation")
134 @SuppressLint("SetJavaScriptEnabled")
136 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
137 Log_OC
.d(TAG
, "onCreateView, savedInsanceState is " + savedInstanceState
);
139 // Inflate layout of the dialog
140 RelativeLayout ssoRootView
= (RelativeLayout
) inflater
.inflate(R
.layout
.sso_dialog
, container
, false
); // null parent view because it will go in the dialog layout
142 if (mSsoWebView
== null
) {
143 // initialize the WebView
144 mSsoWebView
= new SsoWebView(getSherlockActivity().getApplicationContext());
145 mSsoWebView
.setFocusable(true
);
146 mSsoWebView
.setFocusableInTouchMode(true
);
147 mSsoWebView
.setClickable(true
);
149 WebSettings webSettings
= mSsoWebView
.getSettings();
150 webSettings
.setJavaScriptEnabled(true
);
151 webSettings
.setBuiltInZoomControls(false
);
152 webSettings
.setLoadWithOverviewMode(false
);
153 webSettings
.setSavePassword(false
);
154 webSettings
.setUserAgentString(OwnCloudClient
.USER_AGENT
);
155 webSettings
.setSaveFormData(false
);
157 CookieManager cookieManager
= CookieManager
.getInstance();
158 cookieManager
.setAcceptCookie(true
);
159 cookieManager
.removeAllCookie();
161 mSsoWebView
.loadUrl(mInitialUrl
);
164 mWebViewClient
.setTargetUrl(mTargetUrl
);
165 mSsoWebView
.setWebViewClient(mWebViewClient
);
167 // add the webview into the layout
168 RelativeLayout
.LayoutParams layoutParams
= new RelativeLayout
.LayoutParams(
169 RelativeLayout
.LayoutParams
.WRAP_CONTENT
,
170 RelativeLayout
.LayoutParams
.WRAP_CONTENT
172 ssoRootView
.addView(mSsoWebView
, layoutParams
);
173 ssoRootView
.requestLayout();
179 public void onSaveInstanceState(Bundle outState
) {
180 Log_OC
.d(TAG
, "onSaveInstanceState being CALLED");
181 super.onSaveInstanceState(outState
);
184 outState
.putString(ARG_INITIAL_URL
, mInitialUrl
);
185 outState
.putString(ARG_TARGET_URL
, mTargetUrl
);
189 public void onDestroyView() {
190 Log_OC
.d(TAG
, "onDestroyView");
192 if ((ViewGroup
)mSsoWebView
.getParent() != null
) {
193 ((ViewGroup
)mSsoWebView
.getParent()).removeView(mSsoWebView
);
196 mSsoWebView
.setWebViewClient(null
);
198 // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
199 Dialog dialog
= getDialog();
200 if ((dialog
!= null
)) {
201 dialog
.setOnDismissListener(null
);
203 //dialog.setDismissMessage(null);
206 super.onDestroyView();
210 public void onDestroy() {
211 Log_OC
.d(TAG
, "onDestroy");
216 public void onDetach() {
217 Log_OC
.d(TAG
, "onDetach");
218 mSsoWebViewClientListener
= null
;
219 mWebViewClient
= null
;
224 public void onCancel (DialogInterface dialog
) {
225 Log_OC
.d(TAG
, "onCancel");
226 super.onCancel(dialog
);
230 public void onDismiss (DialogInterface dialog
) {
231 Log_OC
.d(TAG
, "onDismiss");
232 super.onDismiss(dialog
);
236 public void onStart() {
237 Log_OC
.d(TAG
, "onStart");
242 public void onStop() {
243 Log_OC
.d(TAG
, "onStop");
248 public void onResume() {
249 Log_OC
.d(TAG
, "onResume");
251 mSsoWebView
.onResume();
255 public void onPause() {
256 Log_OC
.d(TAG
, "onPause");
257 mSsoWebView
.onPause();
262 public int show (FragmentTransaction transaction
, String tag
) {
263 Log_OC
.d(TAG
, "show (transaction)");
264 return super.show(transaction
, tag
);
268 public void show (FragmentManager manager
, String tag
) {
269 Log_OC
.d(TAG
, "show (manager)");
270 super.show(manager
, tag
);