2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.xwalk.core;
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.graphics.Bitmap;
24 import android.net.http.SslError;
25 import android.os.Message;
26 import android.view.KeyEvent;
27 import android.webkit.ValueCallback;
28 import android.webkit.WebResourceResponse;
29 import android.widget.EditText;
30 import android.widget.FrameLayout;
31 import android.widget.LinearLayout;
32 import android.widget.TextView;
35 * It's the Internal class to handle legacy resource related callbacks not
36 * handled by XWalkResourceClient.
40 public class XWalkClient {
42 private Context mContext;
43 private AlertDialog mDialog;
44 private XWalkView mXWalkView;
46 public XWalkClient(XWalkView view) {
47 mContext = view.getContext();
52 * Give the host application a chance to take over the control when a new
53 * url is about to be loaded in the current XWalkView. If XWalkClient is not
54 * provided, by default XWalkView will ask Activity Manager to choose the
55 * proper handler for the url. If XWalkClient is provided, return true
56 * means the host application handles the url, while return false means the
57 * current XWalkView handles the url.
59 * @param view The XWalkView that is initiating the callback.
60 * @param url The url to be loaded.
61 * @return True if the host application wants to leave the current XWalkView
62 * and handle the url itself, otherwise return false.
64 public boolean shouldOverrideUrlLoading(XWalkView view, String url) {
69 * Notify the host application that a page has started loading. This method
70 * is called once for each main frame load so a page with iframes or
71 * framesets will call onPageStarted one time for the main frame. This also
72 * means that onPageStarted will not be called when the contents of an
73 * embedded frame changes, i.e. clicking a link whose target is an iframe.
75 * @param view The XWalkView that is initiating the callback.
76 * @param url The url to be loaded.
78 public void onPageStarted(XWalkView view, String url) {
82 * Notify the host application that a page has finished loading. This method
83 * is called only for main frame. When onPageFinished() is called, the
84 * rendering picture may not be updated yet. To get the notification for the
85 * new Picture, use {@link XWalkView.PictureListener#onNewPicture}.
87 * @param view The XWalkView that is initiating the callback.
88 * @param url The url of the page.
90 public void onPageFinished(XWalkView view, String url) {
94 * Notify the host application that the renderer of XWalkView is hung.
96 * @param view The XWalkView on which the render is hung.
98 public void onRendererUnresponsive(XWalkView view) {
102 * Notify the host application that the renderer of XWalkView is no longer hung.
104 * @param view The XWalkView which becomes responsive now.
106 public void onRendererResponsive(XWalkView view) {
110 * Notify the host application that there have been an excessive number of
111 * HTTP redirects. As the host application if it would like to continue
112 * trying to load the resource. The default behavior is to send the cancel
115 * @param view The XWalkView that is initiating the callback.
116 * @param cancelMsg The message to send if the host wants to cancel
117 * @param continueMsg The message to send if the host wants to continue
118 * @deprecated This method is no longer called. When the XWalkView encounters
119 * a redirect loop, it will cancel the load.
122 public void onTooManyRedirects(XWalkView view, Message cancelMsg,
123 Message continueMsg) {
124 cancelMsg.sendToTarget();
128 * As the host application if the browser should resend data as the
129 * requested page was a result of a POST. The default is to not resend the
132 * @param view The XWalkView that is initiating the callback.
133 * @param dontResend The message to send if the browser should not resend
134 * @param resend The message to send if the browser should resend data
136 public void onFormResubmission(XWalkView view, Message dontResend,
138 dontResend.sendToTarget();
142 * Notify the host application to update its visited links database.
144 * @param view The XWalkView that is initiating the callback.
145 * @param url The url being visited.
146 * @param isReload True if this url is being reloaded.
148 public void doUpdateVisitedHistory(XWalkView view, String url,
153 * Notify the host application that an SSL error occurred while loading a
154 * resource. The host application must call either handler.cancel() or
155 * handler.proceed(). Note that the decision may be retained for use in
156 * response to future SSL errors. The default behavior is to cancel the
159 * @param view The XWalkView that is initiating the callback.
160 * @param callback The callback class. Passing 'true' means accepting the
161 * ssl error and continue to load. Passing 'false' means
162 * forbidding to load the web page.
163 * @param error The SSL error object.
165 public void onReceivedSslError(XWalkView view, ValueCallback<Boolean> callback,
167 final ValueCallback<Boolean> valueCallback = callback;
168 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
169 // Don't use setOnDismissListener as it requires API level 17.
170 dialogBuilder.setTitle(R.string.ssl_alert_title)
171 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
173 public void onClick(DialogInterface dialog, int which) {
174 valueCallback.onReceiveValue(true);
177 }).setNegativeButton(android.R.string.cancel, null)
178 .setOnCancelListener(new DialogInterface.OnCancelListener() {
179 public void onCancel(DialogInterface dialog) {
180 valueCallback.onReceiveValue(false);
183 mDialog = dialogBuilder.create();
188 * Notify the host application that an SSL error occurred while loading a
189 * resource, but the XWalkView chose to proceed anyway based on a
190 * decision retained from a previous response to onReceivedSslError().
193 public void onProceededAfterSslError(XWalkView view, SslError error) {
197 * Notify the host application to handle a SSL client certificate
198 * request (display the request to the user and ask whether to
199 * proceed with a client certificate or not). The host application
200 * has to call either handler.cancel() or handler.proceed() as the
201 * connection is suspended and waiting for the response. The
202 * default behavior is to cancel, returning no client certificate.
204 * @param view The XWalkView that is initiating the callback.
205 * @param handler A ClientCertRequestHandler object that will
206 * handle the user's response.
207 * @param host_and_port The host and port of the requesting server.
211 // TODO: comment this method temporarily, will implemtent later when all
212 // dependencies are resovled.
213 // public void onReceivedClientCertRequest(XWalkView view,
214 // ClientCertRequestHandler handler, String host_and_port) {
219 * Notify the host application to handle an authentication request. The
220 * default behavior is to cancel the request.
222 * @param view The XWalkView that is initiating the callback.
223 * @param handler The XWalkHttpAuthHandler that will handle the user's response.
224 * @param host The host requiring authentication.
225 * @param realm A description to help store user credentials for future
228 public void onReceivedHttpAuthRequest(XWalkView view,
229 XWalkHttpAuthHandler handler, String host, String realm) {
230 if (view == null) return;
232 final XWalkHttpAuthHandler haHandler = handler;
233 LinearLayout layout = new LinearLayout(mContext);
234 final EditText userNameEditText = new EditText(mContext);
235 final EditText passwordEditText = new EditText(mContext);
236 layout.setOrientation(LinearLayout.VERTICAL);
237 layout.setPaddingRelative(10, 0, 10, 20);
238 userNameEditText.setHint(R.string.http_auth_user_name);
239 passwordEditText.setHint(R.string.http_auth_password);
240 layout.addView(userNameEditText);
241 layout.addView(passwordEditText);
243 final Activity curActivity = mXWalkView.getActivity();
244 AlertDialog.Builder httpAuthDialog = new AlertDialog.Builder(curActivity);
245 httpAuthDialog.setTitle(R.string.http_auth_title)
247 .setCancelable(false)
248 .setPositiveButton(R.string.http_auth_log_in, new DialogInterface.OnClickListener() {
249 public void onClick(DialogInterface dialog, int whichButton) {
250 String userName = userNameEditText.getText().toString();
251 String password = passwordEditText.getText().toString();
252 haHandler.proceed(userName, password);
255 }).setNegativeButton(android.R.string.cancel, null)
256 .setOnCancelListener(new DialogInterface.OnCancelListener() {
257 public void onCancel(DialogInterface dialog) {
264 * Give the host application a chance to handle the key event synchronously.
265 * e.g. menu shortcut key events need to be filtered this way. If return
266 * true, XWalkView will not handle the key event. If return false, XWalkView
267 * will always handle the key event, so none of the super in the view chain
268 * will see the key event. The default behavior returns false.
270 * @param view The XWalkView that is initiating the callback.
271 * @param event The key event.
272 * @return True if the host application wants to handle the key event
273 * itself, otherwise return false
275 public boolean shouldOverrideKeyEvent(XWalkView view, KeyEvent event) {
280 * Notify the host application that a key was not handled by the XWalkView.
281 * Except system keys, XWalkView always consumes the keys in the normal flow
282 * or if shouldOverrideKeyEvent returns true. This is called asynchronously
283 * from where the key is dispatched. It gives the host application a chance
284 * to handle the unhandled key events.
286 * @param view The XWalkView that is initiating the callback.
287 * @param event The key event.
289 public void onUnhandledKeyEvent(XWalkView view, KeyEvent event) {
290 // TODO: Commment the below code for compile
291 // ViewRootImpl root = view.getViewRootImpl();
292 // if (root != null) {
293 // root.dispatchUnhandledKey(event);
298 * Notify the host application that a request to automatically log in the
299 * user has been processed.
300 * @param view The XWalkView requesting the login.
301 * @param realm The account realm used to look up accounts.
302 * @param account An optional account. If not null, the account should be
303 * checked against accounts on the device. If it is a valid
304 * account, it should be used to log in the user.
305 * @param args Authenticator specific arguments used to log in the user.
307 public void onReceivedLoginRequest(XWalkView view, String realm,
308 String account, String args) {
311 // TODO(yongsheng): legacy method. Consider removing it?
312 public void onLoadResource(XWalkView view, String url) {