Friday, October 25, 2013

Socket Connection in Android - Bi directional (Simple and Easy)

I have written a class for socket programming.This class can be used for creating both server and client socket.you don't have to write classes for server and client separately.

This class contains two constructors.

for Server :

SocketConnection(int Port,final Handler ReturnHandler)

Port : server port where the socket is open (default is 8080)
ReturnHandler : handler for handling the received value from client

 for Client :

SocketConnection(String ipAddress,int Port,final Handler ReturnHandler)

 ipAddress : Ipaddress of the server where the client have to connect.
ReturnHandler : handler for handling the received value from server


Functions :

ReadValues : This function is used to Read the values and do what with the received value
the Red line is where you have to change accourding to your need (I use send Empty message function and integer value). This function is called after the connection is accepted

public void ReadValues()
    {
        Thread threadRead = new Thread(new Runnable() {
           
            @Override
            public void run() {
                // TODO Auto-generated method stub
               
               
                try {
                 BufferedReader in = new BufferedReader(new InputStreamReader(m_SocClient.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    m_ReturnHandler.sendEmptyMessage(Integer.parseInt(line));
                }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        threadRead.start();
       
    }


WriteValues : This function is used to send values to client/Server from Server/Client.I use integer as input parameter you can use any parameter


    public void WriteValues(final int Number)
    {
        Thread threadWrite = new Thread(new Runnable() {
           
            @Override
            public void run() {
                // TODO Auto-generated method stub
                if(outputValues==null)
                {
                    try {
                        outputValues = new PrintWriter(new BufferedWriter(new OutputStreamWriter(m_SocClient.getOutputStream())), true);
                       
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                outputValues.println(Number);
            }
        });
        threadWrite.start();
    }


Monday, October 21, 2013

MenuBuilder jQuery Plugin

This is my first jQuery Plugin.It allows users to build a Menu like structure using 'ul' , 'li' & 'a'.
The menu can be created from a jSON Object.

This supports unlimited number of submenus.you can use this if you have dynamic menus for each page.you can provide the jSON Object in each subpages and load the menu accordingly.

you can combine this with any jQuery Menu Plugin ( I use jQueryUI ).

to get a Menu structure like this

  • Menu A
    • Menu Aa
  • Menu B
    • Menu Ba
    • Menu Bb
      • Menu Bb1
        • Menu Bb11
        • Menu Bb12
      • Menu Bb2
  • Menu C
    • Menu Ca
    • Menu Cb

 the jSON object have to be like below


var jSONObj = {
            "menu": [
                {
                    "name": "Menu A",
                    "url": "~/Demo/page1",
                    "menu": [
                        {
                            "name": "Menu Aa",
                            "url": "~/Demo/page2",
                        }]
                },
                {
                    "name": "Menu B",
                    "url": "",
                    "menu": [
                        {
                            "name": "Menu Ba",
                            "url": "~/Demo/page3",
                        },
                        {
                            "name": "Menu Bb",
                            "url": "~/Demo/page4",
                            "menu": [
                                        {
                                            "name": "Menu Bb1",
                                            "url": "~/Demo/page5",
                                            "menu": [
                                                        {
                                                            "name": "Menu Bb11",
                                                            "url": "~/Demo/page6",
                                                        },
                                                        {
                                                            "name": "Menu Bb12",
                                                            "url": "~/Demo/page7",

                                                        }
                                                            ]
                                        },
                                        {
                                            "name": "Menu Bb2",
                                            "url": "~/Demo/page6",
                                            
                                        }
                                    ]
                        }
                    ]
                },
                {
                    "name": "Menu C",
                    "url": "~/Demo/page9",
                    "menu": [
                                        {
                                            "name": "Menu Ca",
                                            "url": "~/Demo/page10",
                                        },
                                        {
                                            "name": "Menu Cb",
                                            "url": "~/Demo/page11",
                                        }
                    ]
                }
            ]
        }; 


Then simply call the plugin function in the document.ready function


        $(document).ready(function () {
            $('#dvmenu').MenuBuilder(jSONObj);

        });


where  'dvmenu' is the id of the div where the menu is generated


jQuery Plugin : 


(function ($) {

    $.fn.MenuBuilder = function (jSONObject) {
        this.append(populateSubMenuItems(jSONObject));
        return this;
    };

    function populateSubMenuItems(jsonObject) {
        var items = jsonObject.menu;
        var ul = $('<ul/>');
        $.each(items, function (n, elem) {
            if (elem.menu == null) {
                $(ul).append("<li><a href='"+elem.url+"' >" + elem.name + "</a></li>");
            } else {
                var subul = populateSubMenuItems(elem);
                var subli = $('<li/>');
                $(subli).append("<a href='" + elem.url + "' >" + elem.name + "</a>");
                $(subli).append(subul);
                $(ul).append(subli);
            }
        });
        return ul;
    }

}(jQuery));



Save the plugin as jquery.menubuilder.js 



dont forget to include the jQuery file in the page :)

works with almost all versions of jQuery.

the example is shown with jQueryUI Menu

sample : http://jsfiddle.net/urEka/6/





Tuesday, October 8, 2013

Mobile Developer summit 2013 #mods

Going to attend the Mobile Developer summit 2013 @ NIMHANS Convention Centre, Banglore on 10-11th of this Month.

http://www.developermarch.com/mds/

I was interested in the old presentations of the summits.but to download those items I have to register an account.so I found out a way to download them without an account.

 this is for educational purpose only.
















Clicked on the view more button


then the page with all the presentations appeared.

Right click on the  download presentation link and click on the Inspect Element (I am using Firefox)




double click on the item and copy the selected part shown on the screen


copy the address from address bar

http://www.developermarch.com/mds/2012/report/postPresentationFiles.html

replace the underlined part and replace with the selected text


http://www.developermarch.com/mds/2012/report/downloads/MDS2012_jQueryMobile_SDavis.pdf

and paste it in the addressbar and enjoy the presentation.