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.content.Context;
9 import android.content.Intent;
10 import android.os.Bundle;
11 import android.util.AttributeSet;
12 import android.webkit.ValueCallback;
14 import org.xwalk.core.internal.XWalkNavigationHistoryInternal;
15 import org.xwalk.core.internal.XWalkPreferencesInternal;
16 import org.xwalk.core.internal.XWalkResourceClientInternal;
17 import org.xwalk.core.internal.XWalkUIClientInternal;
18 import org.xwalk.core.internal.XWalkViewInternal;
21 * <p>XWalkView represents an Android view for web apps/pages. Thus most of attributes
22 * for Android view are valid for this class. Since it internally uses
23 * <a href="http://developer.android.com/reference/android/view/SurfaceView.html">
24 * android.view.SurfaceView</a> for rendering web pages by default, it can't be resized,
25 * rotated, transformed and animated due to the limitations of SurfaceView.
26 * Alternatively, if the preference key {@link XWalkPreferences#ANIMATABLE_XWALK_VIEW}
27 * is set to True, XWalkView can be transformed and animated because
28 * <a href="http://developer.android.com/reference/android/view/TextureView.html">
29 * TextureView</a> is intentionally used to render web pages for animation support.
30 * Besides, XWalkView won't be rendered if it's invisible.</p>
32 * <p>XWalkView needs hardware acceleration to render web pages. As a result, the
33 * AndroidManifest.xml of the caller's app must be appended with the attribute
34 * "android:hardwareAccelerated" and its value must be set as "true".</p>
36 * <application android:name="android.app.Application" android:label="XWalkUsers"
37 * android:hardwareAccelerated="true">
40 * <p>Crosswalk provides 2 major callback classes, namely {@link XWalkResourceClient} and
41 * {@link XWalkUIClient} for listening to the events related to resource loading and UI.
42 * By default, Crosswalk has a default implementation. Callers can override them if needed.</p>
44 * <p>Unlike other Android views, this class has to listen to system events like intents and activity result.
45 * The web engine inside this view need to get and handle them.
46 * With contianer activity's lifecycle change, XWalkView will pause all timers and other
47 * components like videos when activity paused, resume back them when activity resumed.
48 * When activity is about to destroy, XWalkView will destroy itself as well.
49 * Embedders can also call onHide() and pauseTimers() to explicitly pause XWalkView.
50 * Similarily with onShow(), resumeTimers() and onDestroy().
55 * import android.app.Activity;
56 * import android.os.Bundle;
58 * import org.xwalk.core.XWalkResourceClient;
59 * import org.xwalk.core.XWalkUIClient;
60 * import org.xwalk.core.XWalkView;
62 * public class MyActivity extends Activity {
63 * XWalkView mXwalkView;
65 * class MyResourceClient extends XWalkResourceClient {
66 * MyResourceClient(XWalkView view) {
71 * WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String url) {
77 * class MyUIClient extends XWalkUIClient {
78 * MyUIClient(XWalkView view) {
83 * void onFullscreenToggled(XWalkView view, String url) {
90 * protected void onCreate(Bundle savedInstanceState) {
91 * mXwalkView = new XWalkView(this, null);
92 * setContentView(mXwalkView);
93 * mXwalkView.setResourceClient(new MyResourceClient(mXwalkView));
94 * mXwalkView.setUIClient(new MyUIClient(mXwalkView));
95 * mXwalkView.load("http://www.crosswalk-project.org", null);
99 * protected void onPause() {
101 * if (mXwalkView != null) {
102 * mXwalkView.pauseTimers();
103 * mXwalkView.onHide();
108 * protected void onResume() {
110 * if (mXwalkView != null) {
111 * mXwalkView.resumeTimers();
112 * mXwalkView.onShow();
117 * protected void onDestroy() {
119 * if (mXwalkView != null) {
120 * mXwalkView.onDestroy();
125 * protected void onActivityResult(int requestCode, int resultCode, Intent data) {
126 * if (mXwalkView != null) {
127 * mXwalkView.onActivityResult(requestCode, resultCode, data);
132 * protected void onNewIntent(Intent intent) {
133 * if (mXwalkView != null) {
134 * mXwalkView.onNewIntent(intent);
140 public class XWalkView extends XWalkViewInternal {
143 * Normal reload mode as default.
146 public static final int RELOAD_NORMAL = 0;
148 * Reload mode with bypassing the cache.
151 public static final int RELOAD_IGNORE_CACHE = 1;
154 * Constructor for inflating via XML.
155 * @param context a Context object used to access application assets.
156 * @param attrs an AttributeSet passed to our parent.
159 public XWalkView(Context context, AttributeSet attrs) {
160 super(context, attrs);
164 * Constructor for Crosswalk runtime. In shared mode, context isi
165 * different from activity. In embedded mode, they're same.
166 * @param context a Context object used to access application assets
167 * @param activity the activity for this XWalkView.
170 public XWalkView(Context context, Activity activity) {
171 super(context, activity);
175 * Load a web page/app from a given base URL or a content.
176 * If url is null or empty and content is null or empty, then this function
178 * If content is not null, load the web page/app from the content.
179 * If content is not null and the url is not set, return "about:blank" ifi
180 * calling {@link XWalkView#getUrl()}.
181 * If content is null, try to load the content from the url.
183 * It supports URL schemes like 'http:', 'https:' and 'file:'.
184 * It can also load files from Android assets, e.g. 'file:///android_asset/'.
185 * @param url the url for web page/app.
186 * @param content the content for the web page/app. Could be empty.
189 public void load(String url, String content) {
190 super.load(url, content);
194 * Load a web app from a given manifest.json file. If content is not null,
195 * load the manifest.json from the content. If content is null, try to load
196 * the manifest.json from the url. Note that url should not be null if the
197 * launched path defined in manifest.json is relative.
199 * It supports URL schemes like 'http:', 'https:' and 'file:'.
200 * It can also load files from Android assets, e.g. 'file:///android_asset/'.
201 * @param url the url for manifest.json.
202 * @param content the content for manifest.json.
205 public void loadAppFromManifest(String url, String content) {
206 super.loadAppFromManifest(url, content);
210 * Reload a web app with a given mode.
211 * @param mode the reload mode.
214 public void reload(int mode) {
219 * Stop current loading progress.
222 public void stopLoading() {
227 * Get the url of current web page/app. This may be different from what's passed
229 * @return the url for current web page/app.
232 public String getUrl() {
233 return super.getUrl();
237 * Get the title of current web page/app. This may be different from what's passed
239 * @return the title for current web page/app.
242 public String getTitle() {
243 return super.getTitle();
247 * Get the original url specified by caller.
248 * @return the original url.
251 public String getOriginalUrl() {
252 return super.getOriginalUrl();
256 * Get the navigation history for current XWalkView. It's synchronized with
257 * this XWalkView if any backward/forward and navigation operations.
258 * @return the navigation history.
261 public XWalkNavigationHistory getNavigationHistory() {
262 XWalkNavigationHistoryInternal history = super.getNavigationHistory();
263 if (history == null || history instanceof XWalkNavigationHistory) {
264 return (XWalkNavigationHistory) history;
267 return new XWalkNavigationHistory(history);
271 * Injects the supplied Java object into this XWalkView.
272 * Each method defined in the class of the object should be
273 * marked with {@link JavascriptInterface} if it's called by JavaScript.
274 * @param object the supplied Java object, called by JavaScript.
275 * @param name the name injected in JavaScript.
278 public void addJavascriptInterface(Object object, String name) {
279 super.addJavascriptInterface(object, name);
283 * Evaluate a fragment of JavaScript code and get the result via callback.
284 * @param script the JavaScript string.
285 * @param callback the callback to handle the evaluated result.
288 public void evaluateJavascript(String script, ValueCallback<String> callback) {
289 super.evaluateJavascript(script, callback);
293 * Clear the resource cache. Note that the cache is per-application, so this
294 * will clear the cache for all XWalkViews used.
295 * @param includeDiskFiles indicate whether to clear disk files for cache.
298 public void clearCache(boolean includeDiskFiles) {
299 super.clearCache(includeDiskFiles);
303 * Indicate that a HTML element is occupying the whole screen.
304 * @return true if any HTML element is occupying the whole screen.
307 public boolean hasEnteredFullscreen() {
308 return super.hasEnteredFullscreen();
312 * Leave fullscreen mode if it's. Do nothing if it's not
316 public void leaveFullscreen() {
317 super.leaveFullscreen();
321 * Pause all layout, parsing and JavaScript timers for all XWalkView instances.
322 * It will be called when the container Activity get paused. It can also be explicitly
323 * called to pause timers.
325 * Note that it will globally impact all XWalkView instances, not limited to
326 * just this XWalkView.
330 public void pauseTimers() {
335 * Resume all layout, parsing and JavaScript timers for all XWalkView instances.
336 * It will be called when the container Activity get resumed. It can also be explicitly
337 * called to resume timers.
339 * Note that it will globally impact all XWalkView instances, not limited to
340 * just this XWalkView.
344 public void resumeTimers() {
345 super.resumeTimers();
349 * Pause many other things except JavaScript timers inside rendering engine,
350 * like video player, modal dialogs, etc. See {@link #pauseTimers} about pausing
352 * It will be called when the container Activity get paused. It can also be explicitly
353 * called to pause above things.
356 public void onHide() {
361 * Resume video player, modal dialogs. Embedders are in charge of calling
362 * this during resuming this activity if they call onHide.
363 * It will be called when the container Activity get resumed. It can also be explicitly
364 * called to resume above things.
367 public void onShow() {
372 * Release internal resources occupied by this XWalkView.
373 * It will be called when the container Activity get destroyed. It can also be explicitly
374 * called to release resources.
377 public void onDestroy() {
382 * Pass through activity result to XWalkView. Many internal facilities need this
383 * to handle activity result like JavaScript dialog, Crosswalk extensions, etc.
384 * See <a href="http://developer.android.com/reference/android/app/Activity.html">
385 * android.app.Activity.onActivityResult()</a>.
386 * @param requestCode passed from android.app.Activity.onActivityResult().
387 * @param resultCode passed from android.app.Activity.onActivityResult().
388 * @param data passed from android.app.Activity.onActivityResult().
391 public void onActivityResult(int requestCode, int resultCode, Intent data) {
392 super.onActivityResult(requestCode, resultCode, data);
396 * Pass through intents to XWalkView. Many internal facilities need this
397 * to receive the intents like web notification. See
398 * <a href="http://developer.android.com/reference/android/app/Activity.html">
399 * android.app.Activity.onNewIntent()</a>.
400 * @param intent passed from android.app.Activity.onNewIntent().
403 public boolean onNewIntent(Intent intent) {
404 return super.onNewIntent(intent);
408 * Save current internal state of this XWalkView. This can help restore this state
409 * afterwards restoring.
410 * @param outState the saved state for restoring.
413 public boolean saveState(Bundle outState) {
414 return super.saveState(outState);
418 * Restore the state from the saved bundle data.
419 * @param inState the state saved from saveState().
420 * @return true if it can restore the state.
423 public boolean restoreState(Bundle inState) {
424 return super.restoreState(inState);
428 * Get the API version of Crosswalk embedding API.
429 * @return the string of API level.
432 // TODO(yongsheng): make it static?
433 public String getAPIVersion() {
434 return super.getAPIVersion();
438 * Get the Crosswalk version.
439 * @return the string of Crosswalk.
442 // TODO(yongsheng): make it static?
443 public String getXWalkVersion() {
444 return super.getXWalkVersion();
448 * Embedders use this to customize their handlers to events/callbacks related
450 * @param client the XWalkUIClient defined by callers.
453 public void setUIClient(XWalkUIClient client) {
454 super.setUIClient(client);
458 * Embedders use this to customize their handlers to events/callbacks related
459 * to resource loading.
460 * @param client the XWalkResourceClient defined by callers.
463 public void setResourceClient(XWalkResourceClient client) {
464 super.setResourceClient(client);