1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 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
.authentication
;
20 import java
.lang
.ref
.WeakReference
;
22 import com
.owncloud
.android
.utils
.Log_OC
;
25 import android
.graphics
.Bitmap
;
26 import android
.os
.Handler
;
27 import android
.os
.Message
;
28 import android
.view
.View
;
29 import android
.webkit
.CookieManager
;
30 import android
.webkit
.WebView
;
31 import android
.webkit
.WebViewClient
;
35 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
36 * running in the {@link WebView} that is attached to.
38 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
39 * authentication process.
41 * @author David A. Velasco
43 public class SsoWebViewClient
extends WebViewClient
{
45 private static final String TAG
= SsoWebViewClient
.class.getSimpleName();
47 public interface SsoWebViewClientListener
{
48 public void onSsoFinished(String sessionCookie
);
51 private Handler mListenerHandler
;
52 private WeakReference
<SsoWebViewClientListener
> mListenerRef
;
53 private String mTargetUrl
;
54 private String mLastReloadedUrlAtError
;
56 public SsoWebViewClient (Handler listenerHandler
, SsoWebViewClientListener listener
) {
57 mListenerHandler
= listenerHandler
;
58 mListenerRef
= new WeakReference
<SsoWebViewClient
.SsoWebViewClientListener
>(listener
);
59 mTargetUrl
= "fake://url.to.be.set";
60 mLastReloadedUrlAtError
= null
;
63 public String
getTargetUrl() {
67 public void setTargetUrl(String targetUrl
) {
68 mTargetUrl
= targetUrl
;
72 public void onPageStarted (WebView view
, String url
, Bitmap favicon
) {
73 Log_OC
.d(TAG
, "onPageStarted : " + url
);
74 super.onPageStarted(view
, url
, favicon
);
78 public void onFormResubmission (WebView view
, Message dontResend
, Message resend
) {
79 Log_OC
.d(TAG
, "onFormResubMission ");
81 // necessary to grant reload of last page when device orientation is changed after sending a form
82 resend
.sendToTarget();
86 public boolean shouldOverrideUrlLoading(WebView view
, String url
) {
91 public void onReceivedError (WebView view
, int errorCode
, String description
, String failingUrl
) {
92 Log_OC
.e(TAG
, "onReceivedError : " + failingUrl
+ ", code " + errorCode
+ ", description: " + description
);
93 if (!failingUrl
.equals(mLastReloadedUrlAtError
)) {
95 mLastReloadedUrlAtError
= failingUrl
;
97 mLastReloadedUrlAtError
= null
;
98 super.onReceivedError(view
, errorCode
, description
, failingUrl
);
103 public void onPageFinished (WebView view
, String url
) {
104 Log_OC
.d(TAG
, "onPageFinished : " + url
);
105 mLastReloadedUrlAtError
= null
;
106 if (url
.startsWith(mTargetUrl
)) {
107 view
.setVisibility(View
.GONE
);
108 CookieManager cookieManager
= CookieManager
.getInstance();
109 final String cookies
= cookieManager
.getCookie(url
);
110 //Log_OC.d(TAG, "Cookies: " + cookies);
111 if (mListenerHandler
!= null
&& mListenerRef
!= null
) {
112 // this is good idea because onPageFinished is not running in the UI thread
113 mListenerHandler
.post(new Runnable() {
116 SsoWebViewClientListener listener
= mListenerRef
.get();
117 if (listener
!= null
) {
118 listener
.onSsoFinished(cookies
);
129 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
130 Log_OC.d(TAG, "doUpdateVisitedHistory : " + url);
134 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
135 Log_OC.d(TAG, "onReceivedSslError : " + error);
139 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
140 Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
144 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
145 Log_OC.d(TAG, "shouldInterceptRequest : " + url);
150 public void onLoadResource (WebView view, String url) {
151 Log_OC.d(TAG, "onLoadResource : " + url);
155 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
156 Log_OC.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
160 public void onScaleChanged (WebView view, float oldScale, float newScale) {
161 Log_OC.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
162 super.onScaleChanged(view, oldScale, newScale);
166 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
167 Log_OC.d(TAG, "onUnhandledKeyEvent : " + event);
171 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
172 Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);