Today as part of our Fun Friday we released version 0.1 of ignition, an Android library that should make your life as an Android developer much less painful. What I’d like to write about here is the module I focused on, that is the ignition-location module. Personally I started working with Android almost 3 years ago, in Android terms that means Android v1.5 - Cupcake - API level 3. It wasn’t easy to understand the framework back then, lots of documentation was missing and I spent hours digging in the source code to understand of things were supposed to work. A lot has changed since 1.5, and developing Android applications has become way easier with better documented APIs and better tools. It’s a bit weird that on Android I always worked on location-aware applications, and sadly if you have to deal with location your life is not that simple. I was literally thrilled when I watched Reto Meier’s talk on the last Google I/O, read his blog posts - on the Android Developers Blog, and on his personal blog (1, 2), and had a look at his android-protips-location project - because he showcased some really cool techniques and gave solutions to a lot of issues that I personally experienced. One issue about the sample project is that it’s not intented to be used as an Android library, so there is no easy way to integrate and all that code, and it’s a lot of code (but fair enough, that project wasn’t intented to be an Android library). So I started to think how that code could be reused.

At that time I was working on integrating Analytics in the Qype application(here’s a prezi of a barcamp session I did during the last DroidCon in London) using AspectJ, and I really liked its unintrusiveness. That’s when I thought: it would be awesome if I wouldn’t had to be worried about enabling/disabling location updates, battery consumption and other details while I’m developing a location-aware application, I just want to know the most recent known location and I don’t care about all those things! In other words, the logic that handles location should be a separate concern with respect to the logic of the application (i.e.: getting the best places nearby, the nearby checked-in friends etc.).

After spending several months adapting the android-protips-location project and adding a couple of features so that it could be used as an Android library, here is the final result:

@IgnitedLocationActivity
public class IgnitedLocationSampleActivity extends MapActivity {
    // MUST BE OVERRIDDEN OR IGNITION LOCATION WON'T WORK!
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

    // MUST BE OVERRIDDEN OR IGNITION LOCATION WON'T WORK!
    @Override
    public void onResume() {
        super.onResume();
    }

    // MUST BE OVERRIDDEN OR IGNITION LOCATION WON'T WORK!
    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public boolean onIgnitedLocationChanged(Location newLocation) {
        ...
        return true;
    }
    ...

}

Et voilà ! That’s all the code you have to write in your location-aware Activity! Under the hood the ignition-location library will:

  • enable/disable location updates
  • enable/disable passive location updates
  • return the most recent location to your application (using the onIgnitedLocationChanged() callback or a Location field with the @IgnitedLocation annotation)
  • avoid using GPS if the battery level is too low
  • automatically switch off GPS and request network updates if a location fix is not returned after a certain interval without the need for you to do anything else apart from adding the @IgnitedLocationActivity annotation to your Activity and make sure you override onCreate(), onResume() and onPause(). Many configuration parameters can be passed to the @IgnitedLocation Activity annotation to have fully customized settings.

If Eclipse is your IDE, additionally to your Android setup you must:

  1. download and install the ajdt
  2. add ignition-location as a library project (have a look here for instructions)
  3. add ignition-location to the aspect path (have a look at the image below)

If you are using Maven as your build manager you have to:

  1. add ignition-location as a dependency

    com.github.ignition ignition-location 0.1 apklib com.google.guava guava-collections

  2. add the aspectj-maven-plugin to your POM file, adding the ignition-location library to the aspect path

    org.codehaus.mojo aspectj-maven-plugin com.github.ignition ignition-location apklib 1.6 process-sources compile

One last good thing about it is that it’s open source and on github! So fork it and help us make it better.

NOTE:

If you are wondering if the ignition-location magic will slow your application down, the simple answer is: no. Reflection is not used, AspectJ’s Context binding is used instead. That means that all the magic is happening at compile time. Isn’t that cool?