Master

90
OpenMobster - Mobile Cloud Platform App Developer Guide openmobster at gmail.com

description

Installation of Openmobster

Transcript of Master

  • OpenMobster - Mobile Cloud PlatformApp Developer Guideopenmobster at gmail.com

  • OpenMobster - Mobile Cloud Platform: App Developer Guideopenmobster at gmail.com2.2-SNAPSHOT

  • iii

    Table of Contents1. Introduction to OpenMobster - Mobile Cloud Platform .......................................................... 1

    Data Synchronization .................................................................................................. 1Real-Time Push Notifications ....................................................................................... 1Mobile RPC (Remote Procedure Call) ............................................................................ 1Management Console .................................................................................................. 1

    2. Mobile Programmer's Dilemma ......................................................................................... 2............................................................................................................................... 2WebApp ("Browser based") Development ....................................................................... 2

    Advantages ........................................................................................................ 2Disadvantages .................................................................................................... 2

    App ("Native") Development ........................................................................................ 3Advantages ........................................................................................................ 3Disadvantages .................................................................................................... 3

    Popular Mobile Platforms (in no particular order of preference or market share) ..................... 3Blackberry ......................................................................................................... 3Google Android ................................................................................................. 3iPhone .............................................................................................................. 3Symbian ............................................................................................................ 4Windows Mobile ................................................................................................ 4

    ............................................................................................................................... 4Back to the Future ...................................................................................................... 4

    3. Programming Concepts .................................................................................................... 5Cloud Server ............................................................................................................. 5

    Channel ............................................................................................................ 5MobileServiceBean ............................................................................................. 5

    Mobile App Frameworks ............................................................................................. 5Mobile Data Framework ...................................................................................... 5Mobile MVC Framework ..................................................................................... 6Mobile Cloud .................................................................................................... 6

    4. Architecture ................................................................................................................... 7OpenMobster Architecture ........................................................................................... 7Mobile Cloud Stack .................................................................................................... 8

    Sync ................................................................................................................. 8Push ................................................................................................................. 8OfflineApp ........................................................................................................ 8Mobile RPC ...................................................................................................... 9Network ............................................................................................................ 9Database ........................................................................................................... 9Inter-App Bus .................................................................................................... 9

    Cloud Server Stack ................................................................................................... 105. Get Started: Hello Mobster ............................................................................................. 12

    .............................................................................................................................. 12System Requirements ........................................................................................ 12

    Create your first Mobile Cloud App ............................................................................. 12Developing the Channel ............................................................................................. 12

    Step 1: Write the HelloSyncBean ......................................................................... 12Step 2: Write the HelloSyncChannel .................................................................... 13Step 3: Configuration ........................................................................................ 15

    Developing the Android App ...................................................................................... 16Step 1: Write the HomeScreen ............................................................................ 16Step 2: Configuration ........................................................................................ 16

  • OpenMobster - MobileCloud Platform

    iv

    Running the App ...................................................................................................... 176. Production Mode Installation ........................................................................................... 18

    Cloud-Side: Installation .............................................................................................. 18Install Cloud Server .......................................................................................... 18

    Device-Side: Installation ............................................................................................ 18Android Installation ........................................................................................... 18

    7. Sync App Development .................................................................................................. 20.............................................................................................................................. 20Tutorial ................................................................................................................... 20

    Cloud-Side: Channel Development ....................................................................... 20App/Device-Side: MobileBean component ............................................................. 24

    8. MobileBean .................................................................................................................. 26MobileBean ............................................................................................................. 26Cloud-Side ............................................................................................................... 26

    Specification .................................................................................................... 26Device-Side ............................................................................................................. 26

    Accessing a Simple Property .............................................................................. 26Accessing a Nested Property ............................................................................... 26Accessing an Indexed Property (One-Dimensional Array or a java.util.List) ................. 27Iterating through an Indexed Property (One-Dimensional Array or a java.util.List) ......... 27

    9. Push Programming ........................................................................................................ 28Sending a Push Notification ........................................................................................ 28Push Setup on an Android App ................................................................................... 28Push Setup on an iOS App ......................................................................................... 29

    Apple Provisioning ........................................................................................... 29OpenMobster Provisioning ................................................................................. 29

    10. iOS + OpenMobster integration ...................................................................................... 32Introduction ............................................................................................................. 32Prepare the mobilecloudlib static library ....................................................................... 32Start a View-based App ............................................................................................. 32Create a Group called OpenMobster ............................................................................. 32Add the libraries and Frameworks ............................................................................... 32Add OpenMobster bootstrap code ................................................................................ 33

    The bootstrapping functions ................................................................................ 33Integrating the bootstrapping function with the App Delegate .................................... 34

    Integrating the CloudManager ..................................................................................... 36Integrate the CloudManager button on the View ..................................................... 36Implement the action behind the button ................................................................ 37

    Sample App ............................................................................................................. 3711. iOS + OpenMobster Sample App ................................................................................... 38

    Introduction ............................................................................................................. 38Prepare the mobilecloudlib static library ....................................................................... 38Run the Cloud Server ................................................................................................ 38Run the SampleApp .................................................................................................. 38

    12. Device-To-Device Push Framework ................................................................................ 39Introduction ............................................................................................................. 39Integration ............................................................................................................... 39

    Step 1: Extend org.openmobster.android.api.d2d.D2DActivity ................................... 39Step 2: Configuration ........................................................................................ 40Step 3: Sample App .......................................................................................... 40

    13. PhoneGap: Offline Web Apps using the Sync Plugin ......................................................... 41Introduction ............................................................................................................. 41Offline App Usage .................................................................................................... 41

    Running the Cloud Server .................................................................................. 41

  • OpenMobster - MobileCloud Platform

    v

    Cloud Activation .............................................................................................. 41Installing the Offline App .................................................................................. 41

    Dissecting the JQuery Offline App .............................................................................. 42Load Synchronized Beans .................................................................................. 42Add a New Bean to the Sync Channel .................................................................. 43Update an existing Bean in the Sync Channel ........................................................ 45Delete a Bean from the Sync Channel .................................................................. 46

    Dissecting the Cloud ................................................................................................. 47The MobileBean ............................................................................................... 47The Channel .................................................................................................... 49

    14. PhoneGap + iOS + OpenMobster integration .................................................................... 51Introduction ............................................................................................................. 51Prepare the mobilecloudlib static library ....................................................................... 51Start a Cordova-based App ......................................................................................... 51Copy JSON components to the App ............................................................................. 51Create a Group called OpenMobster ............................................................................. 51Add the libraries and Frameworks ............................................................................... 51Add OpenMobster bootstrap code ................................................................................ 52

    The bootstrapping functions ................................................................................ 52Integrating the bootstrapping function with the App Delegate .................................... 53

    PhoneGapSync App .................................................................................................. 5515. PhoneGap: Sync Plugin Reference .................................................................................. 56

    Introduction ............................................................................................................. 56ReadAll - window.plugins.sync.readall ......................................................................... 56Get a Property Value - window.plugins.sync.value .......................................................... 57Execute Query Match All - window.plugins.sync.queryByMatchAll ................................... 57Execute Query Match Atleast One - window.plugins.sync.queryByMatchOne ....................... 58Execute Query Do Not Match All - window.plugins.sync.queryByNotMatchAll .................... 59Execute Query Do Not Match Even One - window.plugins.sync.queryByNotMatchOne .......... 59Execute Query Match Contains All - window.plugins.sync.queryByContainsAll ................... 60Execute Query Match Contains Atleast One - window.plugins.sync.queryByContainsOne ....... 61Add a New Bean into the Sync Channel - window.plugins.sync.addNewBean ....................... 62Update a Bean in the Sync Channel - window.plugins.sync.updateBean .............................. 63Delete a Bean from the Sync Channel - window.plugins.sync.deleteBean ............................. 64Commit the operations with the Sync Channel - window.plugins.sync.commit ...................... 65Get the Length of an Array/List property - window.plugins.sync.arrayLength ....................... 65Insert an object into an Array/List property - window.plugins.sync.insertIntoArray ................ 66Clear the entries inside an Array/List property - window.plugins.sync.clearArray .................. 67

    16. Location Aware Apps ................................................................................................... 68Location Aware Apps ................................................................................................ 68LocationServiceBean ................................................................................................. 68The App Side Logic .................................................................................................. 69

    Processing the Response .................................................................................... 7117. Mobile RPC (Remote Procedure Call) Development .......................................................... 72

    Cloud-Side: MobileServiceBean implementation ............................................................. 72Cloud-Side: Configuration .......................................................................................... 73Cloud-Side: Packaging and Deployment ........................................................................ 73Putting it altogether ................................................................................................... 73App/Device-Side: Invoking the MobileServiceBean ........................................................ 73

    18. Management Console ................................................................................................... 74.............................................................................................................................. 74GUI Functionality ..................................................................................................... 74

    Create Account ................................................................................................. 74Devices ........................................................................................................... 74

  • OpenMobster - MobileCloud Platform

    vi

    Administrators .................................................................................................. 74Push Setup ...................................................................................................... 74

    19. Mobile MVC Framework .............................................................................................. 75.............................................................................................................................. 75

    Components ..................................................................................................... 75Services .......................................................................................................... 80Tutorial ........................................................................................................... 80

  • 1Chapter 1. Introduction toOpenMobster - Mobile Cloud Platform

    openmobster at gmail.com OpenMobster is an open source platform for integrating native Mobile Apps with "Cloud" services

    It provides the following features:

    Data SynchronizationCloud data is made available to an App's local storage. It is available via a simple API. This allows the Appto function seamlessly in both online as well as offline modes. The data is automatically synchronized withthe Cloud service based on local state changes. These state changes are auto detected and synchronizedwith the Cloud. It does not require any special device-side sync-related programming on the part of thedeveloper.

    Real-Time Push NotificationsChanges on the Cloud are automatically pushed to the Apps in real time. The Push mechanism uses purenetwork/socket based approach instead of clunky methodologies like sending sms alerts or email alerts.The Push notifications happen inside the App's execution environment. Push notifications are sent via asimple API. It does not require the developer to learn any low-level platform specific services.

    Mobile RPC (Remote Procedure Call)Provides a simple way of exposing your server-side coarse grained business services. These services areinvoked via a simple RPC mechanism without any low-level programming like http-client code, clientside REST library, etc on the part of the App developer. There is a simple RPC API that is used for makingthese calls.

    Management ConsoleA Management Console is provided to administrate the Cloud Server. It provides security, and accountprovisioning features. Over time the console will carry device management features like remote wipe,remote tracking, remote lock-down, etc.

  • 2Chapter 2. Mobile Programmer'sDilemma

    openmobster at gmail.com

    The Mobile Platform is a relatively new beast in the world of computer programming. For the longest time(atleast since 1996 that I know of), companies have tried to come up with ways to squeeze the power ofthe Internet into a device that conveniently sits in your pocket. After many stop and go linear approaches,one company changed the game. Apple with their iPhone release in 2006. A whole new Computer withdifferent architectural rules, and user expectations was born. The notion of native Apps was created. Eventhough "The Browser" is a perfectly mainstream gateway to the Internet on a desktop, the same rule doesnot translate naturally to the mobile environment. In fact that was the very reason why Mobile Internethas been the next big thing atleast for the last 15 yrs. Companies lazily expecting their users to adopt "TheBrowser" on the mobile phone without realizing this is a completely different beast. Its a paradigm shiftin computer programming. The Mobile Computer is turning out to be the next step in evolution of thePersonal Computer.

    WebApp ("Browser based") DevelopmentWeb App Development involves accessing the Mobile App via a native browser located on the device.Programming can be done using standard web technologies like HTML, CSS, and Javascript>. Just like thePC world, the App executes completely on the server, while the mobile browser serves as a dumb terminalrendering just the UI. Some Javascript/Ajax approaches try to add some thickness to the architecture, butfor the most part the App is fully reliant on the server and an active network connection.

    Advantages No need to maintain a different codebase for each native mobile platform.

    Write Once, Run Anywhere flexibility for the developer.

    No App approval process needed.

    Uses the same serverside infrastructure as traditional web applications.

    Disadvantages Not a very good user experience. The network latency on a desktop does not map equally to a mobile

    device. Users need information quick. Even a delay of a few milliseconds can result in frustration. Samepause on a desktop goes completely ignored.

    Web apps don't have the native look and feel which makes the user experience un-intuitive. Plus, eachbrowser renders its content differently depending on the device, which can further add to frustration.

    Does not allow access to all the low level services of the mobile platform that can deliver true innovation.Accessing remote data on the go is not the only real advantage of this new computer. Its ability tofeed context sensitive information back into the system unlocks the potential for a whole new worldof computing.

    Some access via javascript and/or browser plugin is available, but it can never replicate the natural feelof accessing the low level platform API.

  • Mobile Programmer's Dilemma

    3

    Too much dependence on a network connection. No offline access to critical data in the event of anetwork outage.

    App ("Native") DevelopmentNative App Development involves developing the App using the programming language and APIsprovided by the native platform. It provides the most flexible approach to implementing complicatedfunctionality and provide the best user experience possible. Here are some of the advantages of developinga native app.

    Advantages Best user experience with respect to response times and intuitive user interaction

    Accessiblity to low level hardware, and sensors to truly take advantage of a mobile computer that isaware of its surroundings

    Offline access to critical data even in the event of network failure

    Provide intuitive push based notifications as the state of an App changes remotely

    Disadvantages Managing an App codebase across multiple programming languages and platform APIs

    No write once, run everywhere convenience for programmers

    Some platforms may require a slightly tedious approval process before installation on an actual device

    Popular Mobile Platforms (in no particularorder of preference or market share)

    Here is a list of some of the popular smartphone platforms available in the market

    Blackberry Programming Language: Java

    Operating System: RIMOS (Proprietary/Open API)

    Google Android Programming Language: Java

    Operating System: Android (Open Source, License: Apache 2.0)

    iPhone Programming Language: Objective-C Operating System: Mac OSX (Proprietary/Open API)

  • Mobile Programmer's Dilemma

    4

    Symbian Programming Language: C++

    Operating System: Symbian (Open Source, License: Eclipse Public License 1.0)

    Windows Mobile Programming Language: C#, .Net Programming Languages

    Operating System: Windows Mobile (Proprietary/Open API)

    In other words, whether to use the native approach or the web based approach is purely a business decision.For simple apps that only need to access data via a dumb terminal, native approach is overkill. For complexapps that fully utilize the power of the underlying platform, a native app is the best route.

    The OpenMobster Mobile Cloud Platform aims to deliver the low level infrastructure that providesservices needed to build easy to use, innovative native apps. It takes away the hardwork of writingmiddleware infrastructure, so that the app developer can focus their development effort on implementingtheir business requirements. Here are some of the ways, OpenMobster tries to alleviate some of thedisadvantages of native app development

    The pluggable nature of the Mobile MVC framework makes it easier to port an app across multipleplatforms. Most of the GUI level plumbing is provided by the Mobile Cloud runtime. Only thingthat requires porting are the Screen and Command components that are implemented using the nativeprogramming language/API.

    Having a consistent messaging spec across all the device level components. This makes the API featureslot more consistent and only thing that varies across platform code is the syntax of the programminglanguge.

    Provide the same exact framework API in case where the programming languages are the same. Forinstance, Blackberry OS and Google Android are based on the same 'Java' programming language. Inthat case the API of the various frameworks have the same exact syntax. This cuts down the portingeffort dramatically. Only code that would require porting is the code that uses the low level platformAPI. The code cannot be platform independent, but it can be language-portable.

    Back to the FutureThe ideal scenario would be to have a standard API/programming language across all platform for nativeapps. This is highly impractical considering we still have not achieved this in a desktop environment, anda mobile computer has many differentiating factors that prevent this from happening.

    However, what is possible are native apps that use HTML, and CSS for the GUI presentation layer,and Javascript for event handling code. This is in fact possible in theory with HTML5. HTML5 aims atproviding a standard for developing cross platform native apps, with all the features and services that aredesirable for a native app. However, this is still an on-going effort at the spec level. HTML5 based nativeapps are probably a couple of years away from fruition.

  • 5Chapter 3. Programming Conceptsopenmobster at gmail.com

    Cloud ServerA Cloud Server is the server-side component of the infrastructure that is located in the 'Cloud'. Thesystem provides mobile-oriented features like data synchronization, real-time push, and mobile rpc. Froman architecture standpoint it sits between the mobile device and the actual cloud data services beingmobilized. The Cloud Server provides a Java based Developer API to expose your data services. Here areits programming concepts:

    ChannelA Channel serves as a gateway for integrating on-device model/data objects with the server-side backendstorage systems such as relational databases, content repositories, or Enterprise systems like CRMs, ERPsetc. It provides a simple CRUD (Create, Read, Update, and Delete) interface to expose the backend data.The Channel is specifically designed such that the Developer does not have to worry about any low-levelstate management, synchronization, or other mobile-oriented issues. The idea is to keep a Channel a purelydata-oriented component.

    MobileServiceBeanA MobileServiceBean exposes some coarse grained business process to the on-device Mobile App.It provides a very simple request/response based synchronous invocation mechanism. It frees up thedeveloper from all low-level (Remote Procedure Call) concerns like making network connections, security,marshalling/unmarshalling payloads etc.

    Note: This component is quite simple at the time of the milestone M1 release. Eventually it will providemore robust REST-based functionality. In any case, the Developer will still be shielded from the low-levelprogramming details regardless of what higher-level services will be supported.

    Mobile App FrameworksMobile Data Framework

    The Mobile Data Framework provides Cloud data-oriented services like data synchronization, real-timepush notifications, and simple RPC (Remote Procedure Call) mechanism.

    MobileBeanMobileBean is a managed Mobile Component which carries the state of the domain object that it representson the Cloud. It is propagated from the Cloud Server to the mobile device via its corresponding "Channel"on the server. The Mobile Data Framework shields the App developer from state management issues like,offline access, receiving push notifications related to state changes on the server, synchronizing locallymodified beans back with the server, sync concepts like two-way sync, one-way sync etc. The nativeruntime smartly tracks the changes to the local state of the MobileBean and decides which type of syncis needed.

  • Programming Concepts

    6

    MobileServiceMobileService facilitates making RPC (Remote Procedure Call) invocations from the device to the serverside 'MobileServiceBean' components. It presents a simple API to the developer and shields them fromlow-level networking details, http libraries, REST invocations etc.

    Mobile MVC FrameworkThis is a thick client MVC (Model-View-Controller) framework. It is based on a Rich InternetApplication [http://en.wikipedia.org/wiki/Rich_Internet_application] design principle. At this point intime of evolution of the mobile space, there isn't a commonly adopted GUI development standard acrossvarious mobile platforms. This results in a lot of App porting activity across platforms. Although thisframework is not designed for (Write Once, Run Anywhere) approach, it abstracts out a lot of the UIFramework plumbing that would otherwise need to be written, ported and maintained by the developer.The framework abstracts out some of the low level services into the container such as App bootstrapping,screen navigation, graceful error handling, and internationalization. There are two types of componentsin this framework that use a plugin mechanism and receive call backs to perform activities implementedusing native platform API. These components are:

    ScreenScreen is an abstraction for an instance of the App screen that must be made visible to the user at a particularmoment in time The low level Navigation Manager keeps track of the various screens of an App andprovides services such as navigating to a specified screen, going back to the previous screen, and goingto the home screen. Besides the actual implementation of a "Screen" all services related to a "Screen" areportable across mobile platforms.

    CommandCommand is an abstraction for an instance of a GUI Event Handler which receives various callbacks basedon the screen's lifecycle A command typically puts a business process into motion via accessing variousother services like the Mobile Cloud Framework components and/or native platform services.

    The Mobile MVC Framework is extensible to support various GUI frameworks. This does open the doorfor integrating cross platform GUI frameworks like standard widgets, HTML5 based GUI, etc.

    Mobile CloudMobile Cloud is an on-device native system service. It hosts the runtime that is used by the above mentionedApp Frameworks. On platforms that support inter-application communication such as Android, there is asingle instance of a Mobile Cloud which is shared by all the Apps installed on the device. This helps makebetter use of device resources like storage, network management, push sockets, background services, etc.On platforms that do not support inter-application communication, an instance of the Mobile Cloud runtimemust be installed by bundling it with each App. The Mobile Cloud also comes with a Cloud ManagerApp. The Cloud Manager App provides configuration functions such as secure Device Activation, PushManagement, Channel Management, Discovering/Installing new Apps, etc.

  • 7Chapter 4. Architectureopenmobster at gmail.com

    OpenMobster Architecture

  • Architecture

    8

    Mobile Cloud Stack

    This is a software stack that is installed on the mobile device. It provides the following services to MobileApps: Sync, Push, OfflineApp, Mobile RPC, Network, Database, Inter-App Bus.

    SyncSync service auto-synchronizes all state changes to App/Moblet Data back with the Cloud Server. Itsupports various synchronization modes such as two way sync, one way server sync, one way device sync,slow sync, and boot sync.

    PushPush service manages state updates being sent as notifications from the Cloud Server. This improves themobile user's experience as they do not have to pro-actively check for new information. When relevantinformation becomes available on the server, the user is automatically notified via system notifications likea beep, vibration, etc. Clarification: The Push service is a real time comet based service. The notificationsare received within the context of the App and not as SMS alerts or some other non-intuitive experience.The experience is just like the Blackberry email experience. The Cloud Server does not require any specialinfrastructure like the Blackberry Enterprise Server to make this happen.

    OfflineAppOfflineApp service provided is designed to be an App Developer's best friend. Its carries the managementcapabilities to create smart coordination between low-level services like Sync and Push. Because of the

  • Architecture

    9

    OfflineApp service, the programmer never has to write any code to actually perform any synchronization.Synchronization is something that is managed by the OfflineApp service and it decides which mode ofsynchronization is the best for the current runtime state of the App. The App developer is never exposedto low level synchronization details like two way sync, one way device sync, etc. It coordinates managingthe Push service. It carries the smartness to track the type of data being pushed along with which installedApp on the device needs the notification. The App developer does not have to write any special code toreceive notifications. The moment the data channel for the App is established, all synchronizations andpush notifications are automatically handled by the OfflineApp service.

    Mobile RPCMobile RPC facilitates making synchronous RPC (Remote Procedure Call) invocations from the deviceto the server side 'MobileServiceBean' components.

    NetworkNetwork service manages establishing a network connection with the Cloud Server. It manages thecommunication channel needed to receive Push notifications from the server. It carries the smartness totrack coverage and establishes proper connections automatically. This is a very low-level service and anApp developer never has to deal with using it directly. The App developer is shielded from any low levelconnection establishment, security, protocol details, etc by using the higher level Mobile Data Frameworkcomponents.

    DatabaseDatabase service manages local data storage details for Apps. Depending on the platform in question ituses the corresponding storage facilities. It is designed to coordinate storage among the suite of Apps/Moblets installed on the device. It provides thread-safe concurrent access to the Apps. Just like the Networkservice, its a low-level service used by the Mobile Data Framework components.

    Inter-App BusInter-App Bus service provides low-level coordination/communication between the suite of Apps/Mobletsinstalled on the device.

  • Architecture

    10

    Cloud Server Stack

    This is a software stack that is installed on the server-side. It provides the following services to MobileApps: Sync, Push, Secure Socket-Based Data Service, Mobile RPC, Security, Management Console

    SyncSync service synchronizes device side App state changes with the backend services where the data actuallyoriginates. It provides a plugin framework to mobilize the backend data. It uses the concept of a data"Channel" which mobilizes the data in the form of "MobileBean" instances.

    PushPush service monitors data "Channels" for updates. The moment updates are detected, correspondingComet-based notifications are sent back to the device. If the device is out of coverage or disconnected forsome reason, it waits in a queue, and delivers the push the moment the device connects back to the network.Clarification: The push service does not depend on any special infrastructure like a Blackberry EnterpriseServer to achieve its functionality. Its a pure Comet-based approach via a socket channel with the device.

    Secure Socket-Based Data ServiceSecure Socket-Based Data Service is a high performance socket server based on Java NIO. The serviceuses the Apache MINA [http://mina.apache.org/] network application framework. It provides both, a plainsocket server , and a SSL-based socket server, depending on the security requirements of the Apps.

  • Architecture

    11

    Mobile RPCMobile RPC service on the server-side provides a Remote Procedure Call framework for invoking coarsegrained business services of an App. The components are plugged in as MobileService Beans and thedevice-side Mobile RPC service invokes them via a simple synchronous request/response based approach.

    SecuritySecurity component provides authentication and authorization services to make sure mobile devicesconnecting to the Cloud Server are in fact allowed to access the system. Every device must be first securelyprovisioned with the system before it can be used. After the device is registered, it is challenged for propercredentials when the device itself needs to be activated. Once the device is activated, all Cloud requestsare properly authenticated/authorized going forward.

    Management ConsoleEvery instance of a Cloud Server ships with a Command Line application called the Management Console.The console provides user and device provisioning functionalities. In the future, this same component willhave more device management features like remote data wipe, remote locking, remote tracking, etc.

  • 12

    Chapter 5. Get Started: Hello Mobsteropenmobster at gmail.com

    In the spirit of confidence building when using a new technology, lets start with a simple app. We willcall it the HelloSync app.

    System RequirementsCloud Server

    Java SE JDK v6.0

    Create your first Mobile Cloud AppDownload an OpenMobster distribution from here: Downloads [http://code.google.com/p/openmobster/downloads/list] .

    In your distribution, go to the directory AppCreator. Inside the directory, use a tool called the'appcreator.bat' (Windows), 'appcreator.sh' (Linux and Mac) to generate a skeleton project

    appcreator.bat

    This will generate a Maven-based skeleton for the Mobile App. Each generated project has the followingmaven modules:

    cloud: Contains the src for the Cloud-side Channel component. Java code is located under src/main/java, and configuration is located under src/main/resources.

    app-android: Contains the src for the Android App. Java code is located under src/main/java,Configuration is located under src/main/resources/moblet-app. Besides the OpenMobster componentsetup, the Android SDK specific setup is located under AndroidManifest.xml, and res directory

    moblet: This is an assembly component that packages this App into a jar file ready for deployment ina JBoss AS based OpenMobster instance.

    Developing the ChannelStep 1: Write the HelloSyncBean

    The HelloSyncBean is a simple annotated MobileBean that carries the domain level informationthat will be synchronized with the device. This information is encapsulated inside a MobileBeaninstance. It will be used by the native App on the device side. This bean implements theorg.openmobster.server.api.model.MobileBean interface. This is a simple marker interface. It does notrequire any methods to be implemented.

    package com.hello.sync;

  • Get Started: Hello Mobster

    13

    import org.openmobster.cloud.api.sync.MobileBean;import org.openmobster.cloud.api.sync.MobileBeanId;

    public class HelloSyncBean implements MobileBean { private static final long serialVersionUID = 1L;

    @MobileBeanId private String oid;

    private String message;

    public HelloSyncBean() {

    }

    public String getOid() { return oid; }

    public void setOid(String oid) { this.oid = oid; }

    public String getMessage() { return message; }

    public void setMessage(String message) { this.message = message; }}

    Step 2: Write the HelloSyncChannelA Channel serves as a gateway for integrating on-device model/data objects with the server-side backendstorage systems such as relational databases, email servers, content repositories, or Enterprise systems likeCRMs, ERPs etc. It provides a simple CRUD (Create, Read, Update, and Delete) interface to expose thebackend data. The Channel is specifically designed such that the Developer does not have to worry aboutany low-level state management, and synchronization issues.

    For sake of simplicity, we will only implement a small portion of theorg.openmobster.server.api.model.Channel interface.

    Implement the org.openmobster.server.api.model.Channel interface

  • Get Started: Hello Mobster

    14

    package com.hello.sync;

    import java.util.Date;import java.util.List;import java.util.ArrayList;

    import org.openmobster.cloud.api.sync.Channel;import org.openmobster.cloud.api.sync.ChannelInfo;import org.openmobster.cloud.api.sync.MobileBean;

    import org.openmobster.core.security.device.Device;

    @ChannelInfo(uri="hellosync", mobileBeanClass="com.hello.sync.HelloSyncBean")public class HelloSyncChannel implements Channel

    Annotate the channel class with org.openmobster.server.api.model.ChannelInfo annotation.

    uri: Unique value for registering the channel with the Channel Framework.

    mobileBeanClass: Class of the MobileBean that will be managed via this channel.

    Implement bootup method

    @Overridepublic List

  • Get Started: Hello Mobster

    15

    public String[] scanForNew(Device device, Date lastScanTimestamp) { //In this example, it pushes something every scan..Just for push demo return new String[]{"push:1", "push:2"};}

    This method checks with the backend service if a new MobileBean instance has been created on thebackend connected to by the channel. Based on that it would send the just new bean ids back, or returnnull, if nothing new is available. If something new is available, this information is automatically syncedand notified on the user's mobile device. If not, nothing happens on the device side.

    device: provides the context around which device should this call apply to. Backend information islinked via the user identity associated with the device

    lastScanTimestamp: provides when the last scan occurred. This way developer knows to only includebeans that may have been created in the backend since this period in time.

    Implement read method

    @Overridepublic MobileBean read(String id) { //Just mock data....Usually the bean //would be constructed using the data //from a backend service or database HelloSyncBean syncBean = new HelloSyncBean(); syncBean.setOid(id); syncBean.setMessage("hello from "+syncBean.getOid());

    return syncBean;}

    This method provides a fully loaded MobileBean instance associated with the supplied id.

    Step 3: ConfigurationRegister this channel using the cloud/src/resources/META-INF/openmobster-config.xml file.

    services://MobileObjectMonitor services://MobileServiceMonitor

  • Get Started: Hello Mobster

    16

    Developing the Android AppStep 1: Write the HomeScreen

    Using the OpenMobster MVC Framework requires at the very least a very basic understanding of AndroidApplication Fundamentals [http://developer.android.com/guide/topics/fundamentals.html]. A high levelunderstanding of the concept of Activities is a bonus. The HomeScreen component represents the main/home screen that is launched when the Android App is launched. The full src code of the HomeScreencomponent can be found here [http://openmobster.googlecode.com/svn/samples/hellosync/app-android/src/main/java/com/hello/sync/app/HomeScreen.java]. In order to keep the tutorial clean, we will onlyhighlight the aspect that deals with displaying the HelloSyncBeans that are synchronized between theCloud and the device. This is accomplished using the following snippet of code:

    @Overridepublic void postRender(){ ListActivity listApp = (ListActivity)Registry.getActiveInstance(). getContext();

    .............

    //Show the List of the "HelloSyncBeans" automatically synced and stored on the device //As a developer you only deal with the MobileBean component... //No low-level sync stuff to worry about if(MobileBean.isBooted("hellosync")) { MobileBean[] helloBeans = MobileBean.readAll("hellosync");

    //Preparing the ui with data stored in the beans..in the message field String[] ui = new String[helloBeans.length]; for(int i=0,size=ui.length;i

  • Get Started: Hello Mobster

    17

    com.hello.sync.app.HomeScreen

    com.hello.sync.app.ResetChannel

    hellosync

    Android platform specific configuration can be found under: android-app/AndroidManifest.xml, android-app/res/layout/home.xml, and android-app/res/strings.xml

    Running the App Start the Cloud Server. Under the HelloSync project, go to the cloud directory, and type in: mvn -PrunCloud integration-test

    Start the Android emulator. Here [http://developer.android.com/guide/developing/tools/emulator.html]are more details about using the Android Emulator

    emulator -avd droid

    and

    ddms

    Once the emulator is started and ddms is connected, install the development mode DevCloudCloudManager App and the HelloSync App. Go to the app-android directory and type in:

    mvn -Phot-deploy install

    Activate the emulated device with the Cloud Server by launching the DevCloud CloudManager Appand clicking the Activate option.

    Launch the HelloSync App.

    You can download the entire HelloSync App here: HelloSyncApp [http://openmobster.googlecode.com/svn/samples/hellosync.zip]

  • 18

    Chapter 6. Production ModeInstallation

    openmobster at gmail.com

    Cloud-Side: InstallationInstall Cloud Server

    Step 1: Download and install JBoss-5.1.0.GA from here [http://www.jboss.org/jbossas/downloads/] Step 2: Copy openmobster to the JBoss AS server directory. openmobster is a pre-configured/optimized

    instance for the OpenMobster Cloud Server

    Step 3: In the case of using MySQL5 as the database, modify openmobster-ds.xml according to yourown MySql5 instance

    Step 4: Start the JBoss AS instance with the OpenMobster binary installed using: run -c openmobster-b "a real IP address"

    Note: A real IP address like "192.168.0.1" or something like that is needed for the mobile device to beable to connect to the server. Even in the case of a simulation environment, the device's browser doesnot connect to a loopback address like "localhost" or "127.0.0.1"

    Step 5: Verify the Cloud Server installation by typing in: http://{cloudserverIp}:{port}/o

    Device-Side: InstallationMobile Cloud infrastructure runtimes MUST be installed on each device, before the device can integratewith the "Cloud Server". Think of them as bootstrap binaries.

    Each mobile platform will have its corresponding set of artifacts that must be first installed on the device.

    The current version consists of Google Android 2.0+ support

    Android InstallationIf you have a Cloud Server running and accessible from the Internet, you can simply download thesebinaries from the built-in browser of the device.

    Download the following and follow the instructions provided by the device:

    http://{cloudServer IP}:{cloudServer port}/o/android/cloudmanager

    Device Activation Step 1: Launch the 'CloudManager' app.

    Step 2: Select the 'Activate' function.

    Step 3: Follow the wizard and provide the appropriate values

  • Production Mode Installation

    19

    Server : IP Address of the Cloud Server

    Email : The user id for this user

    Password: The password to be used to secure the account

    Installing the Sample Offline AppUsing your CloudManager App , install the deployed Apps using the Corporate App Store option

  • 20

    Chapter 7. Sync App Developmentopenmobster at gmail.com

    A native Mobile App runs within the constraints of its local runtime. A major advantage of this environmentis access to local storage for data associated with the App. If logic is the nervous system of an App, datais its life blood. Without the data, an App is lifeless. Local Storage brings an App to life instantaneously.The closer this data is stored, the faster and better is the performance of the App.

    A typical Mobile App has the following data-oriented requirements

    Data originates in the Cloud.

    App related data is synchronized from the Cloud with the local storage over a network connection.

    Any data state changes (add/update/delete) via the App are synchronized with the Cloud.

    Any data state changes (add/update/delete) on the Cloud are pushed/synchronized with the device.

    This data is available to the mobile App even in the event the Cloud is unavailable.

    The OpenMobster Cloud Platform uses a "Channel" component on the Cloud-side and a MobileBeancomponent on the Device-side to provide the above mentioned offline capabilities to an App

    TutorialCloud-Side: Channel Development

    A Channel serves as a gateway for integrating on-device model/data objects with the server-side backendstorage systems such as relational databases, content repositories, or Enterprise systems like CRMs, ERPsetc. It provides a simple CRUD (Create, Read, Update, and Delete) interface to expose the backend data.The Channel is specifically designed such that the Developer does not have to worry about any low-levelstate management, and synchronization issues.

    Step 1:Define a simple MobileBean to represent a data entity being mobilized. This MobileBean should adhereto the MobileBean specification covered here: Specification

    import java.util.List;import org.openmobster.cloud.api.sync.MobileBean;import org.openmobster.cloud.api.sync.MobileBeanId;public class DemoBean implements MobileBean { @MobileBeanId private String beanId;

    private String demoString; //used to demonstrate mobilizing a simple property of type 'String'

    private String[] demoArray; //used to demonstrate mobilizing of an indexed property that is an 'array'

  • Sync App Development

    21

    private List demoList; //used to demonstrate mobilizing an indexed property that is a 'list'

    public DemoBean() {

    }

    public String getBeanId() { return beanId; }

    public void setBeanId(String beanId) { this.beanId = beanId; }

    public String getDemoString() { return demoString; }

    public void setDemoString(String demoString) { this.demoString = demoString; }

    public String[] getDemoArray() { return demoArray; }

    public void setDemoArray(String[] demoArray) { this.demoArray = demoArray; }

    public List getDemoList() { return demoList; }

    public void setDemoList(List demoList) { this.demoList = demoList; }}

    Step 2:Provide a Channel implementation that exposes this MobileBean via a CRUD interface.

    @ChannelInfo(uri="/offlineapp/demochannel",

  • Sync App Development

    22

    mobileBeanClass="org.openmobster.core.examples.offlineapp.DemoBean")public class DemoChannel implements Channel

    bootupThis method provides a subset of the "MobileBean" instances associated with the said device. They providejust enough information for an App to be functional. This helps with avoiding very long synchronizationsessions. The other beans are loaded on-demand from there on

    public List

  • Sync App Development

    23

    }

    create

    Creates a new instance of a "MobileBean" within the backend data service. This happens when a newinstance is created on the device and synchronized back with the Cloud. It returns the unique id generatedby the server and associated with this bean.

    public String create(MobileBean mobileBean) { DemoBean newBean = (DemoBean)mobileBean;

    //Generate a new unique bean Id. This bean was created on the Device and is being //synchronized with the backend cloud service String newBeanId = String.valueOf(this.getDemoRepository().getData().size()); newBean.setBeanId(newBeanId);

    this.demoRepository.getData().put(newBeanId, newBean);

    return newBeanId;}

    updateSynchronizes the updated state of a bean from the device with the state on the Cloud.

    public void update(MobileBean mobileBean) { DemoBean updatedBean = (DemoBean)mobileBean;

    this.demoRepository.getData().put(updatedBean.getBeanId(), updatedBean);}

    delete

    Deletes a bean instance that is user confirmed to be deleted from the device.

    public void delete(MobileBean mobileBean) { DemoBean deletedBean = (DemoBean)mobileBean; this.demoRepository.getData().remove(deletedBean.getBeanId());}

    Step 3:Provide the META-INF/openmobster-config.xml that will deploy the "Channel" into the Cloud Server.

  • Sync App Development

    24

    Step 4:Package the the above classes and the corresponding META-INF/openmobster-config.xml into a simplejar file.

    Step 5:Deploy this jar file into the "deploy" directory of your JBoss AS instance

    Putting it all togetherThe end-to-end Channel Example is located at : src/dev-tools/sampleApps/offlineapp/cloud andAppCreator/sampleApps/offlineapp/cloud

    App/Device-Side: MobileBean componentOn the device side, this data is accessible via the org.openmobster.android.api.sync.MobileBeancomponent. The device-side MobileBean is a generic component which exposes a Cloud-side MobileBeaninformation through a robust interface. Device-side MobileBean component semantic are coveredhere:Specification

    Here are some simple methods to access a MobileBean on the device

    readAll: Returns all MobileBean instances associated with a channel. Chances are some of the instancesare only proxies that will be loaded seamlessly when they are really needed on-demand. This processhappens behind the scenes and there is nothing special a programmer needs to do.

    MobileBean[] demoBeans = MobileBean.readAll("/offlineapp/demochannel"); actions = new Vector();int size = demoBeans.length;for(int i=0; i

  • Sync App Development

    25

    getValue: Reads the value associated with a field/property of an instance of a bean. A propertyexpression is provided to access this information.

    demoBeans[i].getValue("demoString");

    This particular method call reads the "demoString" property of the corresponding Cloud-SideMobileBean instance covered earlier.

    setValue: Updates the field/property of an instance of a bean. A property expression and its value areprovided.

    demoBeans[i].setValue("demoString", "new updated value");

    For a more detailed coverage of the MobileBean usage, please see some example code located at: src/dev-tools/sampleApps/offlineapp/app-android and AppCreator/sampleApps/offlineapp/app-android

  • 26

    Chapter 8. MobileBeanopenmobster at gmail.com

    MobileBeanMobileBean is a managed Mobile Component which carries the state of the domain object that it representson the server. It is propagated from the Cloud Server to the mobile device via its corresponding Channelon the server. The Mobile Data Framework shields the App developer from state management issues like,offline access, receiving push notifications related to state changes on the server, synchronizing locallymodified beans back with the server, etc.

    The concept of a MobileBean applies to both sides of the world, Cloud-Side as well as Device-Side.

    Cloud-SideOn the Cloud-Side the MobileBean is a simple Java Object that implements theorg.openmobster.cloud.api.sync.MobileBean interface. The MobileBean is processed byits corresponding Channel. Through the channel instances of these beans are serialized into wire formatand propagated to their respective devices. In order to be successfully serialized/deserialized, they shouldfollow the proper specification.

    Specification The system successfully processes the following properties of a bean: Simple Property, Nested Property,

    One-Dimensional Array property, and Parameterized java.util.List properties of ConcreteTypes.

    Array PropertiesMUST NOT contain Null elements.

    MUST contain an empty constructor

    MUST contain provide public get and set methods for each one of its properties

    Device-SideOn the Device-Side the MobileBeans from a channel are made accessible via the Mobile Data Framework.The generic org.openmobster.android.api.sync.MobileBean component is used to extractthe state associated with each instance. It provides various state-oriented operations. The individualproperties of a bean are accessed using simple and intuitive expressions.

    Accessing a Simple PropertyFor a simple property myName on a bean, the following expression is used:

    MobileBean.getValue("myName");

    Accessing a Nested PropertyFor a nested property myAddress.myStreet on a bean, the following expression is used:

  • MobileBean

    27

    MobileBean.getValue("myAddress.myStreet");

    Accessing an Indexed Property (One-Dimensional Arrayor a java.util.List)

    For an indexed property "myName" on the "third element" of an Array or List named "users" ona bean, the following expression is used:

    MobileBean.getValue("users[2].myName");

    Iterating through an Indexed Property (One-DimensionalArray or a java.util.List)

    private void iterateEmails(MobileBean mobileBean){ BeanList emails = mobileBean.readList("emails"); for(int index=0; index

  • 28

    Chapter 9. Push Programmingopenmobster at gmail.com

    Sending a Push NotificationYou can send a push notification from the cloud using the Push API. This API is located on theorg.openmobster.cloud.api.push.PushService object. On this object you use the following method

    /** * A device agnostic Push method. Push is associated with the user and not his operating system *

    * @param identity user that must receive this message * @param appId unique application id this push is associated with * @param message message to be sent * @param title title of the message * @param details any other details associated with the message */ public void push(String identity, String appId, String message, String title, String details)

    Push Setup on an Android AppYou can setup Push Notification support in your app via two configuration files. Before we go into thosedetails you must understand that there are two types of push notifications. User Initiated Push notificationusing the PushService API, and Sync Initiated Push Notification which is generated by the Sync Engine.

    Configuration:AndroidManifest.xml

    First you setup the user initiated push notification receiver. In the action value you have to make sure itsthe same as the name of the unique package that identifies this application.

    Next, you will setup sync initiated push notifications. There is no extra configuration to keep in mind here.

  • Push Programming

    29

    openmobster-app.xml

    org.openmobster.core.mobileCloud.android_native.framework.App push

    Here, launch-activity-class indicates the activity that must be launched when the user clicks on thenotification from the notification bar

    icon-name points to a drawable image that should be displayed as an icon with the notification.

    Push Setup on an iOS AppThis is a guide to integrate the iPhone Apple Push Notification (APN) based system with theOpenMobster? Push Service. It consists of several provisioning steps from the Apple side and integrationvia the Management Console on the OpenMobster? Side.

    Apple ProvisioningStep 1: Obtain the Application Certificate

    In order to push via the APN service, the provider side (OpenMobster?->APN connection) requiresa certificate for each App registered for Push Notifications. The best instructions for doing theproper provisioning and obtaining a certificate is explained at : http://mobiforge.com/developing/story/programming-apple-push-notification-services.

    Step 2: Getting an aps_production_identity.p12 certificateOnce you have downloaded the aps_production_identity.cer file from the Apple Provisioning Portal

    Import the aps_production_identity.cer into the KeyChain?. Double-clicking the file will do it

    Select both certificate and private key (associated to the application you wish to use to send notifications) Right click, and select Export 2 elements, give a name (for example : aps_production_identity.p12) and

    password (for example : p@ssw0rd) and then export as p12.

    OpenMobster Provisioning Step 1: Register the App and the Device Token

    On the OpenMobster? side, Apps that want Push notifications must be registered with the OpenMobster?system. The Device Token is also needed to be registered as it is a requirement for the Apple PushNotification Service. This registration is as follows:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

  • Push Programming

    30

    // Add the view controller's view to the window and display. [self.window addSubview:viewController.view]; [self.window makeKeyAndVisible];

    //Bootstrap the Cloud services [self startCloudService];

    //This registers the App for Push Notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

    return YES;}

    If the registation is successful a callback is invoked on the delegate. It goes as follows:

    - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *deviceTokenStr = [NSString stringWithFormat:@"%@",deviceToken]; deviceTokenStr = [StringUtil replaceAll:deviceTokenStr :@"" :@""];

    NSLog(@"DeviceToken: %@",deviceTokenStr);

    @try { SubmitDeviceToken *submit = [SubmitDeviceToken withInit]; [submit submit:deviceTokenStr]; } @catch (SystemException * syse) { UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Token Registration Error" message:@"Device Token Cloud Registration Failed. Please make sure your device is activated with the Cloud using the ActivationApp. Re-start this App to start the token registration again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; dialog = [dialog autorelease]; [dialog show]; }}

    These two operations registers the Application for Push notifications both on the device and on theOpenMobster? Push Service.

  • Push Programming

    31

    Step 2: Upload the certificate .p12 file

    Login to the Management Console: http://cloud-server-address/console

    Select Push Setup

    Find the App associated with this certificate

    Upload the certificate and supply its password

    If successfull, the icon next to the App will turn green

    Step 3: Send a Test Push

    Click on the App

    Click the 'Test Push' button

    Select the 'Device' where it should be sent

    You should receive a Push alert on your phone

  • 32

    Chapter 10. iOS + OpenMobsterintegration

    openmobster at gmail.com

    IntroductionAs of version 2.2-M1, iOS is fully supported by OpenMobster. Here are some tips related to iOS andOpenMobster integration

    Prepare the mobilecloudlib static library Open the mobilecloudlib XCode project by opening: iPhone/mobilecloudlib/

    mobilecloudlib.xcodeproj

    Build the project in XCode

    For some reason, building the mobilecloudlib fail to compile if your XCode code location is set to therecommended setting of "Derived Data". You must change this option to "Location Specified By Targets".Please take a look at this thread for details: https://groups.google.com/forum/#!searchin/openmobster-users/mobilecloudlib/openmobster-users/zJhJKbFekLs/WiNWtKfG_RcJ

    Start a View-based App Go to File>New Project. In the displayed project templates select the View-based Application and

    follow the wizard

    Create a Group called OpenMobster Create a New group named OpenMobster

    From the mobilecloudlibproject, DragnDrop/Copy all the resources located under the app-bundlegroup

    Add the libraries and FrameworksIn the Frameworks group add the following library and Frameworks

    libmobilecloudlib.a - OpenMobster static library

    CoreData.framework

    CFNetwork.framework

    CoreGraphics.framework

    UIKit.framework

  • iOS + OpenMobster integration

    33

    Add OpenMobster bootstrap codeBefore OpenMobster runtime can be used within an App. It must be bootstrapped and started. Thefollowing code shows how this bootstrapping process works.

    The bootstrapping functionsStart Cloud Service

    -(void)startCloudService{ @try { CloudService *cloudService = [CloudService getInstance]; [cloudService startup]; } @catch (NSException * e) { //something caused the kernel to crash //stop the kernel [self stopCloudService]; }}

    Stop Cloud Service

    -(void)stopCloudService{ @try { CloudService *cloudService = [CloudService getInstance]; [cloudService shutdown]; } @catch (NSException *e) {

    }}

    Start Device Activation if it is not activated with the Cloud already

    -(void)startActivation

  • iOS + OpenMobster integration

    34

    { @try { CloudService *cloudService = [CloudService getInstance]; [cloudService forceActivation:self.window.rootViewController]; } @catch (NSException * e) { //something caused the kernel to crash //stop the kernel [self stopCloudService]; }}

    Do a Sync at Startup

    -(void)sync{ CommandContext *commandContext = [CommandContext withInit:self.viewController]; BackgroundSyncCommand *syncCommand = [BackgroundSyncCommand withInit]; [commandContext setTarget:syncCommand]; CommandService *service = [CommandService getInstance]; [service execute:commandContext]; }

    Integrating the bootstrapping function with the AppDelegate- (BOOL) application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ //OpenMobster bootstrapping [self startCloudService]; [self sync];

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];

  • iOS + OpenMobster integration

    35

    } else { self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease]; }

    //setup the NavigationController self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

    //Add the CloudManager button to the navbar UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Cloud Manager" style:UIBarButtonItemStyleDone target:self.viewController action:@selector(launchCloudManager:)];

    self.navigationController.topViewController.navigationItem.leftBarButtonItem = button; [button release];

    //Add the Create button to the nav bar UIBarButtonItem *create = [[UIBarButtonItem alloc] initWithTitle:@"Create" style:UIBarButtonItemStyleDone target:self.viewController action:@selector(launchCreateBean)];

    self.navigationController.topViewController.navigationItem.rightBarButtonItem = create; [create release];

    self.window.rootViewController = self.navigationController; [self.window makeKeyAndVisible];

    //OpenMobster bootstrapping [self startActivation];

    //Register the App for Push notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

    return YES;}

    -(void)applicationWillEnterForeground:(UIApplication *)application

    - (void)applicationWillEnterForeground:(UIApplication *)application{ /* Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. */ //OpenMobster bootstrapping [self sync];

  • iOS + OpenMobster integration

    36

    if(!self.pushRegistered) { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; }}

    -(void)applicationWillTerminate:(UIApplication *)application

    - (void)applicationWillTerminate:(UIApplication *)application{ /* Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. */ //OpenMobster bootstrapping [self stopCloudService];}

    Integrating the CloudManagerAs an App Developer you can integrate the CloudManager functionality within your App. TheCloudManager is an administrative GUI tool that allows some provisioning functions. You can activateyour device with the Cloud and you can manage the Sync Channels used by your App. This can come inhandy when you want to do some Manual Syncing in case there are issues happening with the automaticSync process.

    This GUI layer integration is done by activating the Modal,View,Controller CloudManager component.Here are the integration steps

    Integrate the CloudManager button on the View

    //setup the NavigationControllerself.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

    //Add the CloudManager button to the navbarUIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Cloud Manager" style:UIBarButtonItemStyleDone target:self.viewController action:@selector(launchCloudManager:)];

    self.navigationController.topViewController.navigationItem.leftBarButtonItem = button;[button release];

  • iOS + OpenMobster integration

    37

    Implement the action behind the button

    -(IBAction)launchCloudManager:(id)sender{ //Launch the CloudManager App [CloudManager modalCloudManager:self];}

    Sample AppIn the OpenMobster distribution, you can find an iOS/OpenMobster Sync App under iphone/SampleApp.On the Cloud Side, the App to run is located under iphone/showcase/cloud. You run the Cloud Serverusing the command

    mvn -PrunCloud integration-test

  • 38

    Chapter 11. iOS + OpenMobsterSample App

    openmobster at gmail.com

    IntroductionThis chapter covers the steps involved in running the iOS + OpenMobster based Sample App

    Prepare the mobilecloudlib static library Open the mobilecloudlib XCode project by opening: iPhone/mobilecloudlib/

    mobilecloudlib.xcodeproj Build the project in XCodeFor some reason, building the mobilecloudlib fail to compile if your XCode code location is set to therecommended setting of "Derived Data". You must change this option to "Location Specified By Targets".Please take a look at this thread for details: https://groups.google.com/forum/#!searchin/openmobster-users/mobilecloudlib/openmobster-users/zJhJKbFekLs/WiNWtKfG_RcJ

    Run the Cloud ServerGo to iphone/showcase/cloud. Under this directory issue the following command to start the Cloud Serverrequired by the sample app

    mvn -PrunCloud integration-test

    Run the SampleAppOpen the XCode project located under iphone/SampleApp. Build the project to make sure there are noerrors. Once the Build is successful, Run the project. If this is the first time running the App, it will presenta device activation screen. This is to securely setup your Cloud account so that you can acess the Enterpriseresources in the Cloud. It asks the following information

    Cloud IP: The IP address of the Cloud Server Port: The port that the Cloud Server is running on (1502, by default) Email Address: Your email address to uniquely identify you with the Cloud Password: Your password to authenticate with the Cloud

    This is a one time activation across all your apps.

  • 39

    Chapter 12. Device-To-Device PushFramework

    openmobster at gmail.com

    IntroductionOpenMobster 2.2-M7, introduces a new device to device push framework. Using this frameworkdevelopers can write Apps which communicate with other devices in the cloud and vice versa. Someapplications of this would be a Chat App or an Instant Messaging App.

    IntegrationStep 1: Extendorg.openmobster.android.api.d2d.D2DActivity

    Override the onStart method to start the CloudService

    @Override protected void onStart() { super.onStart();

    CloudService.getInstance().start(this); }

    Implement the callback method to handle the GUI aspects of receiving an incoming push message

    public void callback(D2DMessage message) { //Append a newly received message from the chat system LinearLayout layout = (LinearLayout)ViewHelper.findViewById(MainActivity.this, "layout"); TextView chatView = new TextView(MainActivity.this); chatView.setText(message.getMessage()); layout.addView(chatView);

    Toast.makeText(MainActivity.this, message.getMessage(), Toast.LENGTH_SHORT).show();

    TextView user = (TextView)ViewHelper.findViewById(this, "user"); user.setText(message.getFrom()); this.to = message.getFrom(); }

  • Device-To-Device Push Framework

    40

    Step 2: ConfigurationIn AndroidManifest.xml, register the org.openmobster.android.api.d2d.D2DReceiver broadcastreceiver

    Also in AndroidManifest.xml, register theorg.openmobster.core.mobileCloud.api.ui.framework.push.PushBroadcastReceiver to receive andnotify arrival of Push messages

    In openmobster-app.xml, configure the Push settings as follows:

    false

    org.openmobster.app.MainActivity push

    launch-activity-class: Is the Activity that must be launched when a Push notification is clicked

    icon-name: Is the icon to display with the Push notification

    Step 3: Sample AppThe OpenMobster, 2.2-M7 binary distribution comes packaged with a sample application for the Device-To-Device Push Framework. It is a simple Chat app and its goal is to show how the framework shouldbe used to build more complex app. The app is located under AppCreator/sampleApps/chat

  • 41

    Chapter 13. PhoneGap: Offline WebApps using the Sync Plugin

    openmobster at gmail.com

    IntroductionPhoneGap [http://www.phonegap.com] is an HTML5 app platform that allows you to author nativeapplications with web technologies and get access to APIs and app stores. PhoneGap leverages webtechnologies developers already know best... HTML and JavaScript. Starting with OpenMobster 2.2-M8,you can write offline web apps with synchronization of data using the OpenMobster Sync Plugin forPhoneGap. The Sync Plugin exposes the native Sync service to the JavaScript layer using the PhoneGapbridge technology. The rest of this chapter will discuss how to use the Sync Plugin using a JQuery basedsample offline application.

    Offline App UsageRunning the Cloud Server

    cd PhoneGap/plugin-jquery-cloud

    mvn -PrunCloud integration-test

    This should make the OpenMobster Cloud Server up and running for the Offline App.

    Cloud ActivationFor security reasons, before apps can use the OpenMobster Cloud, the device must be registered with thecloud. This is done using a CloudManager App that comes with the OpenMobster distribution.

    You can locate this App in the distribution under Android/core-runtime/CloudManager.apk. You caninstall this App on the Android device or emulator using the following command:

    adb install -r CloudManager.apk

    Once installed you can use the Activate function to register with the Cloud.

    Installing the Offline App

  • PhoneGap: Offline WebApps using the Sync Plugin

    42

    adb install -r JQueryOfflineApp.apk

    Dissecting the JQuery Offline AppLoad Synchronized Beans

    //read the oids of the tickets stored in the sync channel window.plugins.sync.readall(channel, function(oids) { if(oids == '0') { return; }

    oids = JSON.parse(oids); var length = oids.length; for(var i=0; i

  • PhoneGap: Offline WebApps using the Sync Plugin

    43

    function(oids) { if(oids == '0') { return; }

    oids = JSON.parse(oids);

    Invokes the readall function and reads the oids of all the beans stored in the sync channel. If the functionis successful it returns an array of oids in JSON format. oids are then parse into a JavaScript object usingthe JSON.parse function.

    //read the value of the 'title' property of the synchronized bean window.plugins.sync.value(channel,oid,'title', function(value) { var encodedOid = encodeURIComponent(oid);

    //create a list item corresponding to the ticket in question html += ''+value+''; }, function(error) { } );

    window.plugins.sync.value reads the value of the specified title property. It takes the channel name andthe oid of the bean as arguments to locate the bean whose property is to be read.

    Add a New Bean to the Sync Channel

    window.plugins.sync.addNewBean(channel, function(tempoid) { window.plugins.sync.updateBean(channel,tempoid,'title',title, function(success) { }, function(error) { });

    window.plugins.sync.updateBean(channel,tempoid,'customer',customer, function(success) {

  • PhoneGap: Offline WebApps using the Sync Plugin

    44

    }, function(error) { });

    window.plugins.sync.updateBean(channel,tempoid,'specialist',specialist, function(success) { }, function(error) { });

    window.plugins.sync.updateBean(channel,tempoid,'comments',comments, function(success) { }, function(error) { }); }, function(error) { alert("Sync Plugin:"+error); });

    //Commit here window.plugins.sync.commit(function(success) { alert("Ticket was successfully added"); }, function(error){ alert("Ticket Add Error:"+error); });

    The above code creates a new bean in the Sync Channel. Once the bean is created, its properties are updatedand committed to the Sync Engine for synchronization

    window.plugins.sync.addNewBean(channel, function(tempoid) {

    window.plugins.sync.addNewBean creates a new bean into the Sync Channel. The method returns atemporary oid used to refer to this newly added bean.

    window.plugins.sync.updateBean(channel,tempoid,'title',title, function(success)

  • PhoneGap: Offline WebApps using the Sync Plugin

    45

    { }, function(error) { });

    window.plugins.sync.updateBean(channel,tempoid,'customer',customer, function(success) { }, function(error) { });

    window.plugins.sync.updateBean updates the specified property on the bean referred to by its oid. Inthis case it modifies the title property on the newly added bean referred to by tempoid.

    //Commit here window.plugins.sync.commit(function(success) { alert("Ticket was successfully added"); }, function(error){ alert("Ticket Add Error:"+error); });

    window.plugins.sync.commit commits the beans into the Sync Channel for synchronization

    Update an existing Bean in the Sync Channel

    var oid = $('#update_ticket_oid').val();

    //update the 'title' property on the ticket bean window.plugins.sync.updateBean(channel,oid,'title',title, function(success) {

    }, function(error) { });

    //update the 'customer' property on the ticket bean window.plugins.sync.updateBean(channel,oid,'customer',customer, function(success) {

  • PhoneGap: Offline WebApps using the Sync Plugin

    46

    }, function(error) { });

    //update the 'specialist' property on the ticket bean window.plugins.sync.updateBean(channel,oid,'specialist',specialist, function(success) {

    }, function(error) { });

    //update the 'comments' property on the ticket bean window.plugins.sync.updateBean(channel,oid,'comments',comments, function(success) {

    }, function(error) { });

    //commit window.plugins.sync.commit(function(success) { alert("The Ticket was successfully saved"); }, function(error){ alert('Ticket Update Failed: '+error); });

    This is very similar to the add new bean explanation above. It updates each property of the bean and thencalls commit to get the bean synchronized with the Cloud.

    Delete a Bean from the Sync Channel

    function deleteTicket() { var oid = $('#read_ticket_oid').val();

    //delete this bean window.plugins.sync.deleteBean(channel,oid, function(success) { //commit window.plugins.sync.commit(function(success)

  • PhoneGap: Offline WebApps using the Sync Plugin

    47

    { alert("The Ticket was successfully deleted"); }, function(error){alert("Ticket Delete Failed: "+error);}); }, function(error) { alert("Ticket Delete Failed: "+error); } ); $.mobile.changePage('#tickets','slide',true,false); }

    window.plugins.sync.deleteBean deletes the bean referred to by the oid on the specified channel.window.plugins.sync.commit commits this change into the Sync Channel and prepares forsynchronization with the Cloud.

    Dissecting the CloudThe MobileBean

    public class JQueryBean implements MobileBean,Serializable { @MobileBeanId private String oid;

    Creates a MobileBean by implementing the MobileBean interface. This is the component which will beinjected into the Sync Channel. It will then be accesssed on the device via the Sync Plugin API. TheMobileBeanId annotation specified that the oid field will serve as the unique object identifier for thesebeans.

    Full Source of the MobileBean implementation

    public class JQueryBean implements MobileBean,Serializable{ @MobileBeanId private String oid;

    private String title; private String customer; private String specialist; private String comments;

    public JQueryBean() {

  • PhoneGap: Offline WebApps using the Sync Plugin

    48

    }

    public String getOid() { return oid; }

    public void setOid(String oid) { this.oid = oid; }

    public String getTitle() { return title; }

    public void setTitle(String title) { this.title = title; }

    public String getCustomer() { return customer; }

    public void setCustomer(String customer) { this.customer = customer; }

    public String getSpecialist() { return specialist; }

    public void setSpecialist(String specialist) { this.specialist = specialist; }

    public String getComments() { return comments; }

    public void setComments(String comments) { this.comments = comments; }}

  • PhoneGap: Offline WebApps using the Sync Plugin

    49

    The ChannelThe Channel is the component that exposes the MobileBeans to the Sync Engine via a CRUD (Create,Read, Update, Delete) interface.

    @ChannelInfo(uri="plugin_jquery_channel", mobileBeanClass="org.openmobster.core.phonegap.plugin.jquery.cloud.JQueryBean")public class PluginJQueryChannel implements Channel

    The ChannelInfo.uri specifies the name of the Sync Channel and ChannelInfo.mobileBeanClassspecifies the class of the MobileBean instance that the channel will be dealin