Skip to Content
Author's profile photo Tahir Öz

Building Phonegap native plugin for HWC container – Android

In this blog we will write native plugin for Phonegap on Android hwc container.

Useful link

Building Phonegap native plugin for HWC container – IOS

The javascript class

The javacript file will be the same as previos blog

var SupuserinfoAccess = {

     devicename: function(types, success, fail) {

          return Cordova.exec(success, fail, “Supuserinfo”, “getUserinfo”, types);

     }

};

The Native Class


Get the source code of Android HWC container application on Eclipse for Android and create a new class name it as Supuserinfo.

Screen Shot 2013-06-04 at 10.07.05 PM.png

Open Supuserinfo class you just created and add the following code. As you can see it extends the Plugin superclass.


package com.sybase.hwc;

import org.apache.cordova.api.Plugin;

import org.apache.cordova.api.PluginResult;

import org.json.JSONArray;

import com.sybase.messaging.common.ClientConfig;

import com.sybase.messaging.common.persist.Property;

public class Supuserinfo  extends Plugin{

    public static final String FILEPATH = “getUserinfo”;

    @Override

    public PluginResult execute(String action, JSONArray data, String callbackId) {

        // TODO Auto-generated method stub

        if (FILEPATH.equals(action)) {

            String supUserName = “”;

            Property oProp = CustomizationHelper.getInstance().getDefaultConnectionAutoRegistration();

            oProp = CustomizationHelper.getInstance().getDefaultConnectionUserName();

            supUserName = oProp.Value.toString();

            return new PluginResult(PluginResult.Status.OK, supUserName);

        }

        return null;

    }

}

Add your plugin to the Plugin.xml file

On hwc container project go to “res->xml” and open the plugin.xml file

pluginxml.PNG

Add following  xml code to the bottom of the <plugins> tag. Make sure the package name is same as your project.

<plugin name=”Supuserinfo” value=”com.sybase.hwc.Supuserinfo”/>

addxml.PNG

Clean project and build again.

Go to the your Hybrid app project and in custom.js file call our plugin to test.


function customAfterWorkflowLoad() {

    document.addEventListener(“deviceready”, onDeviceReady, true);

}

function onDeviceReady() {

    SupuserinfoAccess.devicename([“x”],onSuccess,onFailure);

}

function onSuccess(name){

    alert(name);

}

function onFailure(error){

}

Deploy your project to the modified HWC container application and you will get registered username as below.

final.PNG

Assigned Tags

      12 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jitendra Kansal
      Jitendra Kansal

      nice one Tahir... 🙂

      Author's profile photo Midhun VP
      Midhun VP

      Great blog. Keep it up.

      - Midhun VP

      Author's profile photo Former Member
      Former Member

      Thank you Tahir! You are amazing! 🙂

      - Suraj

      Author's profile photo Former Member
      Former Member

      Hi Tahir,

      I am very new to Android world. I followed your blog religiously, but I am sure that I have done mistakes and missed some pre-requisites.

      Therefore, please be kind enough to help me.

      (a) I don't see plugins.xml in the xml folder.

      (b) Supuserinfo Javascript

      I hope I should create this Javascript in SUP HWC in html -> Js . Please provide a snapshot from your project ( if possible)

      Thanks for your help.

      - Suraj

      Author's profile photo Tahir Öz
      Tahir Öz
      Blog Post Author

      Hi Suraj,

      a- In SUP 2.2. plugin.xml moved into the config.xml.

      1a.PNG

      b- yes, you will add into the HWC project which in html->Js

      1b.PNG

      Regards

      Author's profile photo Former Member
      Former Member

      Dear Tahir,

      Thank you very much. I encountered a small problem here.

      I am unable to build the application as I got an error for oprop.value. Did you encounter this error as well ?

      - Suraj

      Author's profile photo Tahir Öz
      Tahir Öz
      Blog Post Author

      Suraj,

      Change it with oProp.getValue().toString();

      Regards.

      Author's profile photo Former Member
      Former Member

      Tahir,

      I appreciate your prompt response. Unfortunately, I am unable to succeed yet. Here is what I have done. I will be glad if you can help me further.

      1>New class created and config.xml updated

      2> Javascript and workflow screen

      3> custom.js

      But, when I open the application in HWC, I don't see anything

      Any thoughts where I might have done wrong.

      Thank you.

      - Suraj

      Author's profile photo Tahir Öz
      Tahir Öz
      Blog Post Author

      Well it seems okay, have you cleaned and build your android project ?. Have you added the onFailure method in custom.js as well?

      You can also try hwc android  project in debug mode see if it invokes the plugin.

      Tahir

      Author's profile photo Jitendra Kansal
      Jitendra Kansal

      Hi Tahir,

      i am trying to implement above scenario.

      just want to understand

      first i create one mbo, deploy to server, create hybrid designer, generate js, html etc class and then add Supuserinfo  js class with the mentioned code.

      after that you mentioned

      The Native Class

      Get the source code of Android HWC container application on Eclipse for Android and create a new class name it as Supuserinfo.

      i didnt understand it? how to get the source code of android hwc container app?

      plz help.

      Rgrds,

      Jitendra

      Author's profile photo Tahir Öz
      Tahir Öz
      Blog Post Author

      Hi jitendra,

      You can get Android HWC application source code from the path of installation Sybase SDK as,

      C:\<sybasepath>\UnwiredPlatform\MobileSDK213\HybridWeb\Android

      Author's profile photo Jitendra Kansal
      Jitendra Kansal

      Thanks Tahir for help.

      It worked.

      Rgrds,

      Jitendra