Upstream version 6.35.131.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.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;
19
20 /**
21  * This class notifies the embedder UI events/callbacks.
22  */
23 public class XWalkUIClient {
24
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;
31
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;
39
40     /**
41      * Constructor.
42      * @param view the owner XWalkView instance.
43      */
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;
51         }
52         mXWalkView = view;
53         initResources();
54     }
55
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);
63     }
64
65     /**
66      * Request display and focus for this XWalkView.
67      * @param view the owner XWalkView instance.
68      */
69     public void onRequestFocus(XWalkView view) {
70     }
71
72     /**
73      * Notify the client to close the given XWalkView.
74      * @param view the owner XWalkView instance.
75      */
76     public void onJavascriptCloseWindow(XWalkView view) {
77         if (view != null && view.getActivity() != null) {
78             view.getActivity().finish();
79         }
80     }
81
82     /**
83      * The type of JavaScript modal dialog.
84      */
85     public enum JavascriptMessageType {
86         /** JavaScript alert dialog. */
87         JAVASCRIPT_ALERT,
88         /** JavaScript confirm dialog. */
89         JAVASCRIPT_CONFIRM,
90         /** JavaScript prompt dialog. */
91         JAVASCRIPT_PROMPT,
92         /** JavaScript dialog for a window-before-unload notification. */
93         JAVASCRIPT_BEFOREUNLOAD
94     }
95
96     /**
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.
104      */
105     public boolean onJavascriptModalDialog(XWalkView view, JavascriptMessageType type, String url,
106             String message, String defaultValue, XWalkJavascriptResult result) {
107         switch(type) {
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);
117             default:
118                 break;
119         }
120         assert(false);
121         return false;
122     }
123
124     /**
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.
128      */
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);
141             } else {
142                 if ((activity.getWindow().getAttributes().flags &
143                         WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0) {
144                     mOriginalFullscreen = true;
145                 } else {
146                     mOriginalFullscreen = false;
147                 }
148                 if (!mOriginalFullscreen) {
149                     activity.getWindow().setFlags(
150                             WindowManager.LayoutParams.FLAG_FULLSCREEN,
151                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
152                 }
153             }
154         } else {
155             if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
156                 mDecorView.setSystemUiVisibility(mSystemUiFlag);
157             } else {
158                 // Clear the activity fullscreen flag.
159                 if (!mOriginalFullscreen) {
160                     activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
161                 }
162             }
163         }
164     }
165
166     /**
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
176      */
177     public void openFileChooser(XWalkView view, ValueCallback<Uri> uploadFile,
178             String acceptType, String capture) {
179         uploadFile.onReceiveValue(null);
180     }
181
182     /**
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.
187      */
188     public void onScaleChanged(XWalkView view, float oldScale, float newScale) {
189     }
190
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)
196                 .setMessage(message)
197                 .setCancelable(true)
198                 .setPositiveButton(mOKButton, new DialogInterface.OnClickListener() {
199                     @Override
200                     public void onClick(DialogInterface dialog, int which) {
201                         fResult.confirm();
202                         dialog.dismiss();
203                     }
204                 }).setOnKeyListener(new DialogInterface.OnKeyListener() {
205                     @Override
206                     public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
207                         if (keyCode == KeyEvent.KEYCODE_BACK) {
208                             fResult.confirm();
209                             return false;
210                         } else {
211                             return true;
212                         }
213                     }
214                 });
215         mDialog = dialogBuilder.create();
216         mDialog.show();
217         return false;
218     }
219
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)
225                 .setMessage(message)
226                 .setCancelable(true)
227                 .setPositiveButton(mOKButton, new DialogInterface.OnClickListener() {
228                     @Override
229                     public void onClick(DialogInterface dialog, int which) {
230                         fResult.confirm();
231                         dialog.dismiss();
232                     }
233                 })
234                 .setNegativeButton(mCancelButton, null)
235                 .setOnCancelListener(new DialogInterface.OnCancelListener() {
236                     @Override
237                     public void onCancel(DialogInterface dialog) {
238                         fResult.cancel();
239                     }
240                 }).setOnKeyListener(new DialogInterface.OnKeyListener() {
241                     @Override
242                     public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
243                         if (keyCode == KeyEvent.KEYCODE_BACK) {
244                             fResult.confirm();
245                             return false;
246                         } else {
247                             return true;
248                         }
249                     }
250                 });
251         mDialog = dialogBuilder.create();
252         mDialog.show();
253         return false;
254     }
255
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)
261                 .setMessage(message)
262                 .setPositiveButton(mOKButton, new DialogInterface.OnClickListener() {
263                     @Override
264                     public void onClick(DialogInterface dialog, int which) {
265                         fResult.confirmWithResult(mPromptText.getText().toString());
266                         dialog.dismiss();
267                     }
268                 })
269                 .setNegativeButton(mCancelButton, null)
270                 .setOnCancelListener(new DialogInterface.OnCancelListener() {
271                     @Override
272                     public void onCancel(DialogInterface dialog) {
273                         fResult.cancel();
274                     }
275                 });
276         mPromptText = new EditText(mContext);
277         mPromptText.setVisibility(View.VISIBLE);
278         mPromptText.setText(defaultValue);
279         mPromptText.selectAll();
280
281         dialogBuilder.setView(mPromptText);
282         mDialog = dialogBuilder.create();
283         mDialog.show();
284         return false;
285     }
286 }