Sunday, December 9, 2012

Two more days @ AnDevCon_IV

I guess driving on 101 was still better than flying all the way from Japan or India or Russia.

The sond day was much more eventful than the first day.
I started my second day with 'Inside Android User Interface' by Karim Yaghmour. He explained how Android display stack is different than Linux display stack, how is WindowManager used to add views and also touched upon StatsuBarManagerService. Do you know you can send commands to any system service from adb shell? I though that was pretty neat especially during debugging.
For example, if you want to expand the status bar, you can do this on adb shell

$ service call statusbar 1

In order to close the notification tray which was expanded by previous call, send this command

$ service call statusbar 2

The general form of the command is service call

The highlight of the second day was a keynote from Romain Guy and Chet Haase. They talked about what's new in 4.1 and 4.2, nothing much different from their Google IO session except for recent 4.2 enhancements. But nevertheless, it's always fun to listen to these Android Ninjas.

Then it was time to meet people at Exhibition hall, umm ... actually it was time to collect swags and t-shirts. Many of the companies were either focused on testing or monetizing apps. The only interesting spots were Google booth for Android Robots, Amazon booth because it was Amazon, Qualcomm booth with MDP and SnapDragon demo and Immersion tech booth because this was something new for me - use of Haptics tech with Android hardware - can make apps more accessible. Got a chance to ask Romain Guy a few questions and the discussion was quite insightful.

This was an interesting slide from Qualcomm's lightening talk.

On Thursday, the lunch was special too. Robin Jeffries, a Google engineer and a member of the Board of Advisors for the Anita Borg Foundation hosted Women in Android lunch. It was nice to see more geek chics in the room and more importantly Robin's talk was quite inspiring.

After lunch, I attended 'Optimizing Android UI - Tips and Tricks' by Jason Ostrander. He is the author of  Android UI Fundamentals book. He quotes many examples from his book and overall it was a good session. He touched upon Handlers and Services to offload work from main thread, use of Loaders for grabbing data from network sources, ViewHolder pattern to create smooth scrolling lists and many more ...

This was followed by coffee and ice-cream break and a chance to collect more goodies from exhibition hall. And then it was time for yet another keynote, by Mike Shaver, Director of Mobile Engineering at Facebook. The theme of his talk was 'scale' and this was by far the most entertaining talk. He glossed over the challenges posed by Android fragmentation, more than 900 devices and many more coming up with Gingerbread ...

One more day; four more sessions and a hope to win Nexus 7 -

I was really looking forward to attending 'Extending the Android Vibrate Function for Games' especially after the brief discussion I had with Bob at the exhibition hall.
Android supports an API to specify the duration of Vibrator. There is no API to control the frequency, voltage or any other parameters. Immersion Tech. focuses on haptics technologies and in their efforts to promote haptics tech adoption, they have developed a library of over 120 vibration effects. What was more interesting to me was the possibility of making some apps more accessibleusing these various effect. In their presentation, they proposed the idea of Haptics Clock which anyone can use to check time even without looking at the screen.

I have this idea, not sure if it's feasible. Assume a message is written in Braille script and someone does not want it be read to him aloud. In such cases he could touch the screen, and every dot will generate a distinct vibration pattern and it would be possible to read any arbitary message.

Anyways, it's free to use by developers and it's really easy to use. They work with OEMs in order to include more precise motors and implement low level APIS to control the motor. Apparently, OEMs are interested in this technology as it allows them to distinguish their decives based on enhanced Game experience ... Samsung S3 & NOOK have such motors and support their library natively,

Later they announced the winners of various raffles. Oh btw someone gave away an iPad!

So the next session was a long one. 'Battle tested patterns in Android Concurrency'. He had lot of slides and a lot of information to share.
In a nutshell, here are main points

  •  get off the main UI thread
  •  don't leak activity references 
  •  don't just use background threads as they keep running even if Activity is destroed of not handled carefully
  •  Same applies to Asynctask, tehy are not tied with Activity Life cycle
  •  Loaders are aware of Activity Life Cycle - hence post data to the running insatce, managed by LoaderManager - much better than AsyncTask
  •  Loaders cannot throw exceptions which an activity can catch
  •  keep thread count = # cores - 1 => leave some CPU for UI activities!
  •  If you have to, use old school counterlatches to achieve synchronization
  •  use ExecutorService whch has its own threadpool (plain old Java)
  •  LocalBroadcasts can be used as a local data exchange method between loaders and activites - super cheap (work like callback registry)
After this information overload, it was time to sip some coffee. The last session I chose was - 'Intro to RenderScript'. Had no idea about RenderScript so this was a good 101 session.

Google gave away some (only 4 or 5) fur toys, was happy to grab one :)

Overall it was a great experience!

First day @ AnDevCon_IV

Last week I had a great time at AnDevCon_IV. Three days filled with Android, learned interesting stuff, met some smart people, connected with old buddies and shook hands with some famous personalities in Android world.

AnDevCon 4, a four day technical conference started on Dec 4 at Burlingame, California. They had hands on workshops on day 1, including Android Bootcamp, Embedded Android and Facebook apps for Android.

The second day was kicked off by a keynote by Ethan Evans, Director of App Developer Services, Amazon. He discussed the performance and impact of HTML5 apps and also mentioned how Amazon achieved the better performance by using both HTML5 and native code in their apps. He also explained blended HTML5 used in their App store hybrid architecture. "Updates without requiring Upgrades" was the most promising feature of HTML5, he said.

After his keynote, the actual classes began. It was a daunting task to choose between 70+ classes and many interesting topics were cross listed which made the selection even tougher. 'Hacking APKs for fun and profit (mostly fun)' sounded fun but I instead headed to "Dive into Android Fragments" session by Donn Felker. After he explained the basics of Fragments and fragment transactions, he talked about RoboGuice. It's a lightweight library that uses Google Guice library for dependency injection. It's not recommended for games or performance centric apps, but it does make code simpler and cleaner. He also talked about other open source projects -

  • Otto from Square - another dependency injection framework
  • ActionBarSherlock - extension to Android's support library to facilitate the use of ActionBar on all versions of Android
  • Viewpageindicator - adds pagination to Android code, especially useful when using multiple fragments or panes.
Android Bootstrap might be useful if you are planning to use above technologies, the project is built using many open source projects widely used in practice. 

Here is a link to Donn's Github project that he used to explain fragments.

Mike Wolfson presented two part series on Android Developer Tools. Google IO videos on this topics are much more informative so I will skip my notes on this session except for one piece of information.

HAXM - Intel® Hardware Accelerated Execution Manager  It's hardware assisted hypervisor, can greatly reduce the time required to boot an android image on an emulator. You need to download the driver for haxm and also Intel Atom image to use with the emulator. Currently it supports only ICS and JB images.
He also has a repo of all different types of AVDs, which can come handy during testing.

Marko Gargenta presented 'Architecting Android Apps' where he explained basic building blocks - activities, services, providers and receivers, as well as intents that glue them all together.

The post lunch sessions were the most engaging ones and were packed with useful information, so keeping myself awake was not hard.

Alex Gargenta presented 'Deep Dive into IPC/Binder Framework'. You should check out this video. He did a great job explaining binder concept, starting with the kernel level binder driver all the way up to application level code including discovery mechanisms, threading context, security implications etc.

I concluded Day 1 with "Mastering Android Touch system" session presented by Dave Smith. He provided in-depth look at how Android passes touch events between view hierarchies, why should you care about ACTION_DOWN even if you are interested only in ACTION_UP or ACTION_MOVE, what it means to handle a particular event etc. Code examples will make the theory simpler - here is the project he used.

Oh btw, driving on 101 sucks and it doesn't matter which direction you are travelling or if you are in hov lane or not, it sucks!

Saturday, September 1, 2012

Building Your First Android Application

Let's build our first application. Launch Eclipse. click New Android App Project  in the toolbar or select New Project under File menu, then select Android Project. Choose you Application name and package name. Uncheck "Create custom launcher icon" and click next
Next, select Blank Activity
And finally, name your activity. The Title is the Application Title shown in Android launcher screen. Click Finish. 

ADT will generate various folders and files and you will see the layout graphical editor oped for you. This is how your app will appear when launched on device. The ADT has already added the bare bone required to run your app on Android platform.
For this excercis, we will use an android virtual device to run our first app. An AVD is a device configuration for the Android emulator that allows you to model different devices. To create an AVD:
  1. Launch the Android Virtual Device Manager:
    1. In Eclipse, click Android Virtual Device Manager from the toolbar.
    2. From the command line, change directories to/tools/ and execute:
      ./android avd
  2. In the Android Virtual Device Manager panel, click New.
  3. Fill in the details for the AVD. Give it a name, a platform target, an SD card size, and a skin (HVGA is default).
  4. Click Create AVD.
  5. Select the new AVD from the Android Virtual Device Manager and click Start.
  6. After the emulator boots up, unlock the emulator screen, you will see the app running.
In next post, we will learn about application building blocks and we will also examine project directory structure to better understand the app components, Till then play with your Android AVD. It runs the entire android stack and also comes with standard stock apps such as Maps, Browser, Contacts etc. 

Thursday, August 30, 2012

Add Git Branch to Bash prompt and a git goodie - SourceTree

Adding git branch to your bash prompt has become easier than before.

Edit your ~/.bash_profile to add following code

# Set git autocompletion 
if [ -f /usr/local/git/contrib/completion/git-completion.bash ]; then
  . /usr/local/git/contrib/completion/git-completion.bash


if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion

PS1='\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

It not only adds branch name but also indicates the current status of your project. For example, when you add files to staging area, + is added after branch name. 
If you make further changes to staged files, it adds * after branch name to indicate there are unstaged files.

The above script also enables Git autocompletion which will help saving some keystrokes.

If you prefer GUI tool and even if you prefer command line, SourceTree is a must have tool for Mac users. It shows you all branches, their commits, the changes. You can do everything using it.

For newbies, it will make using git a lot more fun!

Tuesday, August 28, 2012

Getting Started with Android App Development

The beauty of Android development is that you are not restricted to any particular development environment. You can use any operating systems and any IDE you like. But since we will be writing Java code, Eclipse is a natural choice.

All you need are these 4 things

- Java SDK 6
- Eclipse IDE
- Android SDK
- Eclipse ADT plugin
- git (optional but recommended)

Get Java SDK

If you don't have Java SDK, getting it is simple. Download it from

Download Eclipse IDE

Download Eclipse Classic from
If you are using Mac or Linux, and not sure if you are running 64bit or 32bit machine, inside Terminal run this command
$ uname -a
The output will tell you which eclipse binary you should download. Unzip and you have Eclipse.

Download Android SDK & Tools

Next step is to download Android SDK from
Unzip the file you just downloaded. You should see android-sdk-mac folder. Copy it to a location where you want to download all other Android Development tools. Using terminal, navigate to tools folder inside android-sdk-mac and run android program
~/dev/android-dev/android-sdk-macosx/tools $ ./android  
This opens up Android SDK Manager. It shows you all installed components, the updates and also lists all available versions of Android SDK. This is where you will select what you need to download that includes, plain SDK, sample code, SDK source code, system images, Google APIs and also vendor specific components. If that's too much to take in, make sure you select for both Android 4.1 and Android 4.0.3
  • Documentation from Android SDK
  • SDK platform
  • Samples for SDK
  • ARM EABI system image
And under Tools category, select
  • Android SDK Tools
  • Android SDK platform-tools
And click install packages, next Accept all and let it finish.

The final step is to install Eclipse ADT plugin which makes Android development tools (mode on that later) part of the IDE for easy access. 

Download the ADT Plugin

  1. Start Eclipse, then select Help > Install New Software.
  2. Click Add, in the top-right corner.
  3. In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location:
  4. Click OK.
    If you have trouble acquiring the plugin, try using "http" in the Location URL, instead of "https" (https is preferred for security reasons).
  5. In the Available Software dialog, select the checkbox next to Developer Tools and click Next.
  6. In the next window, you'll see a list of the tools to be downloaded. Click Next.
  7. Read and accept the license agreements, then click Finish.
    If you get a security warning saying that the authenticity or validity of the software can't be established, click OK.
  8. When the installation completes, restart Eclipse.

Configure the ADT Plugin

After you've installed ADT and restarted Eclipse, you must specify the location of your Android SDK directory:
  1. Select Window > Preferences... to open the Preferences panel (on Mac OS X, select Eclipse > Preferences).
  2. Select Android from the left panel.
  3. You may see a dialog asking whether you want to send usage statistics to Google. If so, make your choice and clickProceed.
  4. For the SDK Location in the main panel, click Browse... and locate your downloaded Android SDK directory (such asandroid-sdk-windows).
  5. Click Apply, then OK.

Install Git

Git is an open source version control system. It works best for small projects as well as massive projects like Android platform itself. It will help you maintain different versions of your source code. In case you would like to download the Android source code, you will require git. It's very easy install.
Download it from below site and install it using package manager.

We will talk s more about git once we start developing actual applications. 

Next, we will talk about some Android Development Tools and will run our first Android app.

Monday, July 30, 2012

rbenv - A Simple Ruby Version Manager

rbenv is a new kid on the block! A simplistic ruby version manger which manages ruby versions and lets you switch between different versions easily and that's it.

Unlike rvm, it does not,

  • Have a configuration file - well there is nothing to configure other than ruby version
  • Install Ruby - you are on your own
  • Manage gemsets - Bundler is your buddy
It's good to have no frills version manager, but if you like rvm, don't switch to rbenv! You will surely miss rvm. 

How to Install Ruby On Rails on Mac - Updated

This post will guide you to prepare your Mac OS to install Ruby on Rails using rvm. It will also show you how to create a sample rails app to enjoy all the efforts involved in installation.

There is a good news and a bad news. First the good news. Ruby comes preinstalled on all Mac's.
To check which version you have, open up Terminal and type
$ ruby -v
The most current version as of July 2012 is 1.9.3.
Now the bad news, the version you have may not be the version you want and it's a pain to manage different ruby versions manually. Ruby Version Manager (rvm) does great job so it carry the burden of installing and managing different versions.

Step 0. Install git and gcc 

To check if you already have git, inside Terminal run this command
$ git --version
If you don't have git installed, download the recent installer from
Open the .dmg file and you will find the installer package. Follow the instructions to install git.

Ruby is compiled locally using gcc, hence make sure you have gcc installed.
$ gcc --version
Gcc does not come preinstalled on Mac. You need a developer account to install it separately.
  • If you are running Lion, login using Apple ID and download Apple's official Command Line Tools for Xcode. Simply double click .dmg file and then the enclosed .pkg file to start the installation process. 
  • If you on Snow Leopard, you can download Xcode (4 GB in size) and install it, which includes gcc. The easier option would be to download the unofficial Gcc compiler and related tools from osx-gcc compiler downloads page.
Step 1. Install RVM

Open up Terminal and execute this command -
$  curl -L | bash -s stable
It downloads and runs RVM installation script.

If curl complains about certificates, try with -k option
$ curl -kL | bash -s stable
If you run into other issues, make sure you have git installed. Don't worry, it's easy to setup.

Once rvm installtion is completed it shows the files it modified. rvm is now ready to be used. Close the current Terminal window and open a new one. Test rvm using
 $ rvm --version
You should see a version number with no errors. In case it was not recognized as a command you will have to load the rvm script into the shell so that shell can recognize 'rvm' as a valid command. You do that by executing following command verbatim.
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && "$HOME/.rvm/scripts/rvm" ' >> ~/.bash_profile

Step 2. Install Ruby using rvm
We will install 1.9.3 as part of this tutorial. To get a feel of what rvm is capable of installing, run this command
 $ rvm list known
It lists different versions of Ruby packages that can be installed and managed by rvm.
To install ruby use this format rvm install for example, 
 $ rvm install 1.9.3
It downloads the source code for ruby 1.9.3 and then compiles it locally. If there are any errors, rvm will also tell you how to fix them. Once done, it displays "Install - #complete"

To verify, list the installed versions using
 $ rvm list
It should list the installed version like this -

~$ rvm list

rvm rubies

 * ruby-1.9.3-p194 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

and now to double check, verify the ruby version itself using
 $ ruby -v
Now set it as your default ruby version.
 $ rvm --default 1.9.3
Optionally, you might want to generate Ruby core documentation by running
 $ rvm docs generate

Step 3. Install Rails and other Gems
It's simple. Run this command and it will install rails and all other dependencies which work with your ruby version. It also install the docs for al the gems. Takes some time ...
 $ gem install rails
To install SQLite 3, first verify you have sqlite3 installed on your system by typing
 $ sqlite3 -version
It will respond with a long version string. Now install ruby binding for sqlite using
 $ gem install sqlite3

Step 4. Ruby On Rails Test Drive
Now that you have all the required components in place, let's create a your first Ruby on Rails app to make sure everything is working smoothly. Again inside a Terminal window,  navigate to a directory where you want to create your new app. ~/rails-app for example. Run these commands one by one
$ rails new todos
This will create the template application with model, view and controller and some static files.
$ cd todos
Tell rails to generate scema for your todos app database and also generate controller and views for standard actions.
$ rails g scaffold todo name:string completed:boolean
Run database migration
$ rake db:migrate
Then tart the rails WEBrick server on default port 3000
$ rails s
Finally, point your browser to http://localhost:3000/ If you see a welcome page, your app is running fine. Congratulations! Your todos can be managed at http://localhost:3000/todos

When you are done, you can stop the server by typing CTRL+C in the shell window where you started it earlier.

Extra Credit: More rvm commands

To revert to system version of ruby, i.e. pre-rvm ruby use this,
$ rvm system
To go back to 1.9.3 or any other version, use
$ rvm 1.9.3
To get the list of all rvm commands,
$ rvm usage

Hope it helped!

Sunday, February 12, 2012

Sublime Text 2 - I'm loving it!

Leaving Notepad++ was not easy. The recommendation for TextMate was loud and clear. But it didn't impress me and the search continued. There came this new kid on the block - Sublime Text 2. I must say it's really Sublime. If you haven't tried yet, download it now - Not just mac users, but my ubuntu friends are also leaving their "vi" and gEdit for Sublime Text 2.

My favorite features -

  • multiple cursors - it's incredibly useful!
  • column select
  • crazy fast file switching - include 'Go to anything' search (cmd + p)
  • context specific search and replace
  • side by side multi-pane editing (like :vsp in vim)
  • auto completion 
  • bookmarks (like in vim)
  • distraction free mode
  • Snippet support 
  • Zen coding support 

And there is a package manager which makes installing new packages a piece of cake.

Terminal command to open up Sublime Text 2
It's simple, just create s symlink and add it to your PATH
ln -s "/Applications/Sublime Text" ~/bin/subl
Edit your ~/.bashrc or ~/.bash_profile to add this -
export PATH

Install package manager-

Open Sublime text 2, you can use subl command in your Terminal now.
Open the console by clicking View -> Show Console  and paste this in the console and hit enter. import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen(''+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'
Restart Sublime Text 2 to complete the installation process.
Presse cmd + shift + p to access the command pallet and then type 'Install packages" and hit Enter.
This will open up a list of all available packages. Select the package from the list and Enter. That's it. It will download the package and install it for you.

Here is the list -

A very quick way to create new folders and file - cmd + alt + n -
Happy Coding!

Print Screen in Mac

When I opened my macbook, the first thing I wanted to do was to take a screenshot and email it to my friend. But there is no Print Screen? First Google result - cmd + shift + 3. Can it be more obscure? 
Sure, it has lot of good functionality built in but a dedicated key or fn+something is more user friendly, isn't it?

Here are all options - 
  • Cmd + shift + 3 -- Takes screenshot of the entire screen and saves it as a .png on the desktop
  • Cmd + shift + 4 -- Lets you select an area and then saves it as a .png on the desktop
  • Cmd + shift + 4 + space -- Lets you take a screenshot of just one window. and saves it as a .png on the desktop

To copy the contents to the clipboard, add control key with above combinations. e.g 
  • Cmd + +ctrl + shift + 3 -- Takes screenshot of the entire screen and saves it to the clipboard
Command Line -
For Terminal lovers, screencapture command can be used and it's especially useful for automated scripts. 
$ screencapture -iW ~/Desktop/screen.jpg

File Format 
Mac OX X 10.4 and later versions use .png as a default file format to save the screenshot but this can be changed by using the below command inside Terminal

$ defaults write type image_format
$ killall SystemUIServer

where image_format can be jpg, tiff, pdf, png, bmp or pict. The second command causes SystemUIServer to restart so that the change takes effect immediately. 

There is also a native app - 
If you don't like so many keys for just a screenshot, you can also use an app called Grab, bundled by Apple by default, to grab a screenshot and view it using Preview application

So much for a simple print-screen!

Moom - Snap for Mac

If you miss the Snap feature of Windows 7 where you can quickly resize any window to occupy half of your screen space, here is a must have app from ManyTricks. It's called Moom -

It does a fantastic job of resizing and aligning multiple windows with simple mouse control. It also has keyboard mappings if you like. I find it extremely useful especially while working with multiple Terminal windows, and also while working on my Website.