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 de
.mobilcom
.debitel
.cloud
.android
.authentication
; 
  20 import java
.lang
.ref
.WeakReference
; 
  22 import de
.mobilcom
.debitel
.cloud
.android
.Log_OC
; 
  24 import android
.graphics
.Bitmap
; 
  25 import android
.os
.Handler
; 
  26 import android
.os
.Message
; 
  27 import android
.view
.View
; 
  28 import android
.webkit
.CookieManager
; 
  29 import android
.webkit
.WebView
; 
  30 import android
.webkit
.WebViewClient
; 
  34  * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process  
  35  * running in the {@link WebView} that is attached to. 
  37  * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the 
  38  * authentication process. 
  40  * @author David A. Velasco 
  42 public class SsoWebViewClient 
extends WebViewClient 
{ 
  44     private static final String TAG 
= SsoWebViewClient
.class.getSimpleName(); 
  46     public interface SsoWebViewClientListener 
{ 
  47         public void onSsoFinished(String sessionCookie
); 
  50     private Handler mListenerHandler
; 
  51     private WeakReference
<SsoWebViewClientListener
> mListenerRef
; 
  52     private String mTargetUrl
; 
  53     private String mLastReloadedUrlAtError
; 
  55     public SsoWebViewClient (Handler listenerHandler
, SsoWebViewClientListener listener
) { 
  56         mListenerHandler 
= listenerHandler
; 
  57         mListenerRef 
= new WeakReference
<SsoWebViewClient
.SsoWebViewClientListener
>(listener
); 
  58         mTargetUrl 
= "fake://url.to.be.set"; 
  59         mLastReloadedUrlAtError 
= null
; 
  62     public String 
getTargetUrl() { 
  66     public void setTargetUrl(String targetUrl
) { 
  67         mTargetUrl 
= targetUrl
; 
  71     public void onPageStarted (WebView view
, String url
, Bitmap favicon
) { 
  72         Log_OC
.d(TAG
, "onPageStarted : " + url
); 
  73         super.onPageStarted(view
, url
, favicon
); 
  77     public void onFormResubmission (WebView view
, Message dontResend
, Message resend
) { 
  78         Log_OC
.d(TAG
, "onFormResubMission "); 
  80         // necessary to grant reload of last page when device orientation is changed after sending a form 
  81         resend
.sendToTarget(); 
  85     public boolean shouldOverrideUrlLoading(WebView view
, String url
) { 
  90     public void onReceivedError (WebView view
, int errorCode
, String description
, String failingUrl
) { 
  91         Log_OC
.e(TAG
, "onReceivedError : " + failingUrl 
+ ", code " + errorCode 
+ ", description: " + description
); 
  92         if (!failingUrl
.equals(mLastReloadedUrlAtError
)) { 
  94             mLastReloadedUrlAtError 
= failingUrl
; 
  96             mLastReloadedUrlAtError 
= null
; 
  97             super.onReceivedError(view
, errorCode
, description
, failingUrl
); 
 102     public void onPageFinished (WebView view
, String url
) { 
 103         Log_OC
.d(TAG
, "onPageFinished : " + url
); 
 104         mLastReloadedUrlAtError 
= null
; 
 105         if (url
.startsWith(mTargetUrl
)) { 
 106             view
.setVisibility(View
.GONE
); 
 107             CookieManager cookieManager 
= CookieManager
.getInstance(); 
 108             final String cookies 
= cookieManager
.getCookie(url
); 
 109             //Log_OC.d(TAG, "Cookies: " + cookies); 
 110             if (mListenerHandler 
!= null 
&& mListenerRef 
!= null
) { 
 111                 // this is good idea because onPageFinished is not running in the UI thread 
 112                 mListenerHandler
.post(new Runnable() { 
 115                         SsoWebViewClientListener listener 
= mListenerRef
.get(); 
 116                         if (listener 
!= null
) { 
 117                             listener
.onSsoFinished(cookies
); 
 128     public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) { 
 129         Log_OC.d(TAG, "doUpdateVisitedHistory : " + url); 
 133     public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { 
 134         Log_OC.d(TAG, "onReceivedSslError : " + error); 
 138     public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) { 
 139         Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host); 
 143     public WebResourceResponse shouldInterceptRequest (WebView view, String url) { 
 144         Log_OC.d(TAG, "shouldInterceptRequest : " + url); 
 149     public void onLoadResource (WebView view, String url) { 
 150         Log_OC.d(TAG, "onLoadResource : " + url);    
 154     public void onReceivedLoginRequest (WebView view, String realm, String account, String args) { 
 155         Log_OC.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args); 
 159     public void onScaleChanged (WebView view, float oldScale, float newScale) { 
 160         Log_OC.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale); 
 161         super.onScaleChanged(view, oldScale, newScale); 
 165     public void onUnhandledKeyEvent (WebView view, KeyEvent event) { 
 166         Log_OC.d(TAG, "onUnhandledKeyEvent : " + event); 
 170     public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) { 
 171         Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);