MyLocationManager.java 4.9 KB

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