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
.DialogFragment
;
31 import android
.support
.v4
.app
.FragmentTransaction
;
32 import android
.support
.v4
.app
.FragmentManager
;
33 import android
.view
.LayoutInflater
;
34 import android
.view
.View
;
35 import android
.view
.ViewGroup
;
36 import android
.webkit
.CookieManager
;
37 import android
.webkit
.CookieSyncManager
;
38 import android
.webkit
.WebSettings
;
39 import android
.webkit
.WebView
;
40 import android
.widget
.RelativeLayout
;
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 DialogFragment
{
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, m
76 * CurrentAuthTokenType)
77 * @return New dialog instance, ready to show.
79 public static SamlWebViewDialog
newInstance(String url
, String targetUrl
) {
80 SamlWebViewDialog fragment
= new SamlWebViewDialog();
81 Bundle args
= new Bundle();
82 args
.putString(ARG_INITIAL_URL
, url
);
83 args
.putString(ARG_TARGET_URL
, targetUrl
);
84 fragment
.setArguments(args
);
89 public SamlWebViewDialog() {
95 public void onAttach(Activity activity
) {
96 Log_OC
.v(TAG
, "onAttach");
97 super.onAttach(activity
);
99 mSsoWebViewClientListener
= (SsoWebViewClientListener
) activity
;
100 mHandler
= new Handler();
101 mWebViewClient
= new SsoWebViewClient(activity
, mHandler
, mSsoWebViewClientListener
);
103 } catch (ClassCastException e
) {
104 throw new ClassCastException(activity
.toString() + " must implement " +
105 SsoWebViewClientListener
.class.getSimpleName());
110 @SuppressLint("SetJavaScriptEnabled")
112 public void onCreate(Bundle savedInstanceState
) {
113 Log_OC
.v(TAG
, "onCreate, savedInstanceState is " + savedInstanceState
);
114 super.onCreate(savedInstanceState
);
116 setRetainInstance(true
);
118 CookieSyncManager
.createInstance(getActivity().getApplicationContext());
120 if (savedInstanceState
== null
) {
121 mInitialUrl
= getArguments().getString(ARG_INITIAL_URL
);
122 mTargetUrl
= getArguments().getString(ARG_TARGET_URL
);
124 mInitialUrl
= savedInstanceState
.getString(ARG_INITIAL_URL
);
125 mTargetUrl
= savedInstanceState
.getString(ARG_TARGET_URL
);
128 setStyle(DialogFragment
.STYLE_NO_TITLE
, R
.style
.Theme_ownCloud_Dialog
);
131 @SuppressWarnings("deprecation")
132 @SuppressLint("SetJavaScriptEnabled")
134 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
135 Bundle savedInstanceState
) {
136 Log_OC
.v(TAG
, "onCreateView, savedInsanceState is " + savedInstanceState
);
138 // Inflate layout of the dialog
139 RelativeLayout ssoRootView
= (RelativeLayout
) inflater
.inflate(R
.layout
.sso_dialog
,
140 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(getActivity().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(MainApp
.getUserAgent());
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
.v(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
.v(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
);
204 super.onDestroyView();
208 public void onDestroy() {
209 Log_OC
.v(TAG
, "onDestroy");
214 public void onDetach() {
215 Log_OC
.v(TAG
, "onDetach");
216 mSsoWebViewClientListener
= null
;
217 mWebViewClient
= null
;
222 public void onCancel (DialogInterface dialog
) {
223 Log_OC
.d(TAG
, "onCancel");
224 super.onCancel(dialog
);
228 public void onDismiss (DialogInterface dialog
) {
229 Log_OC
.d(TAG
, "onDismiss");
230 super.onDismiss(dialog
);
234 public void onStart() {
235 Log_OC
.v(TAG
, "onStart");
240 public void onStop() {
241 Log_OC
.v(TAG
, "onStop");
246 public void onResume() {
247 Log_OC
.v(TAG
, "onResume");
249 mSsoWebView
.onResume();
253 public void onPause() {
254 Log_OC
.v(TAG
, "onPause");
255 mSsoWebView
.onPause();
260 public int show (FragmentTransaction transaction
, String tag
) {
261 Log_OC
.v(TAG
, "show (transaction)");
262 return super.show(transaction
, tag
);
266 public void show (FragmentManager manager
, String tag
) {
267 Log_OC
.v(TAG
, "show (manager)");
268 super.show(manager
, tag
);