MyLocationManager.java 4.6 KB

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