Wednesday, 13 June 2012

Simple plugins with PhoneGap1.7 Android

Recently I had a major run in with PhoneGap 1.7 and since I had a lot of java just begging to be called I ended up using plugins in PhoneGap.

Though there is a decent example on the PhoneGap website , I found it a little off base for a first plugin example , to explain just the basic functioning of plugins.The example I post here is a beginner level example which will help you understand the basic mechanism of plugins without any fancy stuff.

Steps :
1)Create an PhoneGap powered Android Project using phonegap docs
2)This is where we write our business logic . Create a java file MyPluginClass . This class will return some standard string to a html. You need to override the execute method of Plugin class and send back your result in a PluginResult object

package org.firstplugin;



public class 
MyPluginClass extends Plugin{



@Override

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

       try{

       PluginResult rs=null;

      /* Create the JSONObject which will be sent in PluginResult */

      JSONObject jsonobj=new JSONObject();

    /* Add whatever you want to send back to PhoneGap in the JSONObject */

       jsonobj.put("myname","whats in a name");

   /*  Put your JSONObject in a PluginResult object */

       rs=new PluginResult(Status.OK,jsonobj);

}
catch(JSONException e){ LOG.d("DEBUG",e.getmessage())}

/* Send your PluginResult object back to PhoneGap */

return rs;
}

}

3)Now we shall write the plugin javascript which will call MyPluginClass.This "myplugin.js" JS file will call PhoneGap.exec which in turn will call the execute method of MyPluginClass.


var CallPlugin=function(){};

CallPlugin.prototype.get=function(successCallback,failureCallback){

  return PhoneGap.exec(successCallback,failureCallback,'  MyPluginClass ','show',[ ]);

};

PhoneGap.addConstructor(function() {

//Register the javascript plugin with PhoneGap
PhoneGap.addPlugin('callplugin', new CallPlugin());



});
In detail :
successCallback : This method will be called after the plugin gets executed
failureCallback : This method will be called in case of error
We can also pass arguments to the Java class through the array

4)Register the plugin in plugins.xml with :
<plugin name="
MyPluginClass" value="
org.firstplugin.MyPluginClass"></plugin>

5)Now write the html which will execute this plugin.Click the button " Click Me To Return Name" to run the plugin


<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8" />

        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <title>

        </title>

        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />

        <link rel="stylesheet" href="my.css" />

        <link rel="stylesheet" href="photoswipe.css" type="text/css"/>

        <style>

           /* App custom styles */

        </style>

        <script type="text/javascript" src="klass.min.js"></script>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">

        </script>

        <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js">

        </script>

        <script src="cordova-1.7.0.js">

        </script>

        <script src="
myplugin.js">

        </script>

      </head>

    <body>

      

         <div data-role="page" id="home">

              <div data-theme="a" data-role="header">

                <h3>

                    Plugin Example

                </h3>

               </div>

               <div data-role="content">

                <div>

                        <a data-role="button" data-transition="fade"  id="runplugin">

                            Click Me To Return Name

                        </a>

                        </div>

                </div>

               <div data-role="footer"></div>

         </div>

<script type="text/javascript">





$("#
runplugin").click(function(e){

     window.plugins.callcamplugin.get(

function(r){alert(JSON.stringify(r);},

function(e){console.log(e)}

);

});





</script>

   </body>

</html>


 
Using this code you will be able to run a simple plugin using PhoneGap

Friday, 27 April 2012

The Underrated Source of Everything!

"Too often we are so preoccupied with the destination,we forget the journey"

With so much ado about the journey being better than the destination..and everyone striving to reach their destination..how many of us wonder about the underrated source ?

Is it just as forgotten as it is in the old saying..that no one really wonders about how it all began,what led to its inception..somehow it is all about the journey or the destination..the traveller with his tales..did he ever say what made him start.. and led to his travels..and I dont mean the thought process or how it struck him..just the trigger that made him get up and get out..that moment suspended in time when you made a path setting decision..and then promptly forgot what made you do it...nope not the thoughts,rather the act,thats what you let go of!

This seemingly uncaring way of being is a waste of some really special information,that elusive "something" which made u act! So you were thinking about something..maybe as inconsequential as asking someone out!..and then that "something" happened and you actually did it..u had your beautiful journey ,took it as it came..and then it changed..did you look back on your "something" and re-live your journey from there on,only to realise how you got to your present? Maybe it could have set the process to improve the present situation,if not then maybe suggest a new thought process,the beginning of yet another journey maybe?

So why not build a new strategy,whenever you are stuck in a muddle and you don't know how to get out of it,look back to what got you there.Remember the trigger and then relive it..and let it set the trend for some new inspiration!


Wednesday, 18 April 2012

The Golden Hues!

The Golden Hues! by Viny N
The Golden Hues!, a photo by Viny N on Flickr.

ColdFusion IceBreaker #5: JBOSS 7.1 and OpenOffice for ColdFusion cfdocument


In JBOSS7.1 , there have been major changes in the class loading behaviour of applications .Due to this we need to make a few changes to use ColdFusion's cfdocument and other OpenOffice features.Class loading is based on modules,with each module having its module.xml in which one can specify dependencies on the other modules using the <dependencies> element.

To support OpenOffice with JBOSS7.1 for cfdocument, you need to:
1)Install OpenOffice and add the installation path in the ColdFusion Admin
2)Edit the module.xml located in <JBOSS_HOME>/modules/sun/jdk/main and put the following entry there:
<path name="com/sun/star/lib/loader"/>
3)Start JBOSS with the following flag in the standalone.bat/standalone.sh.
-Djava.ext.dirs=<jboss-deployment-dir>/cfusion.ear/cfusion.war/WEB-INF/cfusion/lib/oosdk/:<java-home>/lib/ext/
(':' for unix based machines and ';' in windows)

The <dependecies> element can be used to specify dependencies on the paths and packages  from the class loader which loaded JBoss Modules.These can in turn be specified for re-exported if specified using the <export> element.The <path> element is used here to list the paths.

Adding the oosdk package to the java.ext.dir ensures that the classes of that package can be found and loaded without them being on the actual class path.

One need not add the individual jars to the JBOSS classpath or the java classpath

Click here  for MAC OSX specific instructions. These just make life easier :D.