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
.os
.Message
;
25 import android
.view
.View
;
26 import android
.webkit
.CookieManager
;
27 import android
.webkit
.WebView
;
28 import android
.webkit
.WebViewClient
;
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 //Log_OC.e(TAG, cookies);
74 if (mListenerHandler
!= null
&& mListenerRef
!= null
) {
75 // this is good idea because onPageStarted is not running in the UI thread
76 mListenerHandler
.post(new Runnable() {
79 SsoWebViewClientListener listener
= mListenerRef
.get();
80 if (listener
!= null
) {
81 listener
.onSsoFinished(cookies
);
90 public void onFormResubmission (WebView view
, Message dontResend
, Message resend
) {
91 //Log_OC.e(TAG, "onFormResubMission ");
93 // necessary to grant reload of last page when device orientation is changed after sending a form
94 resend
.sendToTarget();
99 public boolean shouldOverrideUrlLoading(WebView view, String url) {
105 public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
106 Log_OC.e(TAG, "onReceivedError : " + failingUrl);
110 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
111 Log_OC.e(TAG, "doUpdateVisitedHistory : " + url);
115 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
116 Log_OC.e(TAG, "onReceivedSslError : " + error);
120 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
121 Log_OC.e(TAG, "onReceivedHttpAuthRequest : " + host);
125 public void onPageFinished (WebView view, String url) {
126 Log_OC.e(TAG, "onPageFinished : " + url);
130 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
131 Log_OC.e(TAG, "shouldInterceptRequest : " + url);
136 public void onLoadResource (WebView view, String url) {
137 Log_OC.e(TAG, "onLoadResource : " + url);
141 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
142 Log_OC.e(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
146 public void onScaleChanged (WebView view, float oldScale, float newScale) {
147 Log_OC.e(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
151 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
152 Log_OC.e(TAG, "onUnhandledKeyEvent : " + event);
156 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
157 Log_OC.e(TAG, "shouldOverrideKeyEvent : " + event);