JBoss.orgCommunity Documentation
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.
Use the Errai Forge Addon Add Errai Features command and select Errai Cordova to follow along with this section.
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:
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:
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.
Pipes are for getting data from the server. Right now the only implementation is REST it will use Id to construct urls.
Pipe<Task> pipe = new PipeFactory().createPipe(Task.class, "tasks");
pipe.save(new Task(123, "new", "2012-01-01"), new AsyncCallback<Task>() {
@Override
public void onSuccess(Task result) {
Window.alert("jipee saved a taks");
}
@Override
public void onFailure(Throwable caught) {
}
});
This will preform a PUT /tasks URL
Another concept that comes with AeroGear is Stores. Currently there are 2 Store types supported: Memory and SessionLocal. Memory is just a big javascript array to hold your data. Here is how you create and configure a Store:
Store<User> store = new DataManager().store(User.class);
store.save(new User(2, "test2"));
Collection<User> collection = store.readAll();
User user = store.read(2);
Pipes can be authenticated by just adding the authenticator into the Pipe and you are good to go.
Authenticator authenticator = new AuthenticationFactory().createAuthenticator("name");
Pipe<Task> pipe = new PipeFactory().createPipe(Task.class, "tasks", authenticator);
authenticator.login(username.getText(), password.getText(), new AsyncCallback<String>() {
@Override
public void onSuccess(String result) {
Window.alert("successful login");
}
@Override
public void onFailure(Throwable caught) {
message.setText("Login failed, please try again");
}
});
There is also a method called enroll()
for adding new users.