Monday, February 25, 2013

Adding photography grid lines in GIMP

In photography, the "Rule of thirds" suggests places for optimal visual interest. You divide the photo into nine equal parts, like a tic-tac-toe board. The rule says that the most important visual elements must be at the dividing lines, or along the lines of the tic-tac-toe. This is an easy rule, and helps to determine how best to crop an image, or to judge a composition.

GIMP, the free image editor, allows the user to create guide lines. These are lines shown for reference while editing an image. They do not modify the underlying image, and can be dragged out from the ruler or created using a menu item. Gimp also allows certain edits to stick to guide lines. This is useful if you are adding text, or adding layers as it makes it easy to align visual elements in an appealing manner.

I find it helpful to create rule of thirds guide lines to evaluate photographs. Rather than manually drag out guide lines, I have written a GIMP script to automatically create such rule-of-thirds guide lines. This is what the result looks like.


To use, download the rule-of-thirds script and copy it to your local scripts directory. On my machine, this is $HOME/.gimp-2.6/scripts. The exact location depends on your platform (Windows/Linux/Mac) and Gimp version. You can find out the exact location by going to [Menu] -> Edit -> Preferences -> Folders -> Scripts.

Now start gimp and navigate to [Menu] -> Filters -> Script-Fu -> Refresh scripts.

Once it is refreshed, the rule of thirds guide lines should be available under [Menu] -> Images -> Guides -> Photography.


Tuesday, February 12, 2013

Customers want control over their mobile device

Disclaimer: I'm a Google employee.

"The average consumer doesn't care about whether their mobile device is locked down"

I've heard this argument very often. However, the data just doesn't support it. Over the last week, the hacker group evasi0n released a program to jailbreak an iOS device. iOS devices (iPhones and iPads) are closed down: Apple chooses which programs can be installed by device owners. A jailbreak removes this restriction, and allows the device owner to install any program of their choice on their device.

The initial justification for such unprecedented control was that the phone network could not handle poorly written applications. This argument was bogus even when the iPhone came out. At that time, arbitrary applications could be executed on existing phones. Palm Treo and Blackberry both allowed the device owner to choose which applications to run. Not all applications were passed through careful scrutiny: the device owner could write his own application and run it on the device. With the advent of Android, this argument is even more hollow. Google sells Android devices on which the user can not just install any application, but also install any system software.

Other justifications claim that users want a third-party to control their experience and are willing to submit control of their own devices. The data suggests otherwise. The hack by evasi0n was downloaded by four million unique devices in just five days. We don't have any concrete numbers for iOS 6.x devices, but let's try to use people's estimates to arrive at how significant the five million figure is.

Apple announced the number of iOS devices in October 2011: 250 Million.
Rough numbers for devices runing iOS version 6.x: 100 Million.

So 5% of the total user base chose to jailbreak their device within the first four days. There are lots of caveats: the figures are all guess-work (the fact that the numbers are nice round figures should also make you suspicious). We don't know the exact number of 6.x devices, and Jay Freeman, the creator of Cydia, admits that many jailbreakers weren't counted because the jailbreak servers crumbled under the heavy load.

Even so, 5% of the users want control over their own device. And that's within a week of the jailbreak being available. A very conservative estimate is that another 5 million users jailbreak their device. So 10% of the users choose to control their own device. (This is again a rough guess, since total iOS version 6.x devices is a moving target.)

Keep in mind that the entire idea of a jailbreak is somewhat scary. I suspect most users would hesitate to download a program from an unknown website and run it on a costly phone or tablet. Put another way, the number of users who want to jailbreak their device is larger than the ones that do perform the jailbreak.

When one in ten of your users wants something, it isn't a fringe phenomenon anymore. They are sending you a strong message: that they want control over their own device.

Monday, February 11, 2013

Android framework debugging through IntelliJ

I had blogged earlier about using the Android Open Source Project (AOSP) to learn about the Android framework, and help debug your Android applications.

I missed out how easy it is to do this through IntelliJ, the other popular IDE.

IntelliJ is an IDE that was released in 2001, and has boasted an impressive feature set. It started out as a paid product and a community edition was released in 2009, along with source code licensed under the Apache License 2.0. A commercial version is still sold, and that includes enterprise support. For an Android developer's perspective, the community edition is a great product to use for all Android development. This post shows how to get set up and start debugging your project with full cross-references into the AOSP framework code.  This is a great way to learn the intricacies of the framework.

Installing IntelliJ IDEA


  1. Download IntelliJ IDEA community edition for your platform. Linux, Mac and Windows versions are available. At the time of writing this post, version 12 was the latest.
  2. Install it according to the instructions for your platform.

Set up JDK

Android development needs a Java Development Kit installed on the device. I choose to use Sun's Java 1.6, but you are free to use any JDK of your choice.
Configure -> Settings -> Project
It will say "No SDK". Click on New and select the path to your jdk. This is probably in /usr/local/sun-java-1.6, or somewhere else.


Set Android SDK

You should also have the Android SDK downloaded. If you need to download the SDK, notice that you need just the SDK for other IDEs, not the entire ADT. Say you unzip it into /usr/local/android-sdk-linux:
export ANDROID_SDK=/usr/local/android-sdk/linux

Create a new project on the previous screen.


On the following screen, create an "Android Application Module" and set the location of the Android SDK:

The project name doesn't much matter. You could create a Hello World project for the purpose of this guide. After clicking no "New", you need to set it to the path of the Android sdk, which is the location where you expanded the Android SDK zip file: ${ANDROID_SDK}.  You can change this setting later under  Menu bar -> File -> Other Settings -> Default Project Settings -> Platform Settings SDKs -> Project Structure.
This can also be set on a per-project basis. However, you should never need to change the SDK location. Instead, you can change the Build Target to the version of Android you wish to support. A single SDK location can hold multiple build targets (Starting from the earliest version of Android all the way to the latest.
At the end of this, you should have a Hello World Android project. IntelliJ supports looking up code sources if you have downloaded the sources with the SDK. To verify, run ${ANDROID_SDK}/tools/android
Select the sources for the Android build targets you need.

Debug with framework source

At this point, you are ready to debug using the framework source. Highlight any framework code (like Activity, or Context) and hit Ctrl+B. It will take you to the source for that class. Sometimes documentation can be vague, and the definitive word is the source. You can also use the source to learn how canonical classes like ListView or DialogFragment are written. Looking at framework code is a very powerful way of learning good coding practices and system paradigms.

Happy hacking!

Monday, October 22, 2012

Expert C Programming: Deep C Secrets

I read a friend's copy of "Expert C Programming" many years ago, and ended up buying a copy for later reference. I didn't quite remember why I would purchase a book I had read already. Recently, I cracked open my copy for the first time: it still had the new book smell.

Oh, now I remember.

This is the most enjoyable programming book I have ever read.

The book is about corner cases and implementation-quirks of C, with a smattering of C++ thrown in. The entire book is divided into themes, and each theme is self-contained in a chapter. Each chapter talks about a quirk of the language, something that good C programmers should be aware of.  For example, one chapter talks about the differences between Kernighan&Ritchie C and ANSI C. Each idea is crisply presented, with source code and clear explanation. The chapters are littered with relevant anecdotes, historical background, and tantalizing puzzles.

Programming books can be dry and just about the facts. Recent technologies usually come with books that focus on how to get things done. While this is useful, it does not improve the appreciation of the technology. Expert C Programming was written 20 years after the development of the language. Perhaps time leads to some clarity and a light-hearted view of things.

Programming is more than just a profession: it is a creative process. It helps to have such books in your shelf: so you can them pick up and remind yourself how fun computing can be.


(Image courtesy: Amazon)

Wednesday, September 19, 2012

Android debugging using the framework source

When writing Android apps, it is helpful to be able to read the implementation of the framework to clarify a point.  This might be when the documentation is incomplete, or if you want to learn from canonical classes like ListView.

Nexus devices contain this framework code unmodified. This allows you to trace your application flow down to the framework level, either to learn how the platform works or to find a bug. Today, the cheapest Nexus device is $199, so having an additional Nexus device is an excellent tool for Android developers, even if you do most of your development for non-Nexus devices.

It is easy to get the source code from the Android Open Source Project (AOSP). You need the following:
  1. A good Internet connection. The full AOSP tree is well over 8 gigabytes of data. If you have more than one Android developer, you could download the tree for a single developer, and then mirror it locally.
  2. A fast computer. AOSP is a lot of code and Eclipse chews a lot of CPU and RAM with large projects. A computer with two CPUs and about 4GB of RAM should do perfectly, you might get away with lesser RAM.
  3. Disk space. If you choose to build the code, you're look at 30 gigabytes of disk space.
  4. Eclipse, some recent version and Sun's Java 1.6.
  5. Linux or Mac. The instructions work for Ubuntu 10.04/11.10/12.04 and some Mac version.

Here are all the steps:
  1. Set up your computer: This can be done on all developer computers simultaneously. This will install all the development tools. At the end of this, you have all the tools but no AOSP code.
  2. Download all the AOSP source: This uses your internet connection to download all the source. The AOSP source has everything going back to the earliest releases of Android. You get all source history, the framework, the open source applications, the kernel, all open drivers, everything. There are instructions on that page to set up a local mirror. Follow those steps to download the source once and then share it over your local network. This conserves bandwidth, and saves time. You might want to start this download over a weekend, depending on your network connection and other users' needs. I will assume that you are putting the source in /usr/local/aosp.
  3. Build the source. Here, you have two options. You could follow the AOSP instructions. However, if you just want to include the source in an eclipse project, those instructions are overkill. You can get a much smaller project if you follow these alternate instructions. If you have trouble with java versions, read the bottom of this post.
    $ cd /usr/local/aosp
    $ source build/envsetup.sh
    $ lunch full-eng
    $ # The following step takes time. -j<num jobs> increases parallelism.
    $ make -k -j2 sdk
    
  4. Create the Eclipse classpath. By default, we can create a project with all the Android source code. This is beneficial if you want to have access to all the AOSP code for reference. If you want just the framework, you can reduce the size of the project significantly. To do this, first create a file in the directory containing the following:
    $ cd /usr/local/aosp
    $ cat excluded-paths 
    ^external/.*
    ^packages/.*
    ^cts/.*
    ^dalvik/.*
    ^development/.*
    ^prebuilts/.*
    ^out/.*
    ^tools/.*
    ^sdk/.*
    ^libcore/.*
    ^gdk/.*
    ^hardware/.*
    ^device/.*
    
    This file allows you to reduce the size of the Eclipse project, which improves Eclipse's performance significantly. Now, we can generate the Eclipse classpath as follows:
    $ cd /usr/local/aosp
    $ ./development/tools/idegen/idegen.sh 
    Read excludes: 3ms
    Traversed tree: 781ms
    $ ls -l .classpath
    -rw-rw-r-- 1 user group 16938 Sep 18 21:50 .classpath
    
  5. Create the project in Eclipse. First you need to start Eclipse with increased heap size and virtual memory:
    $ eclipse -vmargs -Xms128m -Xmx512m -XX:MaxPermSize=256m
    
    Now, create a new Eclipse project (File -> New Java Project -> Next). In the dialog, under the "Libraries" tab, click the "Add Library" button -> Add System Library -> Add JRE system library. This will help resolve the references to core libraries like Integer and String. Adding the system library is not required, but it reduces the number of syntax error Eclipse finds with the framework. Click "Finish" when done.
  6. Done! Try your setup by searching (Ctrl-Shift-T) for FragmentManager. You should be able to see its source code, and navigate through its code. Some handy commands are: Ctrl-Shift-G to look for references of a class, and F3 to look for a method's implementation.

JDK version pain: You might encounter a problem with JDK versions. You need Sun Java 1.6 to compile the AOSP source code, while your Eclipse setup might require a different version (OpenJDK?). One solution is to use sun-java only to compile the AOSP, and switch back to the previous version after the compilation has finished. On Ubuntu, this is done with the commands shown below. Select the number that corresponds to sun-java before the compilation, and run these commands after running idegen.sh to switch back to your previous version of jdk.
$ sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-6-openjdk-amd64/bin/javac   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-amd64/bin/javac   1061      manual mode
* 2            /usr/local/sun-java-1.6/jdk/bin/javac         1         manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/lib/jvm/java-6-openjdk-amd64/bin/javac to provide /usr/bin/javac (javac) in manual mode.
$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
* 2            /usr/local/sun-java-1.6/jdk/bin/java             1         manual mode

Press enter to keep the current choice[*], or type selection number: 2

Monday, August 27, 2012

Xmodmap: a quick guide

If you use a variety of Linux/Unix machines and are confused between their different keyboard layouts, it might help to learn the xmodmap utility.

Xmodmap allows you to read the keyboard and mouse pointer bindings and to set them. You don't need root permission, and you can modify these without restarting the X session. This is very useful if you use different keyboards, or want to use the CAPS LOCK key for something useful.

The easiest way to use the xmodmap command is to print out its current config. Run
$ xmodmap -pke

This prints out all the keycodes that are assigned. Here is a sample line:
keycode  24 = q Q U094C U0914

This says that 
  1. Keyboard code 24 corresponds to the 'q' key. Pressing the key by itself without any modifiers produces the 'q' key.
  2. If you hold shift while pressing it, it produces the 'Q' key.
  3. If you hold the mode_switch key while pressing it, it produces the Unicode 094C character (in hex)
  4. With the mode_switch and the shift, it produces the Unicode 0914 character (in hex).
The mode_switch key is used on non-US keyboards to produce non-English text. If you don't know what it is, you don't need it.

But you do use the shift key. How do you know which key that is? Run:
$ xmodmap -pm

That command tells you what the modifier keys are. Looking for the 'shift' modifier, you can see that it is mapped to two different keys:
shift       Shift_L (0x32),  Shift_R (0x3e)

They have hex keycodes 0x32 and 0x3e. 0x32 is 32 in hex which is 50 in decimal. If you search for keycode 50 in the output of 'xmodmap -pke', you will see the following line:
keycode  50 = Shift_L ISO_Prev_Group Shift_L ISO_Prev_Group

So that's the left shift key.

All these keys can be remapped. The most common use is to remove a key you do not use, like the CAPS LOCK key. That is the 'lock' modifier. On my keyboard, the lock modifier is:
lock          Caps_Lock(0x42)

It is assigned to the Caps_Lock key. Let's remove this binding. To remove bindings, you can write commands as arguments to xmodmap -e, and you can supply many commands in succession, as follows:
$ xmodmap -e "command1" -e "command2"

To remove the Caps_Lock binding, we will remove the 'lock' modifier from it.
$ xmodmap -e "remove lock = Caps_Lock"

If that didn't produce any output, it was successful. Validate by running "xmodmap -pm" again. Now, the lock line should look like
lock

Now Caps_Lock doesn't produce a shift lock anymore. You can also verify by opening a text window and pressing the caps lock key. It will not do anything.

Now, let us put the key to good use: how about assigning it to control? Run:
$ xmodmap -e "add Control = Caps_Lock"

Great, now your caps lock is an additional control. We can combine these two commands:
$ xmodmap -e "remove lock = Caps_Lock" -e "add Control = Caps_Lock"

Easy remapping, and it works independent of the window manager, the keyboard type, and the version of Linux.

In case you want to find what key code a specific key produces, run the 'xev' program. It tells you in great detail which keystrokes are pressed and released, and also all mouse events.  Here is a sample line:
KeyPress event, serial 36, synthetic NO, window 0x3e00001,
    root 0x15d, subw 0x0, time 1355682, (152,6), root:(783,52),
    state 0x4, keycode 38 (keysym 0x61, a), same_screen YES,
    XLookupString gives 1 bytes: (01) " "
    XmbLookupString gives 1 bytes: (01) " "
    XFilterEvent returns: False

I pressed a key which produces keycode 38. Using this method you can narrow down on an unused key and put it to good use.