Upstream version 9.37.193.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkUIClient.java
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.
4
5 package org.xwalk.core;
6
7 import android.net.Uri;
8 import android.os.Message;
9 import android.view.KeyEvent;
10 import android.webkit.ValueCallback;
11
12 import org.xwalk.core.internal.XWalkJavascriptResultInternal;
13 import org.xwalk.core.internal.XWalkUIClientInternal;
14 import org.xwalk.core.internal.XWalkViewInternal;
15
16 /**
17  * This class notifies the embedder UI events/callbacks.
18  */
19 public class XWalkUIClient extends XWalkUIClientInternal {
20
21     /**
22      * Constructor.
23      * @param view the owner XWalkView instance.
24      * @since 1.0
25      */
26     public XWalkUIClient(XWalkView view) {
27         super(view);
28     }
29
30     /**
31      * The type of JavaScript modal dialog.
32      * @since 1.0
33      */
34     public enum JavascriptMessageType {
35         /** JavaScript alert dialog. */
36         JAVASCRIPT_ALERT,
37         /** JavaScript confirm dialog. */
38         JAVASCRIPT_CONFIRM,
39         /** JavaScript prompt dialog. */
40         JAVASCRIPT_PROMPT,
41         /** JavaScript dialog for a window-before-unload notification. */
42         JAVASCRIPT_BEFOREUNLOAD
43     }
44
45     /**
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.
53      * @since 1.0
54      */
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);
63     }
64
65     /**
66      * @hide
67      */
68     @Override
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(
75                     (XWalkView) view,
76                     type, url, message, defaultValue,
77                     new XWalkJavascriptResultHandler(result));
78         }
79
80         return super.onJavascriptModalDialog(
81                 view, typeInternal, url, message, defaultValue, result);
82     }
83
84     /**
85      * Request display and focus for this XWalkView.
86      * @param view the owner XWalkView instance.
87      * @since 1.0
88      */
89     public void onRequestFocus(XWalkView view) {
90         super.onRequestFocus(view);
91     }
92
93     /**
94      * @hide
95      */
96     @Override
97     public void onRequestFocus(XWalkViewInternal view) {
98         if (view instanceof XWalkView) {
99             onRequestFocus((XWalkView) view);
100         } else {
101             super.onRequestFocus(view);
102         }
103     }
104
105     /**
106      * Notify the client to close the given XWalkView.
107      * @param view the owner XWalkView instance.
108      * @since 1.0
109      */
110     public void onJavascriptCloseWindow(XWalkView view) {
111         super.onJavascriptCloseWindow(view);
112     }
113
114     /**
115      * @hide
116      */
117     @Override
118     public void onJavascriptCloseWindow(XWalkViewInternal view) {
119         if (view instanceof XWalkView) {
120             onJavascriptCloseWindow((XWalkView) view);
121         } else {
122             super.onJavascriptCloseWindow(view);
123         }
124     }
125
126     /**
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.
130      * @since 1.0
131      */
132     public void onFullscreenToggled(XWalkView view, boolean enterFullscreen) {
133         super.onFullscreenToggled(view, enterFullscreen);
134     }
135
136     /**
137      * @hide
138      */
139     @Override
140     public void onFullscreenToggled(XWalkViewInternal view, boolean enterFullscreen) {
141         if (view instanceof XWalkView) {
142             onFullscreenToggled((XWalkView) view, enterFullscreen);
143         } else {
144             super.onFullscreenToggled(view, enterFullscreen);
145         }
146     }
147
148     /**
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
158      * @since 1.0
159      */
160     public void openFileChooser(XWalkView view, ValueCallback<Uri> uploadFile,
161             String acceptType, String capture) {
162         super.openFileChooser(view, uploadFile, acceptType, capture);
163     }
164
165     /**
166      * @hide
167      */
168     @Override
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);
173         } else {
174             super.openFileChooser(view, uploadFile, acceptType, capture);
175         }
176     }
177
178     /**
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.
183      * @since 1.0
184      */
185     public void onScaleChanged(XWalkView view, float oldScale, float newScale) {
186         super.onScaleChanged(view, oldScale, newScale);
187     }
188
189     /**
190      * @hide
191      */
192     @Override
193     public void onScaleChanged(XWalkViewInternal view, float oldScale, float newScale) {
194         if (view instanceof XWalkView) {
195             onScaleChanged((XWalkView) view, oldScale, newScale);
196         } else {
197             super.onScaleChanged(view, oldScale, newScale);
198         }
199     }
200
201     /**
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.
207      *
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
212      *
213      * @since 2.1
214      */
215     public boolean shouldOverrideKeyEvent(XWalkView view, KeyEvent event) {
216         return super.shouldOverrideKeyEvent(view, event);
217     }
218
219     /**
220      * @hide
221      */
222     @Override
223     public boolean shouldOverrideKeyEvent(XWalkViewInternal view, KeyEvent event) {
224         if (view instanceof XWalkView) {
225             return shouldOverrideKeyEvent((XWalkView) view, event);
226         }
227
228         return super.shouldOverrideKeyEvent(view, event);
229     }
230
231     /**
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.
237      *
238      * @param view The XWalkView that is initiating the callback.
239      * @param event The key event.
240      *
241      * @since 2.1
242      */
243     public void onUnhandledKeyEvent(XWalkView view, KeyEvent event) {
244         super.onUnhandledKeyEvent(view, event);
245     }
246
247     /**
248      * @hide
249      */
250     @Override
251     public void onUnhandledKeyEvent(XWalkViewInternal view, KeyEvent event) {
252         if (view instanceof XWalkView) {
253             onUnhandledKeyEvent((XWalkView) view, event);
254         } else {
255             super.onUnhandledKeyEvent(view, event);
256         }
257     }
258
259     /**
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.
263      * @since 2.1
264      */
265     public void onReceivedTitle(XWalkView view, String title) {
266         super.onReceivedTitle(view, title);
267     }
268
269     @Override
270     public void onReceivedTitle(XWalkViewInternal view, String title) {
271         if (view instanceof XWalkView) {
272             onReceivedTitle((XWalkView) view, title);
273         } else {
274             super.onReceivedTitle(view, title);
275         }
276     }
277
278     /**
279      * The status when a page stopped loading
280      * @since 2.1
281      */
282     public enum LoadStatus {
283         /** Loading finished. */
284         FINISHED,
285         /** Loading failed. */
286         FAILED,
287         /** Loading cancelled by user. */
288         CANCELLED
289     }
290
291     /**
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.
297      *
298      * @param view The XWalkView that is initiating the callback.
299      * @param url The url to be loaded.
300      *
301      * @since 2.1
302      */
303     public void onPageLoadStarted(XWalkView view, String url) {
304         super.onPageLoadStarted(view, url);
305     }
306
307     /**
308      * @hide
309      */
310     @Override
311     public void onPageLoadStarted(XWalkViewInternal view, String url) {
312         if (view instanceof XWalkView) {
313             onPageLoadStarted((XWalkView) view, url);
314         } else {
315             super.onPageLoadStarted(view, url);
316         }
317     }
318
319     /**
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}.
324      *
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.
328      *
329      * @since 2.1
330      */
331     public void onPageLoadStopped(XWalkView view, String url, LoadStatus status) {
332         LoadStatusInternal statusInternal = LoadStatusInternal.valueOf(status.toString());
333         super.onPageLoadStopped(view, url, statusInternal);
334     }
335
336     /**
337      * @hide
338      */
339     @Override
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);
345         } else {
346             super.onPageLoadStopped(view, url, statusInternal);
347         }
348     }
349 }