2 * ownCloud Android client application
4 * @author Maria Asensio
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.ui
.dialog
;
24 import android
.annotation
.SuppressLint
;
25 import android
.app
.Activity
;
26 import android
.app
.Dialog
;
27 import android
.content
.DialogInterface
;
28 import android
.os
.Bundle
;
29 import android
.os
.Handler
;
30 import android
.support
.v4
.app
.FragmentTransaction
;
31 import android
.support
.v4
.app
.FragmentManager
;
32 import android
.view
.LayoutInflater
;
33 import android
.view
.View
;
34 import android
.view
.ViewGroup
;
35 import android
.webkit
.CookieManager
;
36 import android
.webkit
.CookieSyncManager
;
37 import android
.webkit
.WebSettings
;
38 import android
.webkit
.WebView
;
39 import android
.widget
.RelativeLayout
;
41 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
42 import com
.owncloud
.android
.MainApp
;
43 import com
.owncloud
.android
.R
;
44 import com
.owncloud
.android
.authentication
.SsoWebViewClient
;
45 import com
.owncloud
.android
.authentication
.SsoWebViewClient
.SsoWebViewClientListener
;
46 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
50 * Dialog to show the WebView for SAML Authentication
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";
61 private WebView mSsoWebView
;
62 private SsoWebViewClient mWebViewClient
;
64 private String mInitialUrl
;
65 private String mTargetUrl
;
67 private Handler mHandler
;
69 private SsoWebViewClientListener mSsoWebViewClientListener
;
72 * Public factory method to get dialog instances.
74 * @param url Url to open at WebView
75 * @param targetUrl mBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
76 * @return New dialog instance, ready to show.
78 public static SamlWebViewDialog
newInstance(String url
, String targetUrl
) {
79 SamlWebViewDialog fragment
= new SamlWebViewDialog();
80 Bundle args
= new Bundle();
81 args
.putString(ARG_INITIAL_URL
, url
);
82 args
.putString(ARG_TARGET_URL
, targetUrl
);
83 fragment
.setArguments(args
);
88 public SamlWebViewDialog() {
94 public void onAttach(Activity activity
) {
95 Log_OC
.v(TAG
, "onAttach");
96 super.onAttach(activity
);
98 mSsoWebViewClientListener
= (SsoWebViewClientListener
) activity
;
99 mHandler
= new Handler();
100 mWebViewClient
= new SsoWebViewClient(activity
, mHandler
, mSsoWebViewClientListener
);
102 } catch (ClassCastException e
) {
103 throw new ClassCastException(activity
.toString() + " must implement " +
104 SsoWebViewClientListener
.class.getSimpleName());
109 @SuppressLint("SetJavaScriptEnabled")
111 public void onCreate(Bundle savedInstanceState
) {
112 Log_OC
.v(TAG
, "onCreate, savedInstanceState is " + savedInstanceState
);
113 super.onCreate(savedInstanceState
);
115 setRetainInstance(true
);
117 CookieSyncManager
.createInstance(getSherlockActivity().getApplicationContext());
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
);
130 @SuppressWarnings("deprecation")
131 @SuppressLint("SetJavaScriptEnabled")
133 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
134 Bundle savedInstanceState
) {
135 Log_OC
.v(TAG
, "onCreateView, savedInsanceState is " + savedInstanceState
);
137 // Inflate layout of the dialog
138 RelativeLayout ssoRootView
= (RelativeLayout
) inflater
.inflate(R
.layout
.sso_dialog
,
139 container
, false
); // null parent view because it will go in the dialog layout
141 if (mSsoWebView
== null
) {
142 // initialize the WebView
143 mSsoWebView
= new SsoWebView(getSherlockActivity().getApplicationContext());
144 mSsoWebView
.setFocusable(true
);
145 mSsoWebView
.setFocusableInTouchMode(true
);
146 mSsoWebView
.setClickable(true
);
148 WebSettings webSettings
= mSsoWebView
.getSettings();
149 webSettings
.setJavaScriptEnabled(true
);
150 webSettings
.setBuiltInZoomControls(false
);
151 webSettings
.setLoadWithOverviewMode(false
);
152 webSettings
.setSavePassword(false
);
153 webSettings
.setUserAgentString(MainApp
.getUserAgent());
154 webSettings
.setSaveFormData(false
);
156 CookieManager cookieManager
= CookieManager
.getInstance();
157 cookieManager
.setAcceptCookie(true
);
158 cookieManager
.removeAllCookie();
160 mSsoWebView
.loadUrl(mInitialUrl
);
163 mWebViewClient
.setTargetUrl(mTargetUrl
);
164 mSsoWebView
.setWebViewClient(mWebViewClient
);
166 // add the webview into the layout
167 RelativeLayout
.LayoutParams layoutParams
= new RelativeLayout
.LayoutParams(
168 RelativeLayout
.LayoutParams
.WRAP_CONTENT
,
169 RelativeLayout
.LayoutParams
.WRAP_CONTENT
171 ssoRootView
.addView(mSsoWebView
, layoutParams
);
172 ssoRootView
.requestLayout();
178 public void onSaveInstanceState(Bundle outState
) {
179 Log_OC
.v(TAG
, "onSaveInstanceState being CALLED");
180 super.onSaveInstanceState(outState
);
183 outState
.putString(ARG_INITIAL_URL
, mInitialUrl
);
184 outState
.putString(ARG_TARGET_URL
, mTargetUrl
);
188 public void onDestroyView() {
189 Log_OC
.v(TAG
, "onDestroyView");
191 if ((ViewGroup
)mSsoWebView
.getParent() != null
) {
192 ((ViewGroup
)mSsoWebView
.getParent()).removeView(mSsoWebView
);
195 mSsoWebView
.setWebViewClient(null
);
197 // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
198 Dialog dialog
= getDialog();
199 if ((dialog
!= null
)) {
200 dialog
.setOnDismissListener(null
);
203 super.onDestroyView();
207 public void onDestroy() {
208 Log_OC
.v(TAG
, "onDestroy");
213 public void onDetach() {
214 Log_OC
.v(TAG
, "onDetach");
215 mSsoWebViewClientListener
= null
;
216 mWebViewClient
= null
;
221 public void onCancel (DialogInterface dialog
) {
222 Log_OC
.d(TAG
, "onCancel");
223 super.onCancel(dialog
);
227 public void onDismiss (DialogInterface dialog
) {
228 Log_OC
.d(TAG
, "onDismiss");
229 super.onDismiss(dialog
);
233 public void onStart() {
234 Log_OC
.v(TAG
, "onStart");
239 public void onStop() {
240 Log_OC
.v(TAG
, "onStop");
245 public void onResume() {
246 Log_OC
.v(TAG
, "onResume");
248 mSsoWebView
.onResume();
252 public void onPause() {
253 Log_OC
.v(TAG
, "onPause");
254 mSsoWebView
.onPause();
259 public int show (FragmentTransaction transaction
, String tag
) {
260 Log_OC
.v(TAG
, "show (transaction)");
261 return super.show(transaction
, tag
);
265 public void show (FragmentManager manager
, String tag
) {
266 Log_OC
.v(TAG
, "show (manager)");
267 super.show(manager
, tag
);