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
.oc_framework
.accounts
;
20 import java
.lang
.ref
.WeakReference
;
22 import android
.graphics
.Bitmap
;
23 import android
.net
.http
.SslError
;
24 import android
.os
.Handler
;
25 import android
.os
.Message
;
26 import android
.util
.Log
;
27 import android
.view
.KeyEvent
;
28 import android
.view
.View
;
29 import android
.webkit
.CookieManager
;
30 import android
.webkit
.HttpAuthHandler
;
31 import android
.webkit
.SslErrorHandler
;
32 import android
.webkit
.WebResourceResponse
;
33 import android
.webkit
.WebView
;
34 import android
.webkit
.WebViewClient
;
38 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
39 * running in the {@link WebView} that is attached to.
41 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
42 * authentication process.
44 * @author David A. Velasco
46 public class SsoWebViewClient
extends WebViewClient
{
48 private static final String TAG
= SsoWebViewClient
.class.getSimpleName();
50 public interface SsoWebViewClientListener
{
51 public void onSsoFinished(String sessionCookie
);
54 private Handler mListenerHandler
;
55 private WeakReference
<SsoWebViewClientListener
> mListenerRef
;
56 private String mTargetUrl
;
57 private String mLastReloadedUrlAtError
;
59 public SsoWebViewClient (Handler listenerHandler
, SsoWebViewClientListener listener
) {
60 mListenerHandler
= listenerHandler
;
61 mListenerRef
= new WeakReference
<SsoWebViewClient
.SsoWebViewClientListener
>(listener
);
62 mTargetUrl
= "fake://url.to.be.set";
63 mLastReloadedUrlAtError
= null
;
66 public String
getTargetUrl() {
70 public void setTargetUrl(String targetUrl
) {
71 mTargetUrl
= targetUrl
;
75 public void onPageStarted (WebView view
, String url
, Bitmap favicon
) {
76 Log
.d(TAG
, "onPageStarted : " + url
);
77 super.onPageStarted(view
, url
, favicon
);
81 public void onFormResubmission (WebView view
, Message dontResend
, Message resend
) {
82 Log
.d(TAG
, "onFormResubMission ");
84 // necessary to grant reload of last page when device orientation is changed after sending a form
85 resend
.sendToTarget();
89 public boolean shouldOverrideUrlLoading(WebView view
, String url
) {
94 public void onReceivedError (WebView view
, int errorCode
, String description
, String failingUrl
) {
95 Log
.e(TAG
, "onReceivedError : " + failingUrl
+ ", code " + errorCode
+ ", description: " + description
);
96 if (!failingUrl
.equals(mLastReloadedUrlAtError
)) {
98 mLastReloadedUrlAtError
= failingUrl
;
100 mLastReloadedUrlAtError
= null
;
101 super.onReceivedError(view
, errorCode
, description
, failingUrl
);
106 public void onPageFinished (WebView view
, String url
) {
107 Log
.d(TAG
, "onPageFinished : " + url
);
108 mLastReloadedUrlAtError
= null
;
109 if (url
.startsWith(mTargetUrl
)) {
110 view
.setVisibility(View
.GONE
);
111 CookieManager cookieManager
= CookieManager
.getInstance();
112 final String cookies
= cookieManager
.getCookie(url
);
113 Log
.d(TAG
, "Cookies: " + cookies
);
114 if (mListenerHandler
!= null
&& mListenerRef
!= null
) {
115 // this is good idea because onPageFinished is not running in the UI thread
116 mListenerHandler
.post(new Runnable() {
119 SsoWebViewClientListener listener
= mListenerRef
.get();
120 if (listener
!= null
) {
121 // Send Cookies to the listener
122 listener
.onSsoFinished(cookies
);
128 Log
.d(TAG
, "URL==> " + url
+ " mTarget==> " + mTargetUrl
);
134 public void doUpdateVisitedHistory (WebView view
, String url
, boolean isReload
) {
135 Log
.d(TAG
, "doUpdateVisitedHistory : " + url
);
139 public void onReceivedSslError (WebView view
, SslErrorHandler handler
, SslError error
) {
140 Log
.d(TAG
, "onReceivedSslError : " + error
);
145 public void onReceivedHttpAuthRequest (WebView view
, HttpAuthHandler handler
, String host
, String realm
) {
146 Log
.d(TAG
, "onReceivedHttpAuthRequest : " + host
);
150 public WebResourceResponse
shouldInterceptRequest (WebView view
, String url
) {
151 Log
.d(TAG
, "shouldInterceptRequest : " + url
);
156 public void onLoadResource (WebView view
, String url
) {
157 Log
.d(TAG
, "onLoadResource : " + url
);
161 public void onReceivedLoginRequest (WebView view
, String realm
, String account
, String args
) {
162 Log
.d(TAG
, "onReceivedLoginRequest : " + realm
+ ", " + account
+ ", " + args
);
166 public void onScaleChanged (WebView view
, float oldScale
, float newScale
) {
167 Log
.d(TAG
, "onScaleChanged : " + oldScale
+ " -> " + newScale
);
168 super.onScaleChanged(view
, oldScale
, newScale
);
172 public void onUnhandledKeyEvent (WebView view
, KeyEvent event
) {
173 Log
.d(TAG
, "onUnhandledKeyEvent : " + event
);
177 public boolean shouldOverrideKeyEvent (WebView view
, KeyEvent event
) {
178 Log
.d(TAG
, "shouldOverrideKeyEvent : " + event
);