Showing posts with label cordova. Show all posts
Showing posts with label cordova. 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.

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.


Monday, January 13, 2014

Using Ripple to emulate Apache Cordova / Phonegap applications.

Apache Ripple


After somegap I had to use Ripple emulator when I was trying to upgrade GWTPhonegap to support Apache Cordova 3.3. Apache Ripple is a browser agnostic mobile emulator. This will enable rapid development of Hybrid mobile applications using frameworks like Apache Cordova and Blackberry webworks. Ripple was initially developed by Blackberry and then donated to Apache open source community. Similar to Apache Cordova, Apache Ripple is a free and Open sourced under the Apache License, Version 2.0

Installation


The latest version of Ripple is distributed using nodejs and so you need to install nodejs first. The instructions to install nodejs are available here.

After installing nodejs, open the command prompt/terminal and run the below command to install ripple emulator.

npm install -g ripple-emulator




And since you will be emulating Apache Cordova applications, you need to install Cordova and create a Apache Cordova application.The instructions to install Apache Cordova are available here.

Running Ripple


Once you create a Apache Cordova project, make changes to the code, add the platform, prepare the project running the below command.

cordova prepare

The above command needs to be run every time you make changes to your project.



Run the below command from the root of your project to start the Ripple emulator.

ripple emulate




Once you run the above command, ripple will launch the default browser on the system. Your application will be emulated in the browser launched.




Some times you may see the below error page after Ripple launches the browser.



Just click on 'Wait' button or refresh to resolve the error.

Device


The devices section highlighted below provides options to emulate the application on different devices.



There is a option to select the device which we need to emulate. There are several Blackberry, Android and iOS devices available which can be emulated.

Also there is an option to simulate the vertical and horizontal orientation also.Just click on the vertical or horizontal  icon against Orientation label to simulate Vertical or Horizontal orientation.


Platform


The platform section provides options to choose the platform and version to be used for emulation.

The platform drop-down provides options to select a platform among WebWorks, Phonegap to emulate.


You can also select the version you want to emulate.Apache Cordova provides versions 1, 2 & 3. I believe all the versions between 1 and  2 can select 1 and so on.

Information


The information section provides information about the environment that is being emulated.The description of  the information displayed is below.


Version: version of the application
Platform: The framework that is being emulated.
Device: The device that is being emulated.
OS: Operating system of emulated device.
Manufacturer: Manufacturer of the device that is being emulated.
Screen: The screen resolution of the emulated device.
Density: The pixel density in the app you are emulating.
CSS Pixel Ratio: The device pixel ratio is the ratio between logical pixels and physical pixels.This indicates the pixel ratio of the emulated device.
CSS Pixels:The CSS pixels that is being emulated.
User Agent: Indicates the user agent in device that is being emulated.


Accelerometer


The accelerometer allows us to emulate the device motion changes using accelerometer  framework. You might as well emulate shaking the device to test the device motion changes. You can use the device icon in this section to emulate the device motion actions.




Settings



The 'Settings' has options to switch the themes between 'Light' or 'Dark' and you can turn the tool tips on or off.

Desktop browsers have a limitation which restricts the Ajax code from making cross domain requests. This is a security restriction on all the browsers. Chrome provides an flag to bypass that restriction. 


The Cross Domain Proxy is one of most useful features of the Ripple emulator. Ripple proxies the request made from then application, makes a request for the data from its own server and then proxies the result back to the application.





Device & Network Settings

This section provides you an option to emulate the network and gloabalization API of the Cordova framework.

You can select the connection type you want to select from all the connection types like ETHERNET, Wi-Fi, 2G, 3G, 4G and also you can indicate if there is a network lag or not.

There are also some functions from globalization API which can be emulated. You can provide the locale name, whether the day light savings are enabled or not and also the first day of the week. The values provided here will be used by your application when these functions are called.


Geo Location


The geolocation allows you to emulate the location related API in your application. You can enter the values to mock in the geolocation section and they will be used by your application.


Config


The Ripple emulator reads the configuration file and displays the result in this section . If there are any errors or issues they will be displayed here.


Events

The Events section provides several events(device ready, back button,pause, resume, search button, menu button, offline, online) which are supported by Apache Cordova. While these events are not actually simulated, they are just registered. Say if you select back button, the event will be registered to the Cordova API, but it doesn't simulate the actual hardware event.



Camera


Ripple supports some of the functions related to Camera API also. It allows you to select a image from your system and then use it for mocking the functionality later.




Unsupported options


As you can see by now, Ripple doesn't support all the options provided by Apache Cordova. When Ripple encounters a call that it doesn't support, it shows up a pop up similar to below.



In this case you can enter the JSON object to the callback that you want to execute.

Conclusion


Ripple is an excellent development tool to emulate the Apache Cordova applications on desktops. You will be able to switch between different OS's like Android, i-OS and Blackberry. And you will able to get a first hand feel of how your application looks on different devices and also in horizontal and Vertical orientations. There may be some limitations in Ripple, but I believe it is a must use tool for developers developing cross platform mobile applications using Apache Cordova. And once you are done with developing, you need make sure you will test the application on real device before pushing the applications to App Stores.