MyLocationManager.java 4.9 KB

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