Friday, January 1, 2010

Accessing GPS on Android 2.0 Platform

One of the great things about building applications for an Android device is the ability to create GPS aware applications. In a single hardware platform, using Android, it is possible to create an application that will change behavior based upon real-world coordinates.

The hard part is getting started. At the moment, there are very few tutorials on creating GPS aware Android apps. I've spent the past few days digging through the Android developer sites and other blogs. I finally found the minimal amount of information to create a very simple GPS aware application. This application will list the current longitude and latitude. In addition to the minimal number of steps needed, I will show you how to test the GPS aware app via the Eclipse debugger and ddms tool from the Android SDK.

My setup consists of Ubutnu 9.04 32-bit, Java 1.6.0_17, Android SDK 2.0, and Eclipse build 20090619-0625. The application created will display the current longitude and latitude as a simple ListView.

Steps to create an Android 2.0 application that will display the current longitude and latitude using an Android phone's GPS

1. Open Eclipse and create an Android project.

2. The project you create will be based upon the Andriod ListActivity. Inherit from ListActivity. For purposes of these instructions, I will assume that this new subclass is called MyGps.

3. In your new Android project, open the AndroidManifest.xml file. Add the following entry just after the tag:

android:name="android.permission.ACCESS_FINE_LOCATION" />

This entry is needed to get info from the Android GPS.

4. In the definition of the MyGPS class, you need to define 2 private helper classes inside the MyGPS class. The first private helper class you need to add will implement the GpsStatus.Litener interface.. The second private helper class you need to add will implement the LocationListener interface. For the purposes of these instructions, the helper class that implements the GpsStatus.Listener interface will be callsed MyGpsStatus. The helper class that implements the LocationListener interface will be called MyLocationListener. In MyLocationListener, create a constructor that takes as argument a ListActivity object.

5. In the MyGps class, add these members:

LocationManager myLocationManager;
ArrayList results;
String[] temp;
Criteria myBestCriteria;
String myBestGps;

myLocationManger is an instance of the LocationManager class that is used to check if the GPS feature is available and to set the sensitivity of GPS position changes.

The results ArrayList holds the various info from GPS queries.

The info stored in the ArrayList results is converted into a regular String[] array called temp.

myBestCriteria is an instance of a Criteria class. This instance is used to define the desired features of the GPS. myBestCriteria can be used to define the number of attributes that a given GPS source will provide(such as altitude, speed, bearing, etc). myBestCriteria is also used to define the amount of position change that is considered a 'change'.

The myBestGps is a String object that holds the name of the GPS provider that best meets the myBestCriteria requirements.

6. In the onCreate method of the MyGps class definition, do the following:

a. Initialize the myLocationManager reference with an instance created by the getSystemService method.

b. Initialize the results ArrayList. Add an initial item so that there is at least 1 member of the array is not null.

c. Use addGpsListner to add a GPS listner instance to MyGps. Use the creation of MyGpsStatus object as an argument.

d. Use requestLocationUpdates to define the resolution of GPS change.

e. Instantiate myBestCriteria's Critera object.

f. Initialize the features of the GPS source by using the various methods of the Criteria class.

g. Get the name of the best GPS source by using the getBestProvider method on the myLocationManger object. Use myBestCriteria as an argument.

h. Display the initial contents of the GPS features by using these last lines of code at the end of the onCreate method:

setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, temp));

getListView().setTextFilterEnabled(true);

temp must be the String[] version of results.

i. Go to the private class definition of MyLocationListener and implement the onLocationMethod as follows:

temp = new String[ 3 ];
String tempLatitude = String.valueOf(location.getLatitude());
String tempLongitude = String.valueOf(location.getLongitude());
temp[0] = new String("status");
temp[1] = tempLatitude;
temp[2] = tempLongitude;

listactivity.setListAdapter(new ArrayAdapter(listactivity,
android.R.layout.simple_list_item_1, temp));

The setListAdatper method is sent to the listactivity reference, which points to the MyGps object. The setListAdapter redisplays the new info from the GPS reading. Note, this method of displaying new info is different from what one finds in the current Android 2.0 docs. The current Android 2.0 docs say that the onUpdate method will work but I've found that it does not work.

j. Download the application to the emulator and wait for the emulator to run the application.

k. When the application is running, run the ddms.

m. In the ddms gui, select the instance of the emulator.

n. In the upper right hand corner area of the ddms gui, click on the rh arrow until the 'Emulator Control' tab is selected.

o. Scroll down until you see the 'Location Controls' area in the 'Eumlator Control' area. The textboxes and send button should not be grayed out. If they are, go back to step m and make sure that the instance of the emulator is selected.

p. Change the longitude and/or latitude coordiantes.

q. Hit the send button.

r. When the send button is clicked, you should see the contents of the application on the Android emulator show the new longitude and/or latitude coordinates.

Saturday, December 26, 2009

How to get longitude and latitude from Google Maps

So, I have a new obsession. I've been toying around with Android and creating gps aware applications. I don't own my own Android phone yet. I'm having a hard time deciding if I want a Droid or an Eris. In the meantime, I've been hanging around the Android sites, learning as much as I can about writing Android apps. One of the areas I'm interested in is GPS aware apps, where apps change their behavior based upon the current GPS position.

The Android 2.0 SDK comes with its own Android device simulator. which can simulate an Android device that is changing its position in the real-world. One of the ways it can simulate this behavior is for a user to submit a longitude and latitude. Well, how do you get longitude and latitude coordinates if you don't own a GPS and/or the location you want to simulate is not accessible. Google Maps can be used to provide longitude and latitude coordinates.

First, go to Google Maps and enter the address of interest. The address you just entered will appear on the left-hand side. Move the mouse over the address which appears as a link. When you move your mouse over the link, the contents will appear in the bottom of your browser. In my case, I'm using Firefox. It's possible your browser may not do this. In the link that appears, look for the string that appears as follows:

ssl=X,Y

X will be the longitude. Y will be the latitude.

Tuesday, November 10, 2009

1-liner for creating symbolic links in /usr/local/bin

If you have to create more symbolic links than you can create manually, this is a useful 1-liner. In this case, this 1-liner was used to install the binaries from Java 1.6 into /usr/local/bin:

find /usr/local/jdk1.6.0_17/bin|grep -vE "bin$|ControlPanel$"|perl -ne 'system "ln -s $_";'

This sort of 1-liner is useful when the number of symbolic links to create is greater than 1. It easily scales to 100's and even 100's of symbolic links. With the grep tool, one could even easily filter out items by name. Very useful!

Wednesday, October 14, 2009

New Machine Learning API's to Explore

Today on reddit, someone asked about freely available machine learning API's.

Before the list gets buried, I'm duplicating the contents of that thread here for future exploration:

Weka - Java based ML API
http://www.cs.waikato.ac.nz/ml/weka/

Toolkit for Advanced Disrimnative Modeling
http://tadm.sf.net/

Mallet - Java based ML API
http://mallet.cs.umass.edu/

WekaUT - An extension of Weka that adds clustering
http://www.cs.utexas.edu/users/ml/risc/code/

LibSVM - SVM API
http://www.csie.ntu.edu.tw/~cjlin/libsvm/

SVMlight - A C API for svm
http://svmlight.joachims.org/

C++ API for Neural Networks
http://github.com/bayerj/arac

Torch5 - A Matlab-like ML environment
http://torch5.sourceforge.net/

R - Open source statistical package that can be used for ML
http://cran.r-project.org/web/views/MachineLearning.html

pyML - Python API for ML
http://pyml.sourceforge.net/

Rapidminer - Open Source Data Mining Tool
http://rapid-i.com/wiki/index.php?title=Main_Page

Orange - An Open Source Data Mining Tool (Python and GUI based)
http://www.ailab.si/orange/

Glue - Open Source API for reinforcement learning (Can be used with multiple languages simultaneously)
http://glue.rl-community.org/wiki/Main_Page

Vowpal Rabiit - Learning API from Yahoo Research
http://hunch.net/~vw/

Tuesday, October 13, 2009

FC9 64-bit and VMware Workstation 6.5.3 issue with VMware Tools

I was working with a VMware appliance, which was configured to use the guest OS Fedora Core 9 64-bit.

I was using this guest OS with the latest vesion of VMware Workstation, v6.5.3. I downloaded the latest version of v6.5.3 today and I upgraded the VMware Tools of the FC9 64-bit guest.

It installed but had some kind of problem. After installing the VMware Tools that came with the latest version of VMware Workstation 6.5.3, yum and the 'software updater' were unable to upgrade, remove, and/or download RPM's.

Saturday, October 10, 2009

Unable to find a C or C++ NLG open source tool this week

So, I've been exploring the area of NLG, natural langugae generation. My personal goal was to develop an application that would read a corpus and respond with either a summary of the corpus, or a response to the categories found. In either case, I wanted the summary or response to not just be a template where the noun/verb/adjective/predicates were merely filled in. That's no better than using grep.

As of this week, I can only find API's written in Java, Python, Lisp, and Prolog. Many of the listed NLG API's or applications haven't been touched in years, or are no longer available. Much to my displeasure, nothing in C or C++. I want something that will run lean, mean, and can scale to datasets over a terabyte in size.

Tuesday, October 6, 2009

Natural Language Generation

While going over some nbc's, I stumbled across the AI area of NLP. But, while observing where the areas of nbc and NLP meet, I've found a new obsession: NLG. NLG is an acronym for natural language generation. Natural language generation is text created by a computer program that appears to be human-like in readability.

I first heard about this topic in detail in my AI class in grad school at University of San Francisco. My professor, Dr. Brooks, had mentioned that researchers had been trying for years to create programs that could generate narratives for computer games. I even recall seeing on some news aggregator that someone had successfully won a writing contest with a story written by a NLG system.

At the time I was taking my AI course, I remember working for a horrible boss. Who made all of us who he saw everyday and interacted with on a continuous basis, write weekly reports. I remember wanting to write a Perl or Python script that would do this for me. I made some attempts but it was hard to get any realistic variance. It was essentially an overglorified mad lib, where the program only filled in the blanks.

I was looking for something more natural and human like.

In NLG, one takes data and has generation rules that result in text that feels as if a human wrote it. Surprisingly, if one does a search on NLG, it is a relatively new area of research. Perhaps the best introduction to this topic is on Wikipedia. From the Wikipedia area, you will find yourself on the Bateman and Zock list of Natural Language Generators(http://www.fb10.uni-bremen.de/anglistik/langpro/NLG-table/NLG-table-root.htm)

At the moment, the state of the art appears to be based upon Java and Lisp languages. Since I work in embedded systems where speed and small footprint are key, I'm intersted in implementations that are in C and can scale. I've noticed that most of the NLP and NLG systems I found do not have a database backend. This surprises me since use of a database would allow for scaling and more consistant performacne as the dataset grows.

I think I'll be experimenting with NLG to see if I can make a program that will generate an email that asks a user for info based upon an email inquiry.