1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core;
7 import android.net.Uri;
8 import android.os.Message;
9 import android.view.KeyEvent;
10 import android.webkit.ValueCallback;
12 import org.xwalk.core.internal.XWalkJavascriptResultInternal;
13 import org.xwalk.core.internal.XWalkUIClientInternal;
14 import org.xwalk.core.internal.XWalkViewInternal;
17 * This class notifies the embedder UI events/callbacks.
19 public class XWalkUIClient extends XWalkUIClientInternal {
23 * @param view the owner XWalkView instance.
26 public XWalkUIClient(XWalkView view) {
31 * The type of JavaScript modal dialog.
34 public enum JavascriptMessageType {
35 /** JavaScript alert dialog. */
37 /** JavaScript confirm dialog. */
39 /** JavaScript prompt dialog. */
41 /** JavaScript dialog for a window-before-unload notification. */
42 JAVASCRIPT_BEFOREUNLOAD
46 * Tell the client to display a prompt dialog to the user.
47 * @param view the owner XWalkView instance.
48 * @param type the type of JavaScript modal dialog.
49 * @param url the url of the web page which wants to show this dialog.
50 * @param message the message to be shown.
51 * @param defaultValue the default value string. Only valid for Prompt dialog.
52 * @param result the callback to handle the result from caller.
55 public boolean onJavascriptModalDialog(XWalkView view, JavascriptMessageType type,
56 String url, String message, String defaultValue, XWalkJavascriptResult result) {
57 XWalkJavascriptResultInternal resultInternal =
58 ((XWalkJavascriptResultHandler) result).getInternal();
59 JavascriptMessageTypeInternal typeInternal =
60 JavascriptMessageTypeInternal.valueOf(type.toString());
61 return super.onJavascriptModalDialog(
62 view, typeInternal, url, message, defaultValue, resultInternal);
69 public boolean onJavascriptModalDialog(XWalkViewInternal view,
70 JavascriptMessageTypeInternal typeInternal,
71 String url, String message, String defaultValue, XWalkJavascriptResultInternal result) {
72 JavascriptMessageType type = JavascriptMessageType.valueOf(typeInternal.toString());
73 if (view instanceof XWalkView) {
74 return onJavascriptModalDialog(
76 type, url, message, defaultValue,
77 new XWalkJavascriptResultHandler(result));
80 return super.onJavascriptModalDialog(
81 view, typeInternal, url, message, defaultValue, result);
85 * Request display and focus for this XWalkView.
86 * @param view the owner XWalkView instance.
89 public void onRequestFocus(XWalkView view) {
90 super.onRequestFocus(view);
97 public void onRequestFocus(XWalkViewInternal view) {
98 if (view instanceof XWalkView) {
99 onRequestFocus((XWalkView) view);
101 super.onRequestFocus(view);
106 * Notify the client to close the given XWalkView.
107 * @param view the owner XWalkView instance.
110 public void onJavascriptCloseWindow(XWalkView view) {
111 super.onJavascriptCloseWindow(view);
118 public void onJavascriptCloseWindow(XWalkViewInternal view) {
119 if (view instanceof XWalkView) {
120 onJavascriptCloseWindow((XWalkView) view);
122 super.onJavascriptCloseWindow(view);
127 * Tell the client to toggle fullscreen mode.
128 * @param view the owner XWalkView instance.
129 * @param enterFullscreen true if it has entered fullscreen mode.
132 public void onFullscreenToggled(XWalkView view, boolean enterFullscreen) {
133 super.onFullscreenToggled(view, enterFullscreen);
140 public void onFullscreenToggled(XWalkViewInternal view, boolean enterFullscreen) {
141 if (view instanceof XWalkView) {
142 onFullscreenToggled((XWalkView) view, enterFullscreen);
144 super.onFullscreenToggled(view, enterFullscreen);
149 * Tell the client to open a file chooser.
150 * @param view the owner XWalkView instance.
151 * @param uploadFile the callback class to handle the result from caller. It MUST
152 * be invoked in all cases. Leave it not invoked will block all following
153 * requests to open file chooser.
154 * @param acceptType value of the 'accept' attribute of the input tag associated
155 * with this file picker.
156 * @param capture value of the 'capture' attribute of the input tag associated
157 * with this file picker
160 public void openFileChooser(XWalkView view, ValueCallback<Uri> uploadFile,
161 String acceptType, String capture) {
162 super.openFileChooser(view, uploadFile, acceptType, capture);
169 public void openFileChooser(XWalkViewInternal view, ValueCallback<Uri> uploadFile,
170 String acceptType, String capture) {
171 if (view instanceof XWalkView) {
172 openFileChooser((XWalkView) view, uploadFile, acceptType, capture);
174 super.openFileChooser(view, uploadFile, acceptType, capture);
179 * Notify the client that the scale applied to the XWalkView has changed.
180 * @param view the owner XWalkView instance.
181 * @param oldScale the old scale before scaling.
182 * @param newScale the current scale factor after scaling.
185 public void onScaleChanged(XWalkView view, float oldScale, float newScale) {
186 super.onScaleChanged(view, oldScale, newScale);
193 public void onScaleChanged(XWalkViewInternal view, float oldScale, float newScale) {
194 if (view instanceof XWalkView) {
195 onScaleChanged((XWalkView) view, oldScale, newScale);
197 super.onScaleChanged(view, oldScale, newScale);
202 * Give the host application a chance to handle the key event synchronously.
203 * e.g. menu shortcut key events need to be filtered this way. If return
204 * true, XWalkView will not handle the key event. If return false, XWalkView
205 * will always handle the key event, so none of the super in the view chain
206 * will see the key event. The default behavior returns false.
208 * @param view The XWalkView that is initiating the callback.
209 * @param event The key event.
210 * @return True if the host application wants to handle the key event
211 * itself, otherwise return false
215 public boolean shouldOverrideKeyEvent(XWalkView view, KeyEvent event) {
216 return super.shouldOverrideKeyEvent(view, event);
223 public boolean shouldOverrideKeyEvent(XWalkViewInternal view, KeyEvent event) {
224 if (view instanceof XWalkView) {
225 return shouldOverrideKeyEvent((XWalkView) view, event);
228 return super.shouldOverrideKeyEvent(view, event);
232 * Notify the host application that a key was not handled by the XWalkView.
233 * Except system keys, XWalkView always consumes the keys in the normal flow
234 * or if shouldOverrideKeyEvent returns true. This is called asynchronously
235 * from where the key is dispatched. It gives the host application a chance
236 * to handle the unhandled key events.
238 * @param view The XWalkView that is initiating the callback.
239 * @param event The key event.
243 public void onUnhandledKeyEvent(XWalkView view, KeyEvent event) {
244 super.onUnhandledKeyEvent(view, event);
251 public void onUnhandledKeyEvent(XWalkViewInternal view, KeyEvent event) {
252 if (view instanceof XWalkView) {
253 onUnhandledKeyEvent((XWalkView) view, event);
255 super.onUnhandledKeyEvent(view, event);
260 * Notify the host application of a change in the document title.
261 * @param view The XWalkView that initiated the callback.
262 * @param title A String containing the new title of the document.
265 public void onReceivedTitle(XWalkView view, String title) {
266 super.onReceivedTitle(view, title);
270 public void onReceivedTitle(XWalkViewInternal view, String title) {
271 if (view instanceof XWalkView) {
272 onReceivedTitle((XWalkView) view, title);
274 super.onReceivedTitle(view, title);
279 * The status when a page stopped loading
282 public enum LoadStatus {
283 /** Loading finished. */
285 /** Loading failed. */
287 /** Loading cancelled by user. */
292 * Notify the host application that a page has started loading. This method
293 * is called once for each main frame load so a page with iframes or
294 * framesets will call onPageLoadStarted one time for the main frame. This also
295 * means that onPageLoadStarted will not be called when the contents of an
296 * embedded frame changes, i.e. clicking a link whose target is an iframe.
298 * @param view The XWalkView that is initiating the callback.
299 * @param url The url to be loaded.
303 public void onPageLoadStarted(XWalkView view, String url) {
304 super.onPageLoadStarted(view, url);
311 public void onPageLoadStarted(XWalkViewInternal view, String url) {
312 if (view instanceof XWalkView) {
313 onPageLoadStarted((XWalkView) view, url);
315 super.onPageLoadStarted(view, url);
320 * Notify the host application that a page has stopped loading. This method
321 * is called only for main frame. When onPageLoadStopped() is called, the
322 * rendering picture may not be updated yet. To get the notification for the
323 * new Picture, use {@link XWalkView.PictureListener#onNewPicture}.
325 * @param view The XWalkView that is initiating the callback.
326 * @param url The url of the page.
327 * @param status the status when the page stopped loading.
331 public void onPageLoadStopped(XWalkView view, String url, LoadStatus status) {
332 LoadStatusInternal statusInternal = LoadStatusInternal.valueOf(status.toString());
333 super.onPageLoadStopped(view, url, statusInternal);
340 public void onPageLoadStopped(
341 XWalkViewInternal view, String url, LoadStatusInternal statusInternal) {
342 LoadStatus status = LoadStatus.valueOf(statusInternal.toString());
343 if (view instanceof XWalkView) {
344 onPageLoadStopped((XWalkView) view, url, status);
346 super.onPageLoadStopped(view, url, statusInternal);