MyLocationManager.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4. import android.content.Context;
  5. import android.location.Location;
  6. import android.location.LocationListener;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. public class MyLocationManager {
  11. private LocationManager locationManager;
  12. /**
  13. * Static variable that always holds the newest location update.
  14. */
  15. private static Location newestLocation = null;
  16. private static final int TWO_MINUTES = 1000 * 60 * 2;
  17. public MyLocationManager(Context context){
  18. // Acquire a reference to the system Location Manager
  19. locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  20. newestLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  21. }
  22. // Define a listener that responds to location updates
  23. LocationListener locationListener = new LocationListener() {
  24. public void onLocationChanged(Location location) {
  25. Log.i("MyLocationManager", location.getLatitude() + " " + location.getLongitude() + " " + location.getAccuracy());
  26. // Called when a new location is found by the network location provider.
  27. if(isBetterLocation(location, newestLocation)){
  28. newestLocation = location;
  29. }
  30. }
  31. public void onStatusChanged(String provider, int status, Bundle extras) {}
  32. public void onProviderEnabled(String provider) {}
  33. public void onProviderDisabled(String provider) {}
  34. };
  35. /** Determines whether one Location reading is better than the current Location fix
  36. * @param location The new Location that you want to evaluate
  37. * @param currentBestLocation The current Location fix, to which you want to compare the new one
  38. */
  39. private boolean isBetterLocation(Location location, Location currentBestLocation) {
  40. if (currentBestLocation == null) {
  41. // A new location is always better than no location
  42. return true;
  43. }
  44. // Check whether the new location fix is newer or older
  45. long timeDelta = location.getTime() - currentBestLocation.getTime();
  46. boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
  47. boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
  48. // If it's been more than two minutes since the current location, use the new location
  49. // because the user has likely moved
  50. if (isSignificantlyNewer) {
  51. return true;
  52. // If the new location is more than two minutes older, it must be worse
  53. } else if (isSignificantlyOlder) {
  54. return false;
  55. }
  56. // Check whether the new location fix is more or less accurate
  57. int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
  58. boolean isMoreAccurate = accuracyDelta < 0;
  59. // Determine location quality using a combination of timeliness and accuracy
  60. if (isMoreAccurate) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * Start updating {@link de.tudarmstadt.informatik.hostage.logging.MyLocationManager#newestLocatio newestLocation}
  67. * if a location provider is enabled and available.
  68. */
  69. public void startUpdates(){
  70. boolean gpsEnabled = false;
  71. boolean networkEnabled = false;
  72. //exceptions will be thrown if provider is not permitted.
  73. try{
  74. gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  75. }catch(Exception ex){}
  76. try{
  77. networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  78. }catch(Exception ex){}
  79. //don't start listeners if no provider is enabled
  80. if(!gpsEnabled && !networkEnabled)
  81. return;
  82. // Register the listener with the Location Manager to receive location updates
  83. if(gpsEnabled)
  84. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  85. if(networkEnabled)
  86. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  87. }
  88. /**
  89. * Starts updating the location data for the given amount of time.
  90. * Calls itself recursive if no location data has been found yet and there are still attempts left.
  91. * @param time Time to update location data
  92. * @param attempts Remaining attempts for recieving location data
  93. */
  94. public void getUpdates(long time, int attempts){
  95. startUpdates();
  96. attempts--;
  97. Timer timer1=new Timer();
  98. timer1.schedule(new StopTask() , time);
  99. if(newestLocation == null && attempts > 0)
  100. getUpdates(time, attempts);
  101. }
  102. class StopTask extends TimerTask {
  103. public void run() {
  104. stopUpdates();
  105. }
  106. }
  107. public void stopUpdates(){
  108. locationManager.removeUpdates(locationListener);
  109. }
  110. public static Location getNewestLocation(){
  111. return newestLocation;
  112. }
  113. }