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 android
.graphics
.Bitmap
;
23 import android
.os
.Handler
;
24 import android
.view
.View
;
25 import android
.webkit
.CookieManager
;
26 import android
.webkit
.WebView
;
27 import android
.webkit
.WebViewClient
;
29 import com
.owncloud
.android
.Log_OC
;
32 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
33 * running in the {@link WebView} that is attached to.
35 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
36 * authentication process.
38 * @author David A. Velasco
40 public class SsoWebViewClient
extends WebViewClient
{
42 private static final String TAG
= SsoWebViewClient
.class.getSimpleName();
44 public interface SsoWebViewClientListener
{
45 public void onSsoFinished(String sessionCookie
);
48 private Handler mListenerHandler
;
49 private WeakReference
<SsoWebViewClientListener
> mListenerRef
;
50 private String mTargetUrl
;
52 public SsoWebViewClient (Handler listenerHandler
, SsoWebViewClientListener listener
) {
53 mListenerHandler
= listenerHandler
;
54 mListenerRef
= new WeakReference
<SsoWebViewClient
.SsoWebViewClientListener
>(listener
);
55 mTargetUrl
= "fake://url.to.be.set";
58 public String
getTargetUrl() {
62 public void setTargetUrl(String targetUrl
) {
63 mTargetUrl
= targetUrl
;
67 public void onPageStarted (WebView view
, String url
, Bitmap favicon
) {
68 //Log_OC.e(TAG, "onPageStarted : " + url);
69 if (url
.startsWith(mTargetUrl
)) {
70 view
.setVisibility(View
.GONE
);
71 CookieManager cookieManager
= CookieManager
.getInstance();
72 final String cookies
= cookieManager
.getCookie(url
);
73 if (mListenerHandler
!= null
&& mListenerRef
!= null
) {
74 // this is good idea because onPageStarted is not running in the UI thread
75 mListenerHandler
.post(new Runnable() {
78 SsoWebViewClientListener listener
= mListenerRef
.get();
79 if (listener
!= null
) {
80 listener
.onSsoFinished(cookies
);
89 public boolean shouldOverrideUrlLoading(WebView view
, String url
) {
95 public void onReceivedError (WebView view
, int errorCode
, String description
, String failingUrl
) {
96 Log_OC
.e(TAG
, "onReceivedError : " + failingUrl
);
102 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
103 Log_OC.e(TAG, "doUpdateVisitedHistory : " + url);
107 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
108 Log_OC.e(TAG, "onReceivedSslError : " + error);
112 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
113 Log_OC.e(TAG, "onReceivedHttpAuthRequest : " + host);
117 public void onPageFinished (WebView view, String url) {
118 Log_OC.e(TAG, "onPageFinished : " + url);
122 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
123 Log_OC.e(TAG, "shouldInterceptRequest : " + url);
128 public void onLoadResource (WebView view, String url) {
129 Log_OC.e(TAG, "onLoadResource : " + url);
133 public void onFormResubmission (WebView view, Message dontResend, Message resend) {
134 Log_OC.e(TAG, "onFormResubMission ");
135 super.onFormResubmission(view, dontResend, resend);
139 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
140 Log_OC.e(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
144 public void onScaleChanged (WebView view, float oldScale, float newScale) {
145 Log_OC.e(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
149 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
150 Log_OC.e(TAG, "onUnhandledKeyEvent : " + event);
154 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
155 Log_OC.e(TAG, "shouldOverrideKeyEvent : " + event);