Skip to content

Instantly share code, notes, and snippets.

@javisantana
Created March 20, 2011 18:39
Show Gist options
  • Save javisantana/878539 to your computer and use it in GitHub Desktop.
Save javisantana/878539 to your computer and use it in GitHub Desktop.
real/fake gps
package hardware;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.location.GpsStatus.Listener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class GPS {
protected static final String TAG = "GPS";
static LocationManager locationManager;
public static LocationUpdateHandler listener;
static Location lastLocation = null;
static boolean fixPosition = false;
//static private GPSInterface gps = new HwGps(); //FakeGPS();
static private GPSInterface gps = new FakeGPS();
static public boolean init(Activity act) {
boolean ok = gps.init(act);
Log.i(TAG, "GPS init");
if (!ok)
Log.e(TAG, "GPS can't be initialized");
return ok;
}
static public boolean shutdown() {
gps.shutdown();
Log.i(TAG, "GPS shutdown");
return true;
}
}
interface GPSInterface {
boolean init(Activity act);
boolean shutdown();
}
class HwGps implements GPSInterface {
@Override
public boolean init(Activity act) {
GPS.locationManager =
(LocationManager) act.getSystemService(Context.LOCATION_SERVICE);
GPS.listener = new LocationUpdateHandler();
GPS.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, GPS.listener);
return true;
}
@Override
public boolean shutdown() {
GPS.locationManager.removeGpsStatusListener((Listener) GPS.listener);
return false;
}
}
class FakeGPS implements GPSInterface {
final Handler mHandler = new Handler();
Thread updateThread;
boolean running = true;
final Runnable mUpdateResults = new Runnable() {
public void run() {
Location loc = getNextPosition();
GPS.listener.onLocationChanged(loc);
}
};
float startLat = 41.71283f;
float startLon = -4.83140f;
int counter = 0;
Location getNextPosition() {
Location loc = new Location("EMU");
loc.setLatitude(startLat + 0.0001*counter);
loc.setLongitude(startLon);
++counter;
return loc;
}
@Override
public boolean init(Activity act) {
GPS.listener = new LocationUpdateHandler();
updateThread = new Thread() {
public void run() {
// 8 seconds to adquire signal
try {
Thread.sleep(8000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
while(running) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.post(mUpdateResults);
}
}
};
updateThread.start();
return true;
}
@Override
public boolean shutdown() {
running = false;
updateThread.stop();
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment