Android's SDK allows developers to do a lot with the platform, but there are some interesting capabilities of the system that aren't accessible through the public API. It's still possible to access these capabilities with a little bit of work and the Android source code, available from
http://source.android.com. The source code I wrote is
here.
The capability I wanted to expose is collecting GPS satellite information - data on the satellites (SVs - space vehicles) themselves. There are hidden methods in the
LocationManager service API to set up callbacks to collect this data, but they aren't accessible through the public API. I considered the various ways I could get this data and the easiest one I came up with is to send messages directly to the
LocationManager service itself using the IPC interface.
LocationManager and LocationManagerService
The
LocationManager API in the
android.location namespace is the public interface to the
LocationManagerService, in
com.android.server. The service runs in a separate process, and the API communicates with it using Android's IPC mechanism (
Binder). When an Android application wants to communicate with the
LocationManagerService, it uses this API call to get a reference to the API object:
LocationManager mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Normally, an application will then use the
LocationManager API to register for location events (via
requestLocationUpdates()) and then use the data to do really fun and interesting things, like
this.
If you dig into the
LocationManager source code, you'll see that there's a method called
registerGpsStatusListener(). This sets up a listener that's able to get updates on GPS satellite information – PRNs, elevations, azimuths, etc, of each of the satellites in view of the GPS. This is what I want. Unfortunately, this method, as well as the argument type (
GpsStatusListenerTransport), aren't visible in the android.jar that comes with the SDK. It's still possible to get at this information by communicating directly with the
LocationManagerService itself.
The APIs available in the Android SDK communicate with system services using IPC. Specifically, they use interfaces defined with
AIDL to communicate with the service. The AIDL specifications for the service IPC API's isn't available in the SDK, but you can get them from the Android source code. The AIDL is used to generate a client stub that Java classes can use to send messages to and receive messages from the service.
Accessing LocationManagerService directly
The class in
LocationManager that communicates with the
LocationManagerService is
GpsStatusListenerTransport. This is an extension of the
IGpsStatusListener.Stub class, which was generated from an AIDL specification –
IGpsStatusListener.aidl. It's possible to copy
IGpsStatusListener.aidl from the Android source code and add to your project to generate the
IGpsListenerStatus.Stub class. This project can then use this to communicate directly with the service that implements that interface.
IGpsStatusListener isn't the only AIDL interface we'll need –
ILocationManager.aidl,
ILocationListener.aidl, and
Address.aidl are also needed to do what we want. Once these classes are available in our project (I put them in the
android.location namespace to avoid changing the code, but it doesn't really matter which namespace they belong to). Once these interfaces are available to our project, it's really easy to get what we want.
While it is possible to write a totally new client for the
LocationManagerService using the AIDL interfaces, it's actually easier for us to re-use an existing client. Setting up a new connection involves a bunch of lines of code and my carpal tunnels hate when I write too much code. The easy way is to use the Java reflection API to get the handle to the system service:
Class c = Class.forName(mLocationManager.getClass().getName());
Field f = c.getDeclaredField("mService");
f.setAccessible(true);
ILocationManager mILM = (ILocationManager)f.get(mLocationManager);
Accessing private fields like this in Java is generally frowned upon in production code, but we're hackers and we want the data and we can do whatever we want to get it and the establishment can't stop us.
So now that we have access to the
ILocationManager API, we can see that there is an
addGpsStatusListener() method we can call to add a listener that retrieves the GPS satellite status updates. This takes an
IGpsStatusListener object, which we can create by instantiating a class that extends
IGpsStatusListener.stub. Passing that object to
addGpsStatusListener() will result in us getting
onSvStatusChanged() callbacks with the satellite status. Nice.
Before we get any of this spiffy satellite info, the GPS must be enabled. In my sample app, I used the regular
LocationManager API to do this by adding a
LocationListener. The result of all of my efforts is this useless and boring application that shows some information about some satellites:
Oops...
If you start digging into the service code, you might notice that no permissions are required to add a listener for GPS status updates. This is a minor and low-risk information leak vulnerability. Applications shouldn't have access to GPS location data unless they have the correct permission. If an application creates a service that listens for GPS status updates (but not GPS location updates, which require the
ACCESS_FINE_LOCATION permission), it will get these updates when any other application turns on the GPS. It's possible, with some fancy math, to determine the location of the phone with this data. You'll need to know the exact time, the relative locations of the GPS satellites to the user, and the absolute locations of the GPS satellites in space at that time. The time and relative locations can come from the phone, and the precise locations of the satellites can be found at
http://www.navcen.uscg.gov/GPS/almanacs.htm. I'll leave doing the fancy math as an exercise to the reader.
Update: Proposed patch to add permission check,
http://review.source.android.com/5124.