MyLocationManager.java 5.1 KB

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