Showing posts with label cross platform. Show all posts
Showing posts with label cross platform. Show all posts

Friday, March 3, 2017

Using Cordova Network API in gwt phonegap


The Network API provides access to the network connectivity information of the device, including wifi and cellular connection status.

Configuration


For using this API, you need to add Network Information plugin to your project.

cordova plugin add cordova-plugin-network-information
The network API can be accessed in gwt phonegap project as described below.

final PhoneGap phoneGap = GWT.create(PhoneGap.class);
Connection connection = phoneGap.getConnection();


The Network API is a very simple API and provides only 1 property/method and 2 events.


Usage


connection.type


Description


This provides the type of active network connection available. The possible connection types are :
  • Connection.UNKNOWN
  • Connection.ETHERNET
  • Connection.WIFI
  • Connection.CELL_2G
  • Connection.CELL_3G
  • Connection.CELL_4G
  • Connection.CELL
  • Connection.NONE

If the device doesn’t have a active network connection, then the connection type returned is NONE.

If the device has a active Wifi connection and a active 3G/2G/4G connection, then the connection type returned will be Wifi. The devices are designed to use the most efficient network when multiple active networks are available. Since the wifi is more efficient(more reliable, stable and cost effective), the devices normally use wifi even if other cellular connection types are available. If wifi is not available, then the device will check for the next efficient cellular connection type which normally will be in the sequence 4G,3G ,2G and CELL.

Sometimes, it is just not enough to known whether the device has active network connection or not. The application needs to know what is the connection type. You may have a user preference where user decides if the app can work on Cellular network or not. In this case, you need to check the connection type to match the user preference and allow the app to work in offline or not allow the user to launch if the connection type doesn’t match the user preference.

Code

String connectionType = connection.getType();


Supported Platforms


  • iOS
  • Android
  • Windows : When running on a emulator, the connection type is ETHERNET.


offline


Description


This event is fired when the device connected to the network is disconnected.


Code


The code for registering the offline event is below.
phoneGap.getEvent().getOffLineHandler().addOfflineHandler(new OffLineHandler() {
               
                @Override
                public void onOffLine(OffLineEvent event) {
               //Add your code here.
                }
            });


online


Description


This event is fired when the device is connected to the network.


Code


The code for registering the online event is below.
phoneGap.getEvent().getOnlineHandler().addOnlineHandler(new OnlineHandler() {
           
            @Override
            public void onOnlineEvent(OnlineEvent event) {
            //Add your code here.
            }
        });


Demo


The below example demonstrates usage of the Network API.

public class ConnectionDemo implements EntryPoint {
     private Logger log = Logger.getLogger(getClass().getName());
     private ScrollPanel scrollPanel = null;
     private LinkedList<String> list = null;
     private CellList<String> cellList = null;
     private Connection connection = null;
    
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
         GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {

              @Override
              public void onUncaughtException(Throwable e) {
                Window.alert("uncaught: " + e.getLocalizedMessage());
                Window.alert(e.getMessage());
                log.log(Level.SEVERE, "uncaught exception", e);
              }
            });

            final PhoneGap phoneGap = GWT.create(PhoneGap.class);
            phoneGap.addHandler(new PhoneGapAvailableHandler() {
              @Override
              public void onPhoneGapAvailable(PhoneGapAvailableEvent event) {
                    createUI(phoneGap);
              }
            });

            phoneGap.addHandler(new PhoneGapTimeoutHandler() {
              @Override
              public void onPhoneGapTimeout(PhoneGapTimeoutEvent event) {
                Window.alert("can not load phonegap");

              }
            });
            phoneGap.initializePhoneGap();
    }
    private void createUI(final PhoneGap phoneGap) {
        try {
            scrollPanel =  new ScrollPanel();
            list = new LinkedList<String>();
            BasicCell<String> cell = new BasicCell<String>() {

                @Override
                public String getDisplayString(String model) {
                    return model;
                }
            };
             cellList = new CellList<String>(cell);
            scrollPanel.add(cellList);
             connection = phoneGap.getConnection();
            addInfo("Connection Type : ");
            phoneGap.getEvent().getOnlineHandler().addOnlineHandler(new OnlineHandler() {
               
                @Override
                public void onOnlineEvent(OnlineEvent event) {
                    addInfo("online and connection Type : ");
                }
            });
            phoneGap.getEvent().getOffLineHandler().addOfflineHandler(new OffLineHandler() {
               
                @Override
                public void onOffLine(OffLineEvent event) {
                    addInfo("Offline and connection Type : ");
                }
            });
            RootPanel.get("ConnectionDemo").add(scrollPanel);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    private void addInfo(String type) {
        list.add(type+connection.getType());
        cellList.render(list);
        scrollPanel.refresh();
    }
}


Test


For testing, I will run this app in both iPhone 6 and Nexus 6P devices. The test steps are below.
  1. The app is launched when the device is connected to wifi and there is no cellular connectivity.
  2. Then the wifi is disabled and since there is no cellular connectivity, the device is in offline mode.
  3. The wifi is enabled.
  4. The wifi is again disabled.
  5. The cellular connectivity is enabled.
  6. The cellular connectivity is disabled.


Android Device


image

iOS Device



image

The code is available on the github.

Monday, February 27, 2017

Using Cordova Device API in gwt phonegap

 
Cordova Device API provides access to the device hardware and software information like what is the OS the device is running, its OS version and Serial id etc..

Configuration


This is a global object and is accessible any time after the deviceready event is triggered.

For using this API, you need to add the Cordova Device Plugin to the project.
cordova plugin add cordova-plugin-device
After your app is launched, include the below code to use any Apache Cordova API’s in the project.
final PhoneGap phoneGap = GWT.create(PhoneGap.class);
The Device API can be used only after the deviceready event is fired. So once your app is launched check for PhoneGapAvailableEvent. This event is fired once the Cordova library is successfully loaded and is ready for use.
    phoneGap.addHandler(new PhoneGapAvailableHandler() {
              @Override
              public void onPhoneGapAvailable(PhoneGapAvailableEvent event) {
                   // start your application code here.              }
            });
The Device API can be accessed through the Device object. The Device Object can be accessed as below

Device device = phoneGap.getDevice();

Usage


platform


Description


This property returns the operating system the device is running.
iOS devices return iOS while the Android devices return Android.

Code

String platform = device.getPlatform();

Supported Platforms


  • Android
  • iOS
  • Windows

version


Description


This property returns the device Operating system version number.

Code

String version = device.getVersion();

Supported Platforms


  • Android
  • iOS
  • Windows

cordova


Description


This property provides information of the platform specific cordova version running on the device. The cordova version and platform specific cordova  versions may be different. The cordova version displayed when you use cordova –v is the Cordova tooling and CLI version installed on your development machine.The cordova version may be 6.1, but the android platform cordova version may be 5.1 and ios platform cordova version may be 4.1. This platform specific Cordova version is the version of the cordova.js file in each platform. To get the platform specific version information from Cordova CLI, use the below command.
cordova platform list

image_thumb1


Code

String cordovaVersion = device.getPhoneGapVersion();

Supported Platforms


  • Android
  • iOS
  • Windows

model


Description


This property provides the information about the device model or product. This value is set by the manufacturer and may vary across the versions of the same product and these may not be well known names.
Ex: iPhone 6 is iPhone7,2.
On Android, this returns the product name instead of the device model.
The list of android devices product names mapping to common names is available at the below location.
https://support.google.com/googleplay/answer/1727131
And a similar list for iOS devices is available at the below url.
http://www.everyi.com/by-identifier/ipod-iphone-ipad-specs-by-model-identifier.html

Code

String model = device.getModel();

Supported Platforms


  • Android
  • iOS
  • Windows

manufacturer


Description


This property returns the name of the manufacturer of the device.
Ex: For iPhone, it would be Apple and for Samsung Galaxy Note, it would be samsung etc.

Code

String manufacturer = device.getManufacturer();

Supported Platforms


  • Android
  • iOS
  • Windows

uuid


Description


This property returns the device UUID(Universally Unique Identifier). The process of generating this UUID is different for different OS and is determined by the manufacturer.

On Android, uuid is a 64 bit integer created on first boot which is returned as string.

On iOS,uuid is created when a app from a vendor is installed. It will be same for all the apps from the same vendor. But if all the apps from the same vendor are reinstalled, then uuid will be recreated and will be different from the earlier one.

Code

String uuid = device.getUuid()

Supported Platforms


  • Android
  • iOS
  • Windows

serial


Description


This property returns the hardware serial number of the device, if available. Works only on physical devices since the emulators doesn’t have serial number.

Code

String serial = device.getSerial()

Supported Platforms


  • Android only.

isVirtual


Description


This property indicates whether the app is running on an emulator or an actual physical device.


Code

boolean isVirtual = device.isVirtual();

Supported Platforms


  • Android
  • iOS
  • Windows

Demo


The below example demonstrates the usage of the Device functions described above.

FlowPanel container = new FlowPanel();
Form widgetList = new Form();
widgetList.setHeader(new Label("Device"));
Device device = phoneGap.getDevice();
widgetList.add(new FormEntry("Platform", createTextBox(device
        .getPlatform())));
widgetList.add(new FormEntry("OS Version", createTextBox(device
        .getVersion())));
widgetList.add(new FormEntry("Cordova Version",
        createTextBox(device.getPhoneGapVersion())));
widgetList.add(new FormEntry("Model", createTextBox(device
        .getModel())));
widgetList.add(new FormEntry("Manufacturer", createTextBox(device
        .getManufacturer())));
widgetList.add(new FormEntry("Name",
        createTextBox(device.getName())));
widgetList.add(new FormEntry("Serial", createTextBox(device
        .getSerial())));
widgetList.add(new FormEntry("UUID",
        createTextBox(device.getUuid())));
widgetList.add(new FormEntry("Is Virtual", createTextBox(String
        .valueOf(device.isVirtual()))));
container.add(widgetList);
RootPanel.get("DeviceDemo").add(container);

The result of the program when it is run on different devices is as below.

Android emulator


g1_thumb1

On the Android emulator, since the manufacturer and the serial are displayed as unknown since this is a virtual device. As you can observe the device is indicated as a virtual device. Since the name of the device property is depreciated, the name value is not displayed.

Android Genymotion emulator


g2_thumb1

On a Genymotion Android emulator, the manufacturer is indicated as ‘Genymotion’ and there is no serial number as this is a virtual device and the serial number is derived from the hardware.
And similar to the other Android emulator, the device is indicated as a virtual device.
The model of the device selected in the Genymotion configuration is indicated as the model.
And the name of the device is depreciated and so no information is displayed against that.

Samsung device


Screenshot_2016-04-28-19-13-33_thumb

When the same program is run on an actual Samsung device, you see slightly different results.
The virtual status is returned as false, indicating that the device is an actual physical device.
The serial number is available since this is a real physical device.
The manufacturer name is displayed as ‘samsung’, since this is a samsung device.
The model of this device is displayed and to understand its common name, a reference to https://support.google.com/googleplay/answer/1727131 indicates that the market name of the device is ‘Galaxy Note3’.
Similar to emulator, the name is not displayed since it is depreciated.

iPhone


When the program is run on the iPhone

image_thumb4

The app is running on iPhone 6 device, so the isVirtual device flag is indicated as false.
And since name is not depreciated and serial is not supported on iOS, those values are set to blank and unknown respectively.
If we look up http://www.everyi.com/by-identifier/ipod-iphone-ipad-specs-by-model-identifier.html for iPhone7,2, we find out it is one of the iPhone 6 models.


iPad


When the app is run on iPad,


image_thumb3

The app is running on iPad device, so the isVirtual device flag is indicated as false.
And since name is not depreciated and serial is not supported on iOS, those values are set to blank and unknown respectively.
If we look up http://www.everyi.com/by-identifier/ipod-iphone-ipad-specs-by-model-identifier.html for iPad3,4, we find out it is iPad 4th Gen wifi model.


iPhone Simulator


When the program is run on the iPhone simulator:

image_thumb6

Since this is running on an emulator, the virtual flag is indicated as false and the name and serial are not supported on iOS and since this is a emulator, the model is indicated differently.

         The code displaying the usage of this API is available on github.








































Saturday, August 29, 2015

Setting up Windows Mobile App development environment.


Windows 8 is released by Microsoft which had major changes to the UI experience to compete with the other major mobile platforms like Android and iOS. As with all Microsoft related stuff, Windows mobile app development environment can be set up only on the Windows machines.

Visual Studio


You need to download Visual Studio from the below link.

https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx

Visual Studio is the IDE supported by Microsoft for developing apps using Microsoft related technologies.

image

Once the Visual Studio is downloaded, double click on downloaded installer to start the installation process and follow the instructions to complete the installation of visual studio.

Once visual studio is installed, launch visual studio.

15

Windows Phone tools


Select File –> New –> Project.

image

In the New Project Wizard, select Installed –> Templates –> Windows 8 and the tools required for creating Windows mobile project are displayed.

19

If the tools are already installed, then the other project related fields like Name, Location etc. are enabled, else the below Message is displayed.

20

Click on ‘Install’ to download the Windows Phone related tools and SDK’s. Once the installer is downloaded, double click on the installer to download and installer the Windows phone related tools.

If the Visual studio is open, close it and click on Retry in the below screen to continue installation of Windows mobile development environment.

21

The installer displays the list of packages that will be installed. Just click on ‘Next’ to continue installation.

22

Click on ‘Update’ to install the selected features. It will take considerable time based on bandwidth available since the installer that has to download is almost 4 GB of data.

23

Windows Phone project


Once the installation is completed, launch the Visual Studio to create and run a sample windows app.

Select File –> New –> Project. And in the New Project wizard, select Installed –> Templates –> Visual Basic –> Windows –> Windows 8 –> Blank App(Windows Phone)

10

Provide the App Name and the Location where you want to create the project and click on ‘OK’.
Visual Studio creates the Windows Phone project.

11

12

Select the emulator on which you want to run the demo app you created.

13

This will start building the project and launching the emulator.

14

15

16

The Windows Phone emulator is not fast and so it will take some time before the emulator is started.

17

The windows phone emulator is launched and the demo app is launched on the emulator. The app doesn’t display any thing which is totally fine, since it is just a template app and we didn’t make any changes to display in the app.

Issues with Emulator


While launching the emulator, you may run into some issues like the below. You may see the below message even if the virtualization is enabled on the system.

5

To work around this problem, go to Control panel –> Programs and Features –> Turn Windows features On or Off.

22


23

24

Check if all the Hyper-V features highlighted above are enabled. If they are not enabled, enable them. If they are enabled already, disable them, restart the machine and enable them again and restart the machine.

Then you may see the below message, just click on ‘Retry’ if you see a similar message.

6

Then you may see the below window, which asks whether you want to connect the emulator to the internet. Click on Ýes’ to allow the emulator to connect to the internet. During this process the network of your system may be disconnected for a while before it reconnects automatically.

7

The above steps should help in resolving any issues with launching the emulator.

Apache Cordova Environment


To create a Cordova project using Visual studio, select File –> New –> Project

In the New Project wizard, search for Cordova and the New Project wizard displays the list of Cordova project templates available.

16

Select ‘Install Tools for Apache Cordova Update 1’ for JavaScript.

17

If the required Windows Mobile tools etc. are not downloaded along with the version of Visual Studio you downloaded, the below message will be displayed.

18

Click on Ínstall’ to download the required tools for developing mobile apps.

Click on ‘Next’ to download and install the required tools for developing Apache Cordova apps using Visual Studio.

20

Click on Úpdate’ to start the download and install the Apache Cordova tools for Visual Studio.

21

Installing Cordova tools for Visual Studio requires around 10 GB of disk space, so make sure your disk has required disk space. Also downloading 10 GB of information over the internet is going to take considerable time.

25

Select File –> New –> Project and in New Project wizard select Installed –> Templates –> JavaScript –> Apache Cordova Apps. Select ‘Blank App (Apache Cordova)’ and provide the project name and location and click on ‘OK’.

26

Visual Studio creates a new Cordova app project.

27

Select the mobile os you want to emulate(highlighted in red) and then select the device model you want to emulate(highlighted in green).

28

This will launch the mobile emulator.

29

And the below message will be displayed in the emulator and since it is just a template app, the message displayed is fine.

30

Open index.html in the www folder and change the message displayed in the index.html to any other message and relaunch the app in emulator.


31

32

The modified message should be displayed in the emulator.

33

We have set up the development environment for creating windows mobile apps and learnt how to run the windows phone emulators.

Apache Cordova Tutorials

This is a series on setting up the development environments for various mobile platforms and using the various cordova core API's for developing Apache cordova apps.


Installing NodeJs on Windows

Setting up Mozilla FirefoxOS development environment on Windows.

Setting up Mozilla FirefoxOS development environment on Mac

Setting up Mozilla FirefoxOS development environment on Ubuntu

Setting up Windows Mobile Development environment.

Friday, August 28, 2015

Installing NodeJs on Windows

 
NodeJs is an asynchronous event driven JavaScript framework designed to build scalable network applications. NodeJs is used to distribute Cordova and all the plugins related to Apache Cordova. 

Download the latest version of nodejs from nodejs website.

https://nodejs.org/

clip_image002

Once the download is completed, double click on the installer to start the installation process.

clip_image004

Click on ‘Next’ to start the Nodejs installation.

clip_image006

Accept the End-User License Agreement and click on ‘Next’ to continue the installation process.

clip_image008

clip_image010

Select the location at which you want to install the Nodejs.

clip_image012

Click on ‘Next’ to continue installation.

clip_image014

Click on Ínstall’ button to start installing nodejs.

clip_image016

clip_image018

Click on ‘Finish’ to complete the nodejs installation.

clip_image020

To verify the installation, open command prompt and run the below commands.
npm –v
node –v

These commands should display the nodejs version and npm versions. They may vary from the below screen based on the version of the nodejs installed.

clip_image021

We have installed nodejs on the windows machine and we will be using this to work with Cordova in the upcoming tutorials.