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 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
.WebSettings
;
34 import android
.webkit
.WebView
;
35 import android
.widget
.RelativeLayout
;
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
.lib
.common
.OwnCloudClient
;
42 import com
.owncloud
.android
.lib
.common
.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";
60 private WebView mSsoWebView
;
61 private SsoWebViewClient mWebViewClient
;
63 private String mInitialUrl
;
64 private String mTargetUrl
;
66 private Handler mHandler
;
68 private SsoWebViewClientListener mSsoWebViewClientListener
;
71 * 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 Log_OC
.d(TAG
, "New instance");
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() {
91 Log_OC
.d(TAG
, "constructor");
96 public void onAttach(Activity activity
) {
97 Log_OC
.d(TAG
, "onAttach");
98 super.onAttach(activity
);
100 mSsoWebViewClientListener
= (SsoWebViewClientListener
) activity
;
101 mHandler
= new Handler();
102 mWebViewClient
= new SsoWebViewClient(activity
, mHandler
, mSsoWebViewClientListener
);
104 } catch (ClassCastException e
) {
105 throw new ClassCastException(activity
.toString() + " must implement " + SsoWebViewClientListener
.class.getSimpleName());
110 @SuppressLint("SetJavaScriptEnabled")
112 public void onCreate(Bundle savedInstanceState
) {
113 Log_OC
.d(TAG
, "onCreate, savedInstanceState is " + savedInstanceState
);
114 super.onCreate(savedInstanceState
);
116 setRetainInstance(true
);
118 CookieSyncManager
.createInstance(getSherlockActivity().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(SherlockDialogFragment
.STYLE_NO_TITLE
, R
.style
.Theme_ownCloud_Dialog
);
131 @SuppressWarnings("deprecation")
132 @SuppressLint("SetJavaScriptEnabled")
134 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
135 Log_OC
.d(TAG
, "onCreateView, savedInsanceState is " + savedInstanceState
);
137 // Inflate layout of the dialog
138 RelativeLayout ssoRootView
= (RelativeLayout
) inflater
.inflate(R
.layout
.sso_dialog
, container
, false
); // null parent view because it will go in the dialog layout
140 if (mSsoWebView
== null
) {
141 // initialize the WebView
142 mSsoWebView
= new SsoWebView(getSherlockActivity().getApplicationContext());
143 mSsoWebView
.setFocusable(true
);
144 mSsoWebView
.setFocusableInTouchMode(true
);
145 mSsoWebView
.setClickable(true
);
147 CookieManager cookieManager
= CookieManager
.getInstance();
148 cookieManager
.setAcceptCookie(true
);
149 cookieManager
.removeAllCookie();
150 mSsoWebView
.loadUrl(mInitialUrl
);
152 WebSettings webSettings
= mSsoWebView
.getSettings();
153 webSettings
.setJavaScriptEnabled(true
);
154 webSettings
.setBuiltInZoomControls(false
);
155 webSettings
.setLoadWithOverviewMode(false
);
156 webSettings
.setSavePassword(false
);
157 webSettings
.setUserAgentString(OwnCloudClient
.USER_AGENT
);
158 webSettings
.setSaveFormData(false
);
161 mWebViewClient
.setTargetUrl(mTargetUrl
);
162 mSsoWebView
.setWebViewClient(mWebViewClient
);
164 // add the webview into the layout
165 RelativeLayout
.LayoutParams layoutParams
= new RelativeLayout
.LayoutParams(
166 RelativeLayout
.LayoutParams
.WRAP_CONTENT
,
167 RelativeLayout
.LayoutParams
.WRAP_CONTENT
169 ssoRootView
.addView(mSsoWebView
, layoutParams
);
170 ssoRootView
.requestLayout();
176 public void onSaveInstanceState(Bundle outState
) {
177 Log_OC
.d(TAG
, "onSaveInstanceState being CALLED");
178 super.onSaveInstanceState(outState
);
181 outState
.putString(ARG_INITIAL_URL
, mInitialUrl
);
182 outState
.putString(ARG_TARGET_URL
, mTargetUrl
);
186 public void onDestroyView() {
187 Log_OC
.d(TAG
, "onDestroyView");
189 if ((ViewGroup
)mSsoWebView
.getParent() != null
) {
190 ((ViewGroup
)mSsoWebView
.getParent()).removeView(mSsoWebView
);
193 mSsoWebView
.setWebViewClient(null
);
195 // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
196 Dialog dialog
= getDialog();
197 if ((dialog
!= null
)) {
198 dialog
.setOnDismissListener(null
);
200 //dialog.setDismissMessage(null);
203 super.onDestroyView();
207 public void onDestroy() {
208 Log_OC
.d(TAG
, "onDestroy");
213 public void onDetach() {
214 Log_OC
.d(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
.d(TAG
, "onStart");
239 public void onStop() {
240 Log_OC
.d(TAG
, "onStop");
245 public void onResume() {
246 Log_OC
.d(TAG
, "onResume");
248 mSsoWebView
.onResume();
252 public void onPause() {
253 Log_OC
.d(TAG
, "onPause");
254 mSsoWebView
.onPause();
259 public int show (FragmentTransaction transaction
, String tag
) {
260 Log_OC
.d(TAG
, "show (transaction)");
261 return super.show(transaction
, tag
);
265 public void show (FragmentManager manager
, String tag
) {
266 Log_OC
.d(TAG
, "show (manager)");
267 super.show(manager
, tag
);