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.
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
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”/>
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.
nice one Tahir… 🙂
Great blog. Keep it up.
– Midhun VP
Thank you Tahir! You are amazing! 🙂
– Suraj
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
Hi Suraj,
a- In SUP 2.2. plugin.xml moved into the config.xml.
b- yes, you will add into the HWC project which in html->Js
Regards
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
Suraj,
Change it with oProp.getValue().toString();
Regards.
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
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
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
i didnt understand it? how to get the source code of android hwc container app?
plz help.
Rgrds,
Jitendra
Hi jitendra,
You can get Android HWC application source code from the path of installation Sybase SDK as,
C:\<sybasepath>\UnwiredPlatform\MobileSDK213\HybridWeb\Android
Thanks Tahir for help.
It worked.
Rgrds,
Jitendra