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.app.Activity;
8 import android.app.AlertDialog;
9 import android.content.Context;
10 import android.content.DialogInterface;
11 import android.net.Uri;
12 import android.os.Build.VERSION;
13 import android.os.Build.VERSION_CODES;
14 import android.view.KeyEvent;
15 import android.view.View;
16 import android.view.WindowManager;
17 import android.webkit.ValueCallback;
18 import android.widget.EditText;
21 * This class notifies the embedder UI events/callbacks.
23 public class XWalkUIClient {
25 // Strings for displaying Dialog.
26 private static String mJSAlertTitle;
27 private static String mJSConfirmTitle;
28 private static String mJSPromptTitle;
29 private static String mOKButton;
30 private static String mCancelButton;
32 private Context mContext;
33 private AlertDialog mDialog;
34 private EditText mPromptText;
35 private int mSystemUiFlag;
36 private View mDecorView;
37 private XWalkView mXWalkView;
38 private boolean mOriginalFullscreen;
42 * @param view the owner XWalkView instance.
44 public XWalkUIClient(XWalkView view) {
45 mContext = view.getContext();
46 mDecorView = view.getActivity().getWindow().getDecorView();
47 if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
48 mSystemUiFlag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
49 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
50 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
56 private void initResources() {
57 if (mJSAlertTitle != null) return;
58 mJSAlertTitle = mContext.getString(R.string.js_alert_title);
59 mJSConfirmTitle = mContext.getString(R.string.js_confirm_title);
60 mJSPromptTitle = mContext.getString(R.string.js_prompt_title);
61 mOKButton = mContext.getString(android.R.string.ok);
62 mCancelButton = mContext.getString(android.R.string.cancel);
66 * Request display and focus for this XWalkView.
67 * @param view the owner XWalkView instance.
69 public void onRequestFocus(XWalkView view) {
73 * Notify the client to close the given XWalkView.
74 * @param view the owner XWalkView instance.
76 public void onJavascriptCloseWindow(XWalkView view) {
77 if (view != null && view.getActivity() != null) {
78 view.getActivity().finish();
83 * The type of JavaScript modal dialog.
85 public enum JavascriptMessageType {
86 /** JavaScript alert dialog. */
88 /** JavaScript confirm dialog. */
90 /** JavaScript prompt dialog. */
92 /** JavaScript dialog for a window-before-unload notification. */
93 JAVASCRIPT_BEFOREUNLOAD
97 * Tell the client to display a prompt dialog to the user.
98 * @param view the owner XWalkView instance.
99 * @param type the type of JavaScript modal dialog.
100 * @param url the url of the web page which wants to show this dialog.
101 * @param message the message to be shown.
102 * @param defaultValue the default value string. Only valid for Prompt dialog.
103 * @param result the callback to handle the result from caller.
105 public boolean onJavascriptModalDialog(XWalkView view, JavascriptMessageType type, String url,
106 String message, String defaultValue, XWalkJavascriptResult result) {
108 case JAVASCRIPT_ALERT:
109 return onJsAlert(view, url, message, result);
110 case JAVASCRIPT_CONFIRM:
111 return onJsConfirm(view, url, message, result);
112 case JAVASCRIPT_PROMPT:
113 return onJsPrompt(view, url, message, defaultValue, result);
114 case JAVASCRIPT_BEFOREUNLOAD:
115 // Reuse onJsConfirm to show the dialog.
116 return onJsConfirm(view, url, message, result);
125 * Tell the client to toggle fullscreen mode.
126 * @param view the owner XWalkView instance.
127 * @param enterFullscreen true if it has entered fullscreen mode.
129 public void onFullscreenToggled(XWalkView view, boolean enterFullscreen) {
130 Activity activity = view.getActivity();
131 if (enterFullscreen) {
132 if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
133 mSystemUiFlag = mDecorView.getSystemUiVisibility();
134 mDecorView.setSystemUiVisibility(
135 View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
136 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
137 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
138 View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
139 View.SYSTEM_UI_FLAG_FULLSCREEN |
140 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
142 if ((activity.getWindow().getAttributes().flags &
143 WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0) {
144 mOriginalFullscreen = true;
146 mOriginalFullscreen = false;
148 if (!mOriginalFullscreen) {
149 activity.getWindow().setFlags(
150 WindowManager.LayoutParams.FLAG_FULLSCREEN,
151 WindowManager.LayoutParams.FLAG_FULLSCREEN);
155 if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
156 mDecorView.setSystemUiVisibility(mSystemUiFlag);
158 // Clear the activity fullscreen flag.
159 if (!mOriginalFullscreen) {
160 activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
167 * Tell the client to open a file chooser.
168 * @param view the owner XWalkView instance.
169 * @param uploadFile the callback class to handle the result from caller. It MUST
170 * be invoked in all cases. Leave it not invoked will block all following
171 * requests to open file chooser.
172 * @param acceptType value of the 'accept' attribute of the input tag associated
173 * with this file picker.
174 * @param capture value of the 'capture' attribute of the input tag associated
175 * with this file picker
177 public void openFileChooser(XWalkView view, ValueCallback<Uri> uploadFile,
178 String acceptType, String capture) {
179 uploadFile.onReceiveValue(null);
183 * Notify the client that the scale applied to the XWalkView has changed.
184 * @param view the owner XWalkView instance.
185 * @param oldScale the old scale before scaling.
186 * @param newScale the current scale factor after scaling.
188 public void onScaleChanged(XWalkView view, float oldScale, float newScale) {
191 private boolean onJsAlert(XWalkView view, String url, String message,
192 XWalkJavascriptResult result) {
193 final XWalkJavascriptResult fResult = result;
194 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
195 dialogBuilder.setTitle(mJSAlertTitle)
198 .setPositiveButton(mOKButton, new DialogInterface.OnClickListener() {
200 public void onClick(DialogInterface dialog, int which) {
204 }).setOnKeyListener(new DialogInterface.OnKeyListener() {
206 public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
207 if (keyCode == KeyEvent.KEYCODE_BACK) {
215 mDialog = dialogBuilder.create();
220 private boolean onJsConfirm(XWalkView view, String url, String message,
221 XWalkJavascriptResult result) {
222 final XWalkJavascriptResult fResult = result;
223 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
224 dialogBuilder.setTitle(mJSConfirmTitle)
227 .setPositiveButton(mOKButton, new DialogInterface.OnClickListener() {
229 public void onClick(DialogInterface dialog, int which) {
234 .setNegativeButton(mCancelButton, null)
235 .setOnCancelListener(new DialogInterface.OnCancelListener() {
237 public void onCancel(DialogInterface dialog) {
240 }).setOnKeyListener(new DialogInterface.OnKeyListener() {
242 public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
243 if (keyCode == KeyEvent.KEYCODE_BACK) {
251 mDialog = dialogBuilder.create();
256 private boolean onJsPrompt(XWalkView view, String url, String message,
257 String defaultValue, XWalkJavascriptResult result) {
258 final XWalkJavascriptResult fResult = result;
259 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
260 dialogBuilder.setTitle(mJSPromptTitle)
262 .setPositiveButton(mOKButton, new DialogInterface.OnClickListener() {
264 public void onClick(DialogInterface dialog, int which) {
265 fResult.confirmWithResult(mPromptText.getText().toString());
269 .setNegativeButton(mCancelButton, null)
270 .setOnCancelListener(new DialogInterface.OnCancelListener() {
272 public void onCancel(DialogInterface dialog) {
276 mPromptText = new EditText(mContext);
277 mPromptText.setVisibility(View.VISIBLE);
278 mPromptText.setText(defaultValue);
279 mPromptText.selectAll();
281 dialogBuilder.setView(mPromptText);
282 mDialog = dialogBuilder.create();