JBoss.orgCommunity Documentation

Chapter 12. Errai Cordova (Mobile Support)

12.1. Integrate with native hardware
12.2. AeroGear Wrappers
12.2.1. Pipes
12.2.2. Stores
12.2.3. Authentication

Starting with version 2.4.0, Errai now supports mobile development. One of the modules that makes this feasible is the Cordova module. It offers a way to integrate with native hardware in an Errai way.

Plugin Tip

Use the Errai Forge Addon Add Errai Features command and select Errai Cordova to follow along with this section.

Manual Setup

Checkout the Manual Setup Section for instructions on how to manually add Errai Cordova to your project.

When the Cordova module is included you can integrate with native hardware by injecting the native components into your code:

@Templated("#main")

public class KitchenSinkClient extends Composite {
  @Inject
  Camera camera;
  @Inject
  @DataField
  Button takePicture;
  @EventHandler("takePicture")
  public void onTakePicktureClicked(ClickEvent event) {
    PictureOptions options = new PictureOptions(25);
    options.setDestinationType(PictureOptions.DESTINATION_TYPE_DATA_URL);
    options.setSourceType(PictureOptions.PICTURE_SOURCE_TYPE_CAMERA);
    camera.getPicture(options, new PictureCallback() {
      @Override
      public void onSuccess(String data) {
        image.setUrl(UriUtils.fromSafeConstant("data:image/jpeg;base64," + data));
      }
      @Override
      public void onFailure(String error) {
        setGeneralErrorMessage("Could not take picture: " + error);
      }
    });
  }

The components that are supported come from the gwt-phonegap project have a look there form more documentation.

Here are the native hardware components you can inject:

  • Camera
  • Accelerometer
  • Contacts
  • Capture (Provides access to the audio, image, and video capture capabilities of the device).
  • Compass
  • Notification ( see documentation on phonegap site )
  • File create a native file
  • Device Get general information about the device.

So to integrate with these things all we have to do is @Inject these classes. There are also a couple of CDI events one can observe to be informed about hardware state:

  • BackButtonEvent
  • BatteryCriticalEvent
  • BatteryEvent
  • BatteryLowEvent
  • BatteryStatusEvent
  • EndCallButtonEvent
  • MenuButtonEvent
  • OffLineEvent
  • OnlineEvent
  • PauseEvent
  • ResumeEvent
  • SearchButtonEvent
  • StartCallButtonEvent
  • VolumeDownButtonEvent
  • VolumeUpButtonEvent

Example of how to use these events:

  private void batteryIsLow(@Observes BatteryLowEvent event) {

    //mission accomplished. we can stop the infinite loop now.
  }

These wrappers allow your Errai client to talk to an AeroGear server. Also have a look at the documentation of the AeroGear project.