refresh account when session expires
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / SsoWebViewClient.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.authentication;
19
20 import java.lang.ref.WeakReference;
21
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;
28
29 import com.owncloud.android.Log_OC;
30
31 /**
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.
34 *
35 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
36 * authentication process.
37 *
38 * @author David A. Velasco
39 */
40 public class SsoWebViewClient extends WebViewClient {
41
42 private static final String TAG = SsoWebViewClient.class.getSimpleName();
43
44 public interface SsoWebViewClientListener {
45 public void onSsoFinished(String sessionCookie);
46 }
47
48 private Handler mListenerHandler;
49 private WeakReference<SsoWebViewClientListener> mListenerRef;
50 private String mTargetUrl;
51
52 public SsoWebViewClient (Handler listenerHandler, SsoWebViewClientListener listener) {
53 mListenerHandler = listenerHandler;
54 mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
55 mTargetUrl = "fake://url.to.be.set";
56 }
57
58 public String getTargetUrl() {
59 return mTargetUrl;
60 }
61
62 public void setTargetUrl(String targetUrl) {
63 mTargetUrl = targetUrl;
64 }
65
66 @Override
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() {
76 @Override
77 public void run() {
78 SsoWebViewClientListener listener = mListenerRef.get();
79 if (listener != null) {
80 listener.onSsoFinished(cookies);
81 }
82 }
83 });
84 }
85 }
86 }
87
88 @Override
89 public boolean shouldOverrideUrlLoading(WebView view, String url) {
90 //view.loadUrl(url);
91 return false;
92 }
93
94 @Override
95 public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
96 Log_OC.e(TAG, "onReceivedError : " + failingUrl);
97 }
98
99 /*
100
101 @Override
102 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
103 Log_OC.e(TAG, "doUpdateVisitedHistory : " + url);
104 }
105
106 @Override
107 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
108 Log_OC.e(TAG, "onReceivedSslError : " + error);
109 }
110
111 @Override
112 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
113 Log_OC.e(TAG, "onReceivedHttpAuthRequest : " + host);
114 }
115
116 @Override
117 public void onPageFinished (WebView view, String url) {
118 Log_OC.e(TAG, "onPageFinished : " + url);
119 }
120
121 @Override
122 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
123 Log_OC.e(TAG, "shouldInterceptRequest : " + url);
124 return null;
125 }
126
127 @Override
128 public void onLoadResource (WebView view, String url) {
129 Log_OC.e(TAG, "onLoadResource : " + url);
130 }
131
132 @Override
133 public void onFormResubmission (WebView view, Message dontResend, Message resend) {
134 Log_OC.e(TAG, "onFormResubMission ");
135 super.onFormResubmission(view, dontResend, resend);
136 }
137
138 @Override
139 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
140 Log_OC.e(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
141 }
142
143 @Override
144 public void onScaleChanged (WebView view, float oldScale, float newScale) {
145 Log_OC.e(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
146 }
147
148 @Override
149 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
150 Log_OC.e(TAG, "onUnhandledKeyEvent : " + event);
151 }
152
153 @Override
154 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
155 Log_OC.e(TAG, "shouldOverrideKeyEvent : " + event);
156 return false;
157 }
158
159 */
160 }