The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.webkit; |
| 18 | |
| 19 | import android.graphics.Bitmap; |
| 20 | import android.net.http.SslError; |
| 21 | import android.os.Message; |
| 22 | import android.view.KeyEvent; |
| 23 | |
| 24 | public class WebViewClient { |
| 25 | |
| 26 | /** |
| 27 | * Give the host application a chance to take over the control when a new |
| 28 | * url is about to be loaded in the current WebView. If WebViewClient is not |
| 29 | * provided, by default WebView will ask Activity Manager to choose the |
| 30 | * proper handler for the url. If WebViewClient is provided, return true |
| 31 | * means the host application handles the url, while return false means the |
| 32 | * current WebView handles the url. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 33 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | * @param view The WebView that is initiating the callback. |
| 35 | * @param url The url to be loaded. |
| 36 | * @return True if the host application wants to leave the current WebView |
| 37 | * and handle the url itself, otherwise return false. |
| 38 | */ |
| 39 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Notify the host application that a page has started loading. This method |
| 45 | * is called once for each main frame load so a page with iframes or |
| 46 | * framesets will call onPageStarted one time for the main frame. This also |
| 47 | * means that onPageStarted will not be called when the contents of an |
| 48 | * embedded frame changes, i.e. clicking a link whose target is an iframe. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 49 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * @param view The WebView that is initiating the callback. |
| 51 | * @param url The url to be loaded. |
| 52 | * @param favicon The favicon for this page if it already exists in the |
| 53 | * database. |
| 54 | */ |
| 55 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Notify the host application that a page has finished loading. This method |
| 60 | * is called only for main frame. When onPageFinished() is called, the |
| 61 | * rendering picture may not be updated yet. To get the notification for the |
| 62 | * new Picture, use {@link WebView.PictureListener#onNewPicture}. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 63 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | * @param view The WebView that is initiating the callback. |
| 65 | * @param url The url of the page. |
| 66 | */ |
| 67 | public void onPageFinished(WebView view, String url) { |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Notify the host application that the WebView will load the resource |
| 72 | * specified by the given url. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 73 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | * @param view The WebView that is initiating the callback. |
| 75 | * @param url The url of the resource the WebView will load. |
| 76 | */ |
| 77 | public void onLoadResource(WebView view, String url) { |
| 78 | } |
| 79 | |
| 80 | /** |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 81 | * Notify the host application of a resource request and allow the |
| 82 | * application to return the data. If the return value is null, the WebView |
| 83 | * will continue to load the resource as usual. Otherwise, the return |
| 84 | * response and data will be used. NOTE: This method is called by the |
| 85 | * network thread so clients should exercise caution when accessing private |
| 86 | * data. |
| 87 | * |
| 88 | * @param view The {@link android.webkit.WebView} that is requesting the |
| 89 | * resource. |
| 90 | * @param url The raw url of the resource. |
| 91 | * @return A {@link android.webkit.WebResourceResponse} containing the |
| 92 | * response information or null if the WebView should load the |
| 93 | * resource itself. |
| 94 | */ |
| 95 | public WebResourceResponse shouldInterceptRequest(WebView view, |
| 96 | String url) { |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | * Notify the host application that there have been an excessive number of |
| 102 | * HTTP redirects. As the host application if it would like to continue |
| 103 | * trying to load the resource. The default behavior is to send the cancel |
| 104 | * message. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 105 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | * @param view The WebView that is initiating the callback. |
| 107 | * @param cancelMsg The message to send if the host wants to cancel |
| 108 | * @param continueMsg The message to send if the host wants to continue |
Patrick Scott | 6a5b0ec | 2010-01-08 09:55:33 -0500 | [diff] [blame] | 109 | * @deprecated This method is no longer called. When the WebView encounters |
| 110 | * a redirect loop, it will cancel the load. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | */ |
Kristian Monsen | b5503c1 | 2010-08-31 14:02:38 +0100 | [diff] [blame] | 112 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | public void onTooManyRedirects(WebView view, Message cancelMsg, |
| 114 | Message continueMsg) { |
| 115 | cancelMsg.sendToTarget(); |
| 116 | } |
| 117 | |
Patrick Scott | 05c9ed9 | 2009-08-25 13:51:08 -0400 | [diff] [blame] | 118 | // These ints must match up to the hidden values in EventHandler. |
| 119 | /** Generic error */ |
| 120 | public static final int ERROR_UNKNOWN = -1; |
| 121 | /** Server or proxy hostname lookup failed */ |
| 122 | public static final int ERROR_HOST_LOOKUP = -2; |
| 123 | /** Unsupported authentication scheme (not basic or digest) */ |
| 124 | public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3; |
| 125 | /** User authentication failed on server */ |
| 126 | public static final int ERROR_AUTHENTICATION = -4; |
| 127 | /** User authentication failed on proxy */ |
| 128 | public static final int ERROR_PROXY_AUTHENTICATION = -5; |
| 129 | /** Failed to connect to the server */ |
| 130 | public static final int ERROR_CONNECT = -6; |
| 131 | /** Failed to read or write to the server */ |
| 132 | public static final int ERROR_IO = -7; |
| 133 | /** Connection timed out */ |
| 134 | public static final int ERROR_TIMEOUT = -8; |
| 135 | /** Too many redirects */ |
| 136 | public static final int ERROR_REDIRECT_LOOP = -9; |
| 137 | /** Unsupported URI scheme */ |
| 138 | public static final int ERROR_UNSUPPORTED_SCHEME = -10; |
| 139 | /** Failed to perform SSL handshake */ |
| 140 | public static final int ERROR_FAILED_SSL_HANDSHAKE = -11; |
| 141 | /** Malformed URL */ |
| 142 | public static final int ERROR_BAD_URL = -12; |
| 143 | /** Generic file error */ |
| 144 | public static final int ERROR_FILE = -13; |
| 145 | /** File not found */ |
| 146 | public static final int ERROR_FILE_NOT_FOUND = -14; |
| 147 | /** Too many requests during this load */ |
| 148 | public static final int ERROR_TOO_MANY_REQUESTS = -15; |
| 149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | /** |
Patrick Scott | 05c9ed9 | 2009-08-25 13:51:08 -0400 | [diff] [blame] | 151 | * Report an error to the host application. These errors are unrecoverable |
| 152 | * (i.e. the main resource is unavailable). The errorCode parameter |
| 153 | * corresponds to one of the ERROR_* constants. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | * @param view The WebView that is initiating the callback. |
Patrick Scott | 447811c | 2009-09-01 09:57:20 -0400 | [diff] [blame] | 155 | * @param errorCode The error code corresponding to an ERROR_* value. |
Patrick Scott | 05c9ed9 | 2009-08-25 13:51:08 -0400 | [diff] [blame] | 156 | * @param description A String describing the error. |
| 157 | * @param failingUrl The url that failed to load. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | */ |
| 159 | public void onReceivedError(WebView view, int errorCode, |
| 160 | String description, String failingUrl) { |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * As the host application if the browser should resend data as the |
| 165 | * requested page was a result of a POST. The default is to not resend the |
| 166 | * data. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 167 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | * @param view The WebView that is initiating the callback. |
| 169 | * @param dontResend The message to send if the browser should not resend |
| 170 | * @param resend The message to send if the browser should resend data |
| 171 | */ |
| 172 | public void onFormResubmission(WebView view, Message dontResend, |
| 173 | Message resend) { |
| 174 | dontResend.sendToTarget(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Notify the host application to update its visited links database. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 179 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | * @param view The WebView that is initiating the callback. |
| 181 | * @param url The url being visited. |
| 182 | * @param isReload True if this url is being reloaded. |
| 183 | */ |
| 184 | public void doUpdateVisitedHistory(WebView view, String url, |
| 185 | boolean isReload) { |
| 186 | } |
| 187 | |
| 188 | /** |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 189 | * Notify the host application to handle a SSL certificate error request |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | * (display the error to the user and ask whether to proceed or not). The |
| 191 | * host application has to call either handler.cancel() or handler.proceed() |
| 192 | * as the connection is suspended and waiting for the response. The default |
| 193 | * behavior is to cancel the load. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 194 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | * @param view The WebView that is initiating the callback. |
| 196 | * @param handler An SslErrorHandler object that will handle the user's |
| 197 | * response. |
| 198 | * @param error The SSL error object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | */ |
| 200 | public void onReceivedSslError(WebView view, SslErrorHandler handler, |
| 201 | SslError error) { |
| 202 | handler.cancel(); |
| 203 | } |
| 204 | |
| 205 | /** |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 206 | * Notify the host application to handle a SSL client certificate |
| 207 | * request (display the request to the user and ask whether to |
| 208 | * proceed with a client certificate or not). The host application |
| 209 | * has to call either handler.cancel() or handler.proceed() as the |
| 210 | * connection is suspended and waiting for the response. The |
| 211 | * default behavior is to cancel, returning no client certificate. |
| 212 | * |
| 213 | * @param view The WebView that is initiating the callback. |
| 214 | * @param handler An ClientCertRequestHandler object that will |
| 215 | * handle the user's response. |
| 216 | * @param host_and_port The host and port of the requesting server. |
| 217 | * |
| 218 | * @hide |
| 219 | */ |
| 220 | public void onReceivedClientCertRequest(WebView view, |
| 221 | ClientCertRequestHandler handler, String host_and_port) { |
| 222 | handler.cancel(); |
| 223 | } |
| 224 | |
| 225 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | * Notify the host application to handle an authentication request. The |
| 227 | * default behavior is to cancel the request. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 228 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | * @param view The WebView that is initiating the callback. |
| 230 | * @param handler The HttpAuthHandler that will handle the user's response. |
| 231 | * @param host The host requiring authentication. |
| 232 | * @param realm A description to help store user credentials for future |
| 233 | * visits. |
| 234 | */ |
| 235 | public void onReceivedHttpAuthRequest(WebView view, |
| 236 | HttpAuthHandler handler, String host, String realm) { |
| 237 | handler.cancel(); |
| 238 | } |
| 239 | |
| 240 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | * Give the host application a chance to handle the key event synchronously. |
| 242 | * e.g. menu shortcut key events need to be filtered this way. If return |
| 243 | * true, WebView will not handle the key event. If return false, WebView |
| 244 | * will always handle the key event, so none of the super in the view chain |
| 245 | * will see the key event. The default behavior returns false. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 246 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | * @param view The WebView that is initiating the callback. |
| 248 | * @param event The key event. |
| 249 | * @return True if the host application wants to handle the key event |
| 250 | * itself, otherwise return false |
| 251 | */ |
| 252 | public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Notify the host application that a key was not handled by the WebView. |
| 258 | * Except system keys, WebView always consumes the keys in the normal flow |
| 259 | * or if shouldOverrideKeyEvent returns true. This is called asynchronously |
| 260 | * from where the key is dispatched. It gives the host application an chance |
| 261 | * to handle the unhandled key events. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 262 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | * @param view The WebView that is initiating the callback. |
| 264 | * @param event The key event. |
| 265 | */ |
| 266 | public void onUnhandledKeyEvent(WebView view, KeyEvent event) { |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Notify the host application that the scale applied to the WebView has |
| 271 | * changed. |
Brian Carlstrom | a147759 | 2011-02-11 13:39:56 -0800 | [diff] [blame] | 272 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | * @param view he WebView that is initiating the callback. |
| 274 | * @param oldScale The old scale factor |
| 275 | * @param newScale The new scale factor |
| 276 | */ |
| 277 | public void onScaleChanged(WebView view, float oldScale, float newScale) { |
| 278 | } |
Patrick Scott | 85a50ff | 2011-01-25 14:42:12 -0500 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * Notify the host application that a request to automatically log in the |
| 282 | * user has been processed. |
| 283 | * @param view The WebView requesting the login. |
| 284 | * @param realm The account realm used to look up accounts. |
| 285 | * @param account An optional account. If not null, the account should be |
| 286 | * checked against accounts on the device. If it is a valid |
| 287 | * account, it should be used to log in the user. |
| 288 | * @param args Authenticator specific arguments used to log in the user. |
| 289 | */ |
| 290 | public void onReceivedLoginRequest(WebView view, String realm, |
| 291 | String account, String args) { |
| 292 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | } |