Vijay Sarin

Thursday, November 8, 2012

Coding Conventions for Android

Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices and methods for each aspect of a piece program written in this language. These conventions usually cover file organizationindentationcommentsdeclarationsstatementswhite spacenaming conventionsprogramming practicesprogramming principlesprogramming rules of thumb, etc. Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Coding conventions are only applicable to the human maintainers and peer reviewers of a software project. Conventions may be formalized in a documented set of rules that an entire team or company follows, or may be as informal as the habitual coding practices of an individual. Coding conventions are not enforced by compilers. As a result, not following some or all of the rules has no impact on the executable programs created from the source code.

For Android



There is a good description of code style rules here.
If you want to enforce this rules without remembering all of them, and you are using eclipse then you can use formatting rules provided by Android team: android-formatting.xml

  1. From Eclipse, Click Window Menu.
  2. Go to Preferences.
  3. Expand Java.
  4. Click on Formatter.
  5. Click on Import and choose the XML file you saved from the above mentioned link.
  6. Hit OK and it's done...



Friday, September 14, 2012

XDebug Setup for NetBeans

  " Over Many days, I was working on php without IDE and other tools to make myself understand the core thoroughly. And Now, I have Shifted to NetBeans IDE and I was looking to get a Debugging Tool. By default NetBeans Provided xDebug support but it was quite hard to setup things as a beginner. But my never give up Attitude gave me what I need. So here I will explain to you what I did and got successfull in xDebug Configuration with most debugging options..."

I will be talking in terms of my work Environment. So here is it,
  • Operating System: Windows 7.0 Professional
  • PHP Server: WAMP 2.2
  • IDE: NetBeans
  • Editor Tool: Notepad++
  • PHP Version: 5.2
  • Environment/Path Variables for PHP.
Hope you have at least WAMP Installed. So lets Begin,

  1. Left Click on WAMP Server and click on localhost. Hope you don't have changed the default localhost page.
  2. Now Click the phpinfo() link and check if xDebug is enabled in it. Just have a Search on the Page from the Browser (Control + F does the trick).
  3. Have a fully Copy of the phpinfo() page (Using Control + A followed by Control + C does it for us. The Most used keys by a developer i say...).
  4. Hoping the Contents are still in ClipBoard, you may now open the xDebug Wizard from your Browser. So open a new tab and type http://xdebug.org/wizard.php.
  5. Now click on the Analyze Button and wait for the Instructions.
Hope you all got something similar to this,


Yes, Now as told in the Instructions, Download the php_xdebug-.dll file in to your PC.

Now Copy the DLL file to your php ext directory. The Path will be indicated in the Instructions as shown above. 

Now open your php.ini file which is actually inside the wamp apache directory and not inside wamp php directory. This confusion is something that normally appears. So take care of it.

In the end of the php.ini file or somewhere else as you configured, check for the section called [xdebug]. Below that add the Following lines of code.

[xdebug]
xdebug.remote_enable=on
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler=dbgp

Remember the port (9000) set up here and the host name (localhost).

Just Above the [xdebug] section, you can notice something like,

zend_extension="C:\Softwares\wamp\bin\php\php5.3.8\ext\php_xdebug-2.2.0-5.3-vc9.dll"

In Some Installations, it will be like zend_extension_ts. This is for php 5.2 or below. From php 5.3 use zend_extension only. Now Change the Path of the DLL File here to the one we downloaded and copied inside php's ext directory of WAMP server.

Once this is done, setup is done for WAMP.

Now to setup NetBeans,
  1. Open NetBeans
  2. Go to Tools. 
  3. Go to Options.
  4. Open Debugging Tab in the Options Window.
  5. Set Debugger Port to one which we gave in php.ini file.
That's it, setup is done. Now restart the machine and try debugging. Just Click on debug project button in NetBeans by adding some breakpoints and the control will be all yours.

Before getting Excited, Windows 7.0 Users must make sure User Access Control is Disabled. You can Disable it from 

Start-->Control Panel-->Action Center-->On the Left Pane, Click Change user account control settings-->Set the Drag Bar to Never Notify. Or else, you will be notified that "php.exe is not recognized as an internal or external program" when tried to debug. Windows XP users and Linux Users make sure you have necessary permissions (say 777 in linux) for the directory where you install wamp. 








Saturday, September 8, 2012

Routing in Zend Framework using INI File

Zend Framework Supports Routing of your requests in a way you wanted. Its Immense Flexibility is what makes me think as the best frameworks i have ever come over.



The Routing can be done in many ways. Each has its own advantage and disadvantage. Here i will be explaining about routing using a ROUTING TABLE which is our INI file. So as a Beginning we will be creating an INI file named routes.ini inside the configs directory of your APPLICATION_PATH. We will put the contents to this later.

Now Open your Bootstrap.php file inside APPLICATION_PATH. In this Add a Method called _initRoutes() which will read the INI File on Application Start and will render routes to the router.


            public function _initRoutes() {
            try {
                    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
                    $router = new Zend_Controller_Router_Rewrite();
                    $router->addConfig($config, 'routes');
                    Zend_Controller_Front::getInstance()->setRouter($router);
                 } catch (Exception $e) {
                    print_r($e);
                    exit;
                 }
            }
            
This is one way. Here we used Rewrite Module of the Apache efficiently. We can also implement in a way as Suggested by ZF Official Documentation. This is it,

            public function _initRoutes() {
                try {
                    $frontController = Zend_Controller_Front::getInstance();
                    $router = $frontController->getRouter();
                    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini','routes');
                    $router->addConfig($config, 'routes');
                    Zend_Controller_Front::getInstance()->setRouter($router);
                } catch (Exception $e) {
                    print_r($e);
                    exit;
                }
            }
            
Now we have setup our initialization. Next is to create Routes inside the ROUTING TABLE. So now open routes.ini and add the following code assuming that we have a controller called index with action named contactview which will take us to contact page when requested like www.example.com/contact.


            [routes]
            contact.type = "Zend_Controller_Router_Route_Static"
            contact.route = "contact"
            contact.defaults.controller = "index"
            contact.defaults.action = "contactview"
            
Now start your Server which ever it is (For me it's WAMP) and go to your Browser and type in www.example.com/contact. I hope you will change the domain part with which ever is yours. Or Else just have a look in to my Virtual Hosts Setup Reference Guide Posting for further Info.

That's It, Its Contact Page opened. Wow, Amazing right? This will add Security to your application whatever it is.

Thursday, September 6, 2012

Virtual Hosts Setup for PHP Web Application

NOTE: I will be talking in terms of Apache web Server in Windows using WAMP.

Zend Framework works perfectly well with other web servers -- including Microsoft Internet Information Server, lighttpd, nginx, and more -- but most developers should be famililar with Apache at the minimum, and it provides an easy introduction to Zend Framework's directory structure and rewrite capabilities.


To create your vhost, you need to know the location of your httpd.conf file, and potentially where other configuration files are located. Some common locations:
  1. /etc/httpd/httpd.conf (Fedora, RHEL, and others)
  2. /etc/apache2/httpd.conf (Debian, Ubuntu, and others)
  3. /usr/local/zend/etc/httpd.conf (Zend Server on *nix machines)
  4. C:\Program Files\Zend\Apache2\conf (Zend Server on Windows machines)
Within your httpd.conf (or httpd-vhosts.conf on some systems), you will need to do two things. First, ensure that the NameVirtualHost is defined; typically, you will set it to a value of "*:80". Second, define a virtual host:


There are several things to note. First, note that the DocumentRoot setting specifies thepublic subdirectory of our project; this means that only files under that directory can ever be served directly by the server. Second, note the AllowOverride, Order, and Allowdirectives; these are to allow us to use htacess files within our project. During development, this is a good practice, as it prevents the need to constantly restart the web server as you make changes to your site directives; however, in production, you should likely push the content of your htaccess file into your server configuration and disable this. Third, note the SetEnv directive. What we are doing here is setting an environment variable for your virtual host; this variable will be picked up in theindex.php and used to set the APPLICATION_ENV constant for our Zend Framework application. In production, you can omit this directive (in which case it will default to the value "production") or set it explicitly to "production".
Finally, you will need to add an entry in your hosts file corresponding to the value you place in your ServerName directive. On *nix-like systems, this is usually /etc/hosts; on Windows, you'll typically find it in C:\WINDOWS\system32\drivers\etc. Regardless of the system, the entry will look like the following: 


 Start your webserver (or restart it), and you should be ready to go.

Tuesday, April 3, 2012

Remote Debugging for Mobile Browsers



" Normally HTML developers don’t have browser debugging tools for devices like iPad. There are Developer Tools for Browsers like Chrome, Internet Explorer etc. For Firefox we can Install Firebug Add-On. But similar tools are not available for device browsers. Only thing available is a debugger console (I will be talking based on iPad Safari the most). This causes the loss of valuable time of Web developers. Are there any Solutions for this? "

And my answer is “weinre

Weinre is earlier developed by Patrick Mueller. Today Weinre is a part of Apache Cordova Project for Mobile Web.

Here is the full and latest documentation on weinre: http://people.apache.org/~pmuellr/weinre/docs/latest/

The project page for Cordova at Apache is: http://incubator.apache.org/cordova/

Information on weinre is available here: http://people.apache.org/~pmuellr/weinre/

Through weinre, developers’ time loss on HTML/CSS designing for mobile browsers can be reduced. You can read the information regarding weinre in the link provided. 

Since I don’t have a Mac I couldn’t try working on weinre for MAC. But as this is a JAR file, please feel free to try...

These are some of the common terms used with weinre,

  • Debug Server – The Server Application for weinre which listens on port 8080 by default and serves the developer tools.

  • Debug Client – The Client application used to debug. This app is 60% same as Chrome Developer Tools. It is on which you does your debugging.

  • Debug target – You will be debugging in iPad Safari. So that will be your Default Target.

Installation & Configuration (in Windows for Debugging in iPad Safari)

  • Download weinre-jar-1.6.1.zip file. This is an older version. Check the official site for latest. This one was a Stable release.

  • Unzip the Contents and you can see a JAR file called weinre.jar.
Download Weinre
To Start the Debug Server, run the Jar file using the Command

java –jar weinre.jar

Since you will be using remote debugging, better run with –boundHost parameter

java –jar weinre.jar –boundHost your_ip_address


Once the Debug Server is Started, open the following URL in your browser (replacing with your IP & Port Number that you configured. By default the port number is 8080)



http://your_ip_address:port_number/

In the weinre server home page that got opened, check Target Script. You can see a Script generated for you with example. Simply copy and paste it to the web page you are currently working on (I hope you have hosted your web application in some server).
Now open your page in your mobile browser. Also open the following link replacing with values wherever you find necessary, (If not showing anything, try refreshing)



http://your_ip_address:port_number/client/

The Above link will open the Debug Client provided by weinre. Now hover through the contents of your web page via the Debug Client and you can see it gets highlighted in the mobile browser.

That’s it…. Your Debugging Client is on and ready…

And also, this is an open source project, so do not hesitate to use it thinking about Licenses…