public class

XWalkView

extends XWalkViewInternal
java.lang.Object
   ↳ XWalkViewInternal
     ↳ org.xwalk.core.XWalkView

Class Overview

XWalkView represents an Android view for web apps/pages. Thus most of attributes for Android view are valid for this class. Since it internally uses android.view.SurfaceView for rendering web pages by default, it can't be resized, rotated, transformed and animated due to the limitations of SurfaceView. Alternatively, if the preference key ANIMATABLE_XWALK_VIEW is set to True, XWalkView can be transformed and animated because TextureView is intentionally used to render web pages for animation support. Besides, XWalkView won't be rendered if it's invisible.

XWalkView needs hardware acceleration to render web pages. As a result, the AndroidManifest.xml of the caller's app must be appended with the attribute "android:hardwareAccelerated" and its value must be set as "true".

 <application android:name="android.app.Application" android:label="XWalkUsers"
     android:hardwareAccelerated="true">
 

Crosswalk provides 2 major callback classes, namely XWalkResourceClient and XWalkUIClient for listening to the events related to resource loading and UI. By default, Crosswalk has a default implementation. Callers can override them if needed.

Unlike other Android views, this class has to listen to system events like intents and activity result. The web engine inside this view need to get and handle them. With contianer activity's lifecycle change, XWalkView will pause all timers and other components like videos when activity paused, resume back them when activity resumed. When activity is about to destroy, XWalkView will destroy itself as well. Embedders can also call onHide() and pauseTimers() to explicitly pause XWalkView. Similarily with onShow(), resumeTimers() and onDestroy(). For example:

   import android.app.Activity;
   import android.os.Bundle;

   import org.xwalk.core.XWalkResourceClient;
   import org.xwalk.core.XWalkUIClient;
   import org.xwalk.core.XWalkView;

   public class MyActivity extends Activity {
       XWalkView mXwalkView;

       class MyResourceClient extends XWalkResourceClient {
           MyResourceClient(XWalkView view) {
               super(view);
           }

           @Override
           WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String url) {
               // Handle it here.
               ...
           }
       }

       class MyUIClient extends XWalkUIClient {
           MyUIClient(XWalkView view) {
               super(view);
           }

           @Override
           void onFullscreenToggled(XWalkView view, String url) {
               // Handle it here.
               ...
           }
       }

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           mXwalkView = new XWalkView(this, null);
           setContentView(mXwalkView);
           mXwalkView.setResourceClient(new MyResourceClient(mXwalkView));
           mXwalkView.setUIClient(new MyUIClient(mXwalkView));
           mXwalkView.load("http://www.crosswalk-project.org", null);
       }

       @Override
       protected void onPause() {
           super.onPause();
           if (mXwalkView != null) {
               mXwalkView.pauseTimers();
               mXwalkView.onHide();
           }
       }

       @Override
       protected void onResume() {
           super.onResume();
           if (mXwalkView != null) {
               mXwalkView.resumeTimers();
               mXwalkView.onShow();
           }
       }

       @Override
       protected void onDestroy() {
           super.onDestroy();
           if (mXwalkView != null) {
               mXwalkView.onDestroy();
           }
       }

       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           if (mXwalkView != null) {
               mXwalkView.onActivityResult(requestCode, resultCode, data);
           }
       }

       @Override
       protected void onNewIntent(Intent intent) {
           if (mXwalkView != null) {
               mXwalkView.onNewIntent(intent);
           }
       }
   }
 

Summary

Constants
int RELOAD_IGNORE_CACHE Reload mode with bypassing the cache.
int RELOAD_NORMAL Normal reload mode as default.
Public Constructors
XWalkView(Context context, AttributeSet attrs)
Constructor for inflating via XML.
XWalkView(Context context, Activity activity)
Constructor for Crosswalk runtime.
Public Methods
void addJavascriptInterface(Object object, String name)
Injects the supplied Java object into this XWalkView.
void clearCache(boolean includeDiskFiles)
Clear the resource cache.
void evaluateJavascript(String script, callback)
Evaluate a fragment of JavaScript code and get the result via callback.
String getAPIVersion()
Get the API version of Crosswalk embedding API.
XWalkNavigationHistory getNavigationHistory()
Get the navigation history for current XWalkView.
String getOriginalUrl()
Get the original url specified by caller.
String getTitle()
Get the title of current web page/app.
String getUrl()
Get the url of current web page/app.
String getXWalkVersion()
Get the Crosswalk version.
boolean hasEnteredFullscreen()
Indicate that a HTML element is occupying the whole screen.
void leaveFullscreen()
Leave fullscreen mode if it's.
void load(String url, String content)
Load a web page/app from a given base URL or a content.
void loadAppFromManifest(String url, String content)
Load a web app from a given manifest.json file.
void onActivityResult(int requestCode, int resultCode, Intent data)
Pass through activity result to XWalkView.
void onDestroy()
Release internal resources occupied by this XWalkView.
void onHide()
Pause many other things except JavaScript timers inside rendering engine, like video player, modal dialogs, etc.
boolean onNewIntent(Intent intent)
Pass through intents to XWalkView.
void onShow()
Resume video player, modal dialogs.
void pauseTimers()
Pause all layout, parsing and JavaScript timers for all XWalkView instances.
void reload(int mode)
Reload a web app with a given mode.
boolean restoreState(Bundle inState)
Restore the state from the saved bundle data.
void resumeTimers()
Resume all layout, parsing and JavaScript timers for all XWalkView instances.
boolean saveState(Bundle outState)
Save current internal state of this XWalkView.
void setResourceClient(XWalkResourceClient client)
Embedders use this to customize their handlers to events/callbacks related to resource loading.
void setUIClient(XWalkUIClient client)
Embedders use this to customize their handlers to events/callbacks related to UI.
void stopLoading()
Stop current loading progress.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int RELOAD_IGNORE_CACHE

Reload mode with bypassing the cache.

Constant Value: 1 (0x00000001)

public static final int RELOAD_NORMAL

Normal reload mode as default.

Constant Value: 0 (0x00000000)

Public Constructors

public XWalkView (Context context, AttributeSet attrs)

Constructor for inflating via XML.

Parameters
context a Context object used to access application assets.
attrs an AttributeSet passed to our parent.

public XWalkView (Context context, Activity activity)

Constructor for Crosswalk runtime. In shared mode, context isi different from activity. In embedded mode, they're same.

Parameters
context a Context object used to access application assets
activity the activity for this XWalkView.

Public Methods

public void addJavascriptInterface (Object object, String name)

Injects the supplied Java object into this XWalkView. Each method defined in the class of the object should be marked with JavascriptInterface if it's called by JavaScript.

Parameters
object the supplied Java object, called by JavaScript.
name the name injected in JavaScript.

public void clearCache (boolean includeDiskFiles)

Clear the resource cache. Note that the cache is per-application, so this will clear the cache for all XWalkViews used.

Parameters
includeDiskFiles indicate whether to clear disk files for cache.

public void evaluateJavascript (String script, callback)

Evaluate a fragment of JavaScript code and get the result via callback.

Parameters
script the JavaScript string.
callback the callback to handle the evaluated result.

public String getAPIVersion ()

Get the API version of Crosswalk embedding API.

Returns
  • the string of API level.

public XWalkNavigationHistory getNavigationHistory ()

Get the navigation history for current XWalkView. It's synchronized with this XWalkView if any backward/forward and navigation operations.

Returns
  • the navigation history.

public String getOriginalUrl ()

Get the original url specified by caller.

Returns
  • the original url.

public String getTitle ()

Get the title of current web page/app. This may be different from what's passed by caller.

Returns
  • the title for current web page/app.

public String getUrl ()

Get the url of current web page/app. This may be different from what's passed by caller.

Returns
  • the url for current web page/app.

public String getXWalkVersion ()

Get the Crosswalk version.

Returns
  • the string of Crosswalk.

public boolean hasEnteredFullscreen ()

Indicate that a HTML element is occupying the whole screen.

Returns
  • true if any HTML element is occupying the whole screen.

public void leaveFullscreen ()

Leave fullscreen mode if it's. Do nothing if it's not in fullscreen.

public void load (String url, String content)

Load a web page/app from a given base URL or a content. If url is null or empty and content is null or empty, then this function will do nothing. If content is not null, load the web page/app from the content. If content is not null and the url is not set, return "about:blank" ifi calling getUrl(). If content is null, try to load the content from the url. It supports URL schemes like 'http:', 'https:' and 'file:'. It can also load files from Android assets, e.g. 'file:///android_asset/'.

Parameters
url the url for web page/app.
content the content for the web page/app. Could be empty.

public void loadAppFromManifest (String url, String content)

Load a web app from a given manifest.json file. If content is not null, load the manifest.json from the content. If content is null, try to load the manifest.json from the url. Note that url should not be null if the launched path defined in manifest.json is relative. It supports URL schemes like 'http:', 'https:' and 'file:'. It can also load files from Android assets, e.g. 'file:///android_asset/'.

Parameters
url the url for manifest.json.
content the content for manifest.json.

public void onActivityResult (int requestCode, int resultCode, Intent data)

Pass through activity result to XWalkView. Many internal facilities need this to handle activity result like JavaScript dialog, Crosswalk extensions, etc. See android.app.Activity.onActivityResult().

Parameters
requestCode passed from android.app.Activity.onActivityResult().
resultCode passed from android.app.Activity.onActivityResult().
data passed from android.app.Activity.onActivityResult().

public void onDestroy ()

Release internal resources occupied by this XWalkView. It will be called when the container Activity get destroyed. It can also be explicitly called to release resources.

public void onHide ()

Pause many other things except JavaScript timers inside rendering engine, like video player, modal dialogs, etc. See pauseTimers() about pausing JavaScript timers. It will be called when the container Activity get paused. It can also be explicitly called to pause above things.

public boolean onNewIntent (Intent intent)

Pass through intents to XWalkView. Many internal facilities need this to receive the intents like web notification. See android.app.Activity.onNewIntent().

Parameters
intent passed from android.app.Activity.onNewIntent().

public void onShow ()

Resume video player, modal dialogs. Embedders are in charge of calling this during resuming this activity if they call onHide. It will be called when the container Activity get resumed. It can also be explicitly called to resume above things.

public void pauseTimers ()

Pause all layout, parsing and JavaScript timers for all XWalkView instances. It will be called when the container Activity get paused. It can also be explicitly called to pause timers. Note that it will globally impact all XWalkView instances, not limited to just this XWalkView.

public void reload (int mode)

Reload a web app with a given mode.

Parameters
mode the reload mode.

public boolean restoreState (Bundle inState)

Restore the state from the saved bundle data.

Parameters
inState the state saved from saveState().
Returns
  • true if it can restore the state.

public void resumeTimers ()

Resume all layout, parsing and JavaScript timers for all XWalkView instances. It will be called when the container Activity get resumed. It can also be explicitly called to resume timers. Note that it will globally impact all XWalkView instances, not limited to just this XWalkView.

public boolean saveState (Bundle outState)

Save current internal state of this XWalkView. This can help restore this state afterwards restoring.

Parameters
outState the saved state for restoring.

public void setResourceClient (XWalkResourceClient client)

Embedders use this to customize their handlers to events/callbacks related to resource loading.

Parameters
client the XWalkResourceClient defined by callers.

public void setUIClient (XWalkUIClient client)

Embedders use this to customize their handlers to events/callbacks related to UI.

Parameters
client the XWalkUIClient defined by callers.

public void stopLoading ()

Stop current loading progress.