Monday, August 23, 2010

Android GPS Tracking

For quite a while now i wanted to program something for the android platform, and dive into the holy world of google, but i really never had time to do any investigation on that platform , maybe just because lack of any good and reasonable project (Dell XPS M1210 Battery) .

But i got my chance now at a recent course where the assignment was to develop any Web 2.0 related, or something for mobile devices ideally in combination with Web 2.0 so two colleagues and me submitted a project to live track your way on a mobile Android device on crossingways.com, aside from live tracking we also wanted to support offline track recording with the possibility to upload the tracks later (Dell XPS M1210 Battery) .

No one complaint about the project idea, so we started developing.

Result

We finally have 4 Screens

The main screen

It is the starting point for all operations, and displays an overview of your location if gps is enabled and if location is available. Live tracking can be enabled and disabled seamlessly here (assuming your user credentials are configured properly, see settings screen (Dell Vostro 1710 Battery) .

Also a nice feature of android is the automatic switching of layouts. Suppose you have a device which supports screen rotation, on other platforms (e.g. Windows Mobile ) it's a real pain to support portait and landscape mode at once. On Android it's just defining two different layouts in predefined locations and the android framework choses the "best" layout for the device the application is running on, even when switching from portrait to landscape while the application is running (Dell Latitude XT2 Tablet PC Battery) !

The settings screen

As the name already says, enter and check your user credentials and enter the upload interval for live tracking to give the internet connection (umts or whatever) the change to sleep in these intervals to save battery power (Dell Precision M70 Battery) .

The track management screen

As you will see in the program flow later, this screen contains all tracks that are recorded on the device. Here you can delete, load (continue) and upload them. More technical details on uploading following later (Dell XPS M1330 Battery) .

Program flow

As you can see starting a new track recording is quite simple, after pushing the "Start Recording" button the "New Track" window pops up where you can enter some information about the new track. After creation the recording starts immediately with the option to pause or to stop the recording (Dell TT485 Battery) .

Because of the slow solid state memory access the received track points are not saved as they are received. After receiving a punch of trackpoints the data is written to disk. Maybe we will add an option to configure the "maximum count of uncommited trackpoints" ;-)

Implementation details (Dell XT832 Battery)

I only have picked some interesting things about the implementation if you want to see the whole source, go to the end of this section.

SOAP for Android

One of the first questions were, how to "talk" with crossingways.com. They offer a (undocumented ;-) ) webservice written in ASP.net. These webservices can be contacted by either HTTP GET (really ugly), HTTP POST (nicer, but not what i wanted) and SOAP (Dell Studio 1737 battery ) .

We decided to use SOAP. So the question was "Is there already a SOAP library for Android?" and we discovered that ksoap2 has already been ported to android! The only problem know was to get the ASP.net webservice to speak with the ksoap2 library because that's not real standard SOAP (Dell Studio XPS 1340 Battery) .

The result of that work is a nice PerformSOAPCall method.

Asynchronous Web Requests

After some first testing we figured out that the internet connection on an android device can be really slow, so we decided to implement asynchronous web requests, which don't block the user interface, well i know that is always a good idea, but we thought if it's fast enough without reasonable blocking we could leave it because threads are expensive (Dell Latitude E6400 battery) .

This raises some more problems, what if the (sending) queue gets larger and larger (filled with live tracking requests, they occur quite often) and i want to upload my recorded track NOW without waiting for all the live tracking requests to get processed (IBM ThinkPad X41 Tablet battery) ?

to solve this issue we implemented a 2-stage request queue, a low priority and a priority. The low priority queue gets only processed if no elements in the priority queue are present and if the low priority timeout is reached (defined on the settings screen). The priority queue gets processed immediately on adding a new request (it would only wait for an other element currently processed (Acer Extensa 500 battery) .

But now we are still missing one important feature --> Ui feedback. It's nice that the requests get processed in the background but the user wants to know if the track upload has finished or if it failed. This Ui feedback is not straight forward, because the methods that show the feedback (message window) or the methods that update the list element status or whatever need to be called in the same thread than the user interface is running and not in the background-connection-thread. But the android framework also has a soution for this problem. It's called AsyncTask (HP Pavilion dv6000 Battery)

You just extend that class (in this case we extended CrossingWaysConnection) and override two methods.

The first one: doInBackground

This is the method which is executed in a seperate thread without interfering the main/ui thread.

The second one: onProgressUpdate

Once publishProgress is called in the background thread, this method is called in context of the main/ui thread. But keeb in mind without locking or condition variables the background threads runs further even before this method is finished (Acer Aspire One battery) .

That's a real nice mechanism which makes the program feel faster and it's real flexible. Currently there are only a few requests implemented but it could be extended easily (Dell Latitude C400 Battery) .

No comments:

Post a Comment