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 application life
45 * cycle, intents, and activity result. The web engine inside this view need to get and handle
46 * them. And the onDestroy() method of XWalkView MUST be called explicitly when an XWalkView
47 * won't be used anymore, otherwise it will cause the memory leak from the native side of the web
48 * engine. It's similar to the
49 * <a href="http://developer.android.com/reference/android/webkit/WebView.html#destroy()">
50 * destroy()</a> method of Android WebView. For example:</p>
53 * import android.app.Activity;
54 * import android.os.Bundle;
56 * import org.xwalk.core.XWalkResourceClient;
57 * import org.xwalk.core.XWalkUIClient;
58 * import org.xwalk.core.XWalkView;
60 * public class MyActivity extends Activity {
61 * XWalkView mXwalkView;
63 * class MyResourceClient extends XWalkResourceClient {
64 * MyResourceClient(XWalkView view) {
69 * WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String url) {
75 * class MyUIClient extends XWalkUIClient {
76 * MyUIClient(XWalkView view) {
81 * void onFullscreenToggled(XWalkView view, String url) {
88 * protected void onCreate(Bundle savedInstanceState) {
89 * mXwalkView = new XWalkView(this, null);
90 * setContentView(mXwalkView);
91 * mXwalkView.setResourceClient(new MyResourceClient(mXwalkView));
92 * mXwalkView.setUIClient(new MyUIClient(mXwalkView));
93 * mXwalkView.load("http://www.crosswalk-project.org", null);
97 * protected void onPause() {
99 * if (mXwalkView != null) {
100 * mXwalkView.pauseTimers();
101 * mXwalkView.onHide();
106 * protected void onResume() {
108 * if (mXwalkView != null) {
109 * mXwalkView.resumeTimers();
110 * mXwalkView.onShow();
115 * protected void onDestroy() {
117 * if (mXwalkView != null) {
118 * mXwalkView.onDestroy();
123 * protected void onActivityResult(int requestCode, int resultCode, Intent data) {
124 * if (mXwalkView != null) {
125 * mXwalkView.onActivityResult(requestCode, resultCode, data);
130 * protected void onNewIntent(Intent intent) {
131 * if (mXwalkView != null) {
132 * mXwalkView.onNewIntent(intent);
138 public class XWalkView extends XWalkViewInternal {
141 * Normal reload mode as default.
144 public static final int RELOAD_NORMAL = 0;
146 * Reload mode with bypassing the cache.
149 public static final int RELOAD_IGNORE_CACHE = 1;
152 * Constructor for inflating via XML.
153 * @param context a Context object used to access application assets.
154 * @param attrs an AttributeSet passed to our parent.
157 public XWalkView(Context context, AttributeSet attrs) {
158 super(context, attrs);
162 * Constructor for Crosswalk runtime. In shared mode, context isi
163 * different from activity. In embedded mode, they're same.
164 * @param context a Context object used to access application assets
165 * @param activity the activity for this XWalkView.
168 public XWalkView(Context context, Activity activity) {
169 super(context, activity);
173 * Load a web page/app from a given base URL or a content.
174 * If url is null or empty and content is null or empty, then this function
176 * If content is not null, load the web page/app from the content.
177 * If content is not null and the url is not set, return "about:blank" ifi
178 * calling {@link XWalkView#getUrl()}.
179 * If content is null, try to load the content from the url.
181 * It supports URL schemes like 'http:', 'https:' and 'file:'.
182 * It can also load files from Android assets, e.g. 'file:///android_asset/'.
183 * @param url the url for web page/app.
184 * @param content the content for the web page/app. Could be empty.
187 public void load(String url, String content) {
188 super.load(url, content);
192 * Load a web app from a given manifest.json file. If content is not null,
193 * load the manifest.json from the content. If content is null, try to load
194 * the manifest.json from the url. Note that url should not be null if the
195 * launched path defined in manifest.json is relative.
197 * It supports URL schemes like 'http:', 'https:' and 'file:'.
198 * It can also load files from Android assets, e.g. 'file:///android_asset/'.
199 * @param url the url for manifest.json.
200 * @param content the content for manifest.json.
203 public void loadAppFromManifest(String url, String content) {
204 super.loadAppFromManifest(url, content);
208 * Reload a web app with a given mode.
209 * @param mode the reload mode.
212 public void reload(int mode) {
217 * Stop current loading progress.
220 public void stopLoading() {
225 * Get the url of current web page/app. This may be different from what's passed
227 * @return the url for current web page/app.
230 public String getUrl() {
231 return super.getUrl();
235 * Get the title of current web page/app. This may be different from what's passed
237 * @return the title for current web page/app.
240 public String getTitle() {
241 return super.getTitle();
245 * Get the original url specified by caller.
246 * @return the original url.
249 public String getOriginalUrl() {
250 return super.getOriginalUrl();
254 * Get the navigation history for current XWalkView. It's synchronized with
255 * this XWalkView if any backward/forward and navigation operations.
256 * @return the navigation history.
259 public XWalkNavigationHistory getNavigationHistory() {
260 XWalkNavigationHistoryInternal history = super.getNavigationHistory();
261 if (history == null || history instanceof XWalkNavigationHistory) {
262 return (XWalkNavigationHistory) history;
265 return new XWalkNavigationHistory(history);
269 * Injects the supplied Java object into this XWalkView.
270 * Each method defined in the class of the object should be
271 * marked with {@link JavascriptInterface} if it's called by JavaScript.
272 * @param object the supplied Java object, called by JavaScript.
273 * @param name the name injected in JavaScript.
276 public void addJavascriptInterface(Object object, String name) {
277 super.addJavascriptInterface(object, name);
281 * Evaluate a fragment of JavaScript code and get the result via callback.
282 * @param script the JavaScript string.
283 * @param callback the callback to handle the evaluated result.
286 public void evaluateJavascript(String script, ValueCallback<String> callback) {
287 super.evaluateJavascript(script, callback);
291 * Clear the resource cache. Note that the cache is per-application, so this
292 * will clear the cache for all XWalkViews used.
293 * @param includeDiskFiles indicate whether to clear disk files for cache.
296 public void clearCache(boolean includeDiskFiles) {
297 super.clearCache(includeDiskFiles);
301 * Indicate that a HTML element is occupying the whole screen.
302 * @return true if any HTML element is occupying the whole screen.
305 public boolean hasEnteredFullscreen() {
306 return super.hasEnteredFullscreen();
310 * Leave fullscreen mode if it's. Do nothing if it's not
314 public void leaveFullscreen() {
315 super.leaveFullscreen();
319 * Pause all layout, parsing and JavaScript timers for all XWalkView instances.
320 * Typically it should be called when the activity for this view is paused,
321 * and accordingly {@link #resumeTimers} should be called when the activity
324 * Note that it will globally impact all XWalkView instances, not limited to
325 * just this XWalkView.
329 public void pauseTimers() {
334 * Resume all layout, parsing and JavaScript timers for all XWalkView instances.
335 * Typically it should be called when the activity for this view is resumed.
337 * Note that it will globally impact all XWalkView instances, not limited to
338 * just this XWalkView.
342 public void resumeTimers() {
343 super.resumeTimers();
347 * Pause many other things except JavaScript timers inside rendering engine,
348 * like video player, modal dialogs, etc. See {@link #pauseTimers} about pausing
350 * Typically it should be called when the activity for this view is paused.
353 public void onHide() {
358 * Resume video player, modal dialogs. Embedders are in charge of calling
359 * this during resuming this activity if they call onHide.
360 * Typically it should be called when the activity for this view is resumed.
363 public void onShow() {
368 * Release internal resources occupied by this XWalkView.
371 public void onDestroy() {
376 * Pass through activity result to XWalkView. Many internal facilities need this
377 * to handle activity result like JavaScript dialog, Crosswalk extensions, etc.
378 * See <a href="http://developer.android.com/reference/android/app/Activity.html">
379 * android.app.Activity.onActivityResult()</a>.
380 * @param requestCode passed from android.app.Activity.onActivityResult().
381 * @param resultCode passed from android.app.Activity.onActivityResult().
382 * @param data passed from android.app.Activity.onActivityResult().
385 public void onActivityResult(int requestCode, int resultCode, Intent data) {
386 super.onActivityResult(requestCode, resultCode, data);
390 * Pass through intents to XWalkView. Many internal facilities need this
391 * to receive the intents like web notification. See
392 * <a href="http://developer.android.com/reference/android/app/Activity.html">
393 * android.app.Activity.onNewIntent()</a>.
394 * @param intent passed from android.app.Activity.onNewIntent().
397 public boolean onNewIntent(Intent intent) {
398 return super.onNewIntent(intent);
402 * Save current internal state of this XWalkView. This can help restore this state
403 * afterwards restoring.
404 * @param outState the saved state for restoring.
407 public boolean saveState(Bundle outState) {
408 return super.saveState(outState);
412 * Restore the state from the saved bundle data.
413 * @param inState the state saved from saveState().
414 * @return true if it can restore the state.
417 public boolean restoreState(Bundle inState) {
418 return super.restoreState(inState);
422 * Get the API version of Crosswalk embedding API.
423 * @return the string of API level.
426 // TODO(yongsheng): make it static?
427 public String getAPIVersion() {
428 return super.getAPIVersion();
432 * Get the Crosswalk version.
433 * @return the string of Crosswalk.
436 // TODO(yongsheng): make it static?
437 public String getXWalkVersion() {
438 return super.getXWalkVersion();
442 * Embedders use this to customize their handlers to events/callbacks related
444 * @param client the XWalkUIClient defined by callers.
447 public void setUIClient(XWalkUIClient client) {
448 super.setUIClient(client);
452 * Embedders use this to customize their handlers to events/callbacks related
453 * to resource loading.
454 * @param client the XWalkResourceClient defined by callers.
457 public void setResourceClient(XWalkResourceClient client) {
458 super.setResourceClient(client);