Process share with password
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / SamlWebViewDialog.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Maria Asensio
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
7 *
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.
11 *
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.
16 *
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/>.
19 *
20 */
21
22 package com.owncloud.android.ui.dialog;
23
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;
40
41 import com.actionbarsherlock.app.SherlockDialogFragment;
42 import com.owncloud.android.R;
43 import com.owncloud.android.authentication.SsoWebViewClient;
44 import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;
45 import com.owncloud.android.lib.common.OwnCloudClient;
46 import com.owncloud.android.lib.common.utils.Log_OC;
47
48
49 /**
50 * Dialog to show the WebView for SAML Authentication
51 */
52 public class SamlWebViewDialog extends SherlockDialogFragment {
53
54 public final String SAML_DIALOG_TAG = "SamlWebViewDialog";
55
56 private final static String TAG = SamlWebViewDialog.class.getSimpleName();
57
58 private static final String ARG_INITIAL_URL = "INITIAL_URL";
59 private static final String ARG_TARGET_URL = "TARGET_URL";
60
61 private WebView mSsoWebView;
62 private SsoWebViewClient mWebViewClient;
63
64 private String mInitialUrl;
65 private String mTargetUrl;
66
67 private Handler mHandler;
68
69 private SsoWebViewClientListener mSsoWebViewClientListener;
70
71 /**
72 * Public factory method to get dialog instances.
73 *
74 * @param handler
75 * @param Url Url to open at WebView
76 * @param targetURL mBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
77 * @return New dialog instance, ready to show.
78 */
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);
85 return fragment;
86 }
87
88
89 public SamlWebViewDialog() {
90 super();
91 }
92
93
94 @Override
95 public void onAttach(Activity activity) {
96 Log_OC.v(TAG, "onAttach");
97 super.onAttach(activity);
98 try {
99 mSsoWebViewClientListener = (SsoWebViewClientListener) activity;
100 mHandler = new Handler();
101 mWebViewClient = new SsoWebViewClient(activity, mHandler, mSsoWebViewClientListener);
102
103 } catch (ClassCastException e) {
104 throw new ClassCastException(activity.toString() + " must implement " + SsoWebViewClientListener.class.getSimpleName());
105 }
106 }
107
108
109 @SuppressLint("SetJavaScriptEnabled")
110 @Override
111 public void onCreate(Bundle savedInstanceState) {
112 Log_OC.v(TAG, "onCreate, savedInstanceState is " + savedInstanceState);
113 super.onCreate(savedInstanceState);
114
115 setRetainInstance(true);
116
117 CookieSyncManager.createInstance(getSherlockActivity().getApplicationContext());
118
119 if (savedInstanceState == null) {
120 mInitialUrl = getArguments().getString(ARG_INITIAL_URL);
121 mTargetUrl = getArguments().getString(ARG_TARGET_URL);
122 } else {
123 mInitialUrl = savedInstanceState.getString(ARG_INITIAL_URL);
124 mTargetUrl = savedInstanceState.getString(ARG_TARGET_URL);
125 }
126
127 setStyle(SherlockDialogFragment.STYLE_NO_TITLE, R.style.Theme_ownCloud_Dialog);
128 }
129
130 @SuppressWarnings("deprecation")
131 @SuppressLint("SetJavaScriptEnabled")
132 @Override
133 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
134 Log_OC.v(TAG, "onCreateView, savedInsanceState is " + savedInstanceState);
135
136 // Inflate layout of the dialog
137 RelativeLayout ssoRootView = (RelativeLayout) inflater.inflate(R.layout.sso_dialog, container, false); // null parent view because it will go in the dialog layout
138
139 if (mSsoWebView == null) {
140 // initialize the WebView
141 mSsoWebView = new SsoWebView(getSherlockActivity().getApplicationContext());
142 mSsoWebView.setFocusable(true);
143 mSsoWebView.setFocusableInTouchMode(true);
144 mSsoWebView.setClickable(true);
145
146 WebSettings webSettings = mSsoWebView.getSettings();
147 webSettings.setJavaScriptEnabled(true);
148 webSettings.setBuiltInZoomControls(false);
149 webSettings.setLoadWithOverviewMode(false);
150 webSettings.setSavePassword(false);
151 webSettings.setUserAgentString(OwnCloudClient.USER_AGENT);
152 webSettings.setSaveFormData(false);
153
154 CookieManager cookieManager = CookieManager.getInstance();
155 cookieManager.setAcceptCookie(true);
156 cookieManager.removeAllCookie();
157
158 mSsoWebView.loadUrl(mInitialUrl);
159 }
160
161 mWebViewClient.setTargetUrl(mTargetUrl);
162 mSsoWebView.setWebViewClient(mWebViewClient);
163
164 // add the webview into the layout
165 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
166 RelativeLayout.LayoutParams.WRAP_CONTENT,
167 RelativeLayout.LayoutParams.WRAP_CONTENT
168 );
169 ssoRootView.addView(mSsoWebView, layoutParams);
170 ssoRootView.requestLayout();
171
172 return ssoRootView;
173 }
174
175 @Override
176 public void onSaveInstanceState(Bundle outState) {
177 Log_OC.v(TAG, "onSaveInstanceState being CALLED");
178 super.onSaveInstanceState(outState);
179
180 // save URLs
181 outState.putString(ARG_INITIAL_URL, mInitialUrl);
182 outState.putString(ARG_TARGET_URL, mTargetUrl);
183 }
184
185 @Override
186 public void onDestroyView() {
187 Log_OC.v(TAG, "onDestroyView");
188
189 if ((ViewGroup)mSsoWebView.getParent() != null) {
190 ((ViewGroup)mSsoWebView.getParent()).removeView(mSsoWebView);
191 }
192
193 mSsoWebView.setWebViewClient(null);
194
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);
199 }
200
201 super.onDestroyView();
202 }
203
204 @Override
205 public void onDestroy() {
206 Log_OC.v(TAG, "onDestroy");
207 super.onDestroy();
208 }
209
210 @Override
211 public void onDetach() {
212 Log_OC.v(TAG, "onDetach");
213 mSsoWebViewClientListener = null;
214 mWebViewClient = null;
215 super.onDetach();
216 }
217
218 @Override
219 public void onCancel (DialogInterface dialog) {
220 Log_OC.d(TAG, "onCancel");
221 super.onCancel(dialog);
222 }
223
224 @Override
225 public void onDismiss (DialogInterface dialog) {
226 Log_OC.d(TAG, "onDismiss");
227 super.onDismiss(dialog);
228 }
229
230 @Override
231 public void onStart() {
232 Log_OC.v(TAG, "onStart");
233 super.onStart();
234 }
235
236 @Override
237 public void onStop() {
238 Log_OC.v(TAG, "onStop");
239 super.onStop();
240 }
241
242 @Override
243 public void onResume() {
244 Log_OC.v(TAG, "onResume");
245 super.onResume();
246 mSsoWebView.onResume();
247 }
248
249 @Override
250 public void onPause() {
251 Log_OC.v(TAG, "onPause");
252 mSsoWebView.onPause();
253 super.onPause();
254 }
255
256 @Override
257 public int show (FragmentTransaction transaction, String tag) {
258 Log_OC.v(TAG, "show (transaction)");
259 return super.show(transaction, tag);
260 }
261
262 @Override
263 public void show (FragmentManager manager, String tag) {
264 Log_OC.v(TAG, "show (manager)");
265 super.show(manager, tag);
266 }
267
268 }