@Test @Config(sdk = 17) public void getLastLocation_ifNoFreshLocationsShouldReturnMostRecent() throws Exception { long time = android.os.SystemClock.elapsedRealtime() * MS_TO_NS; initTestClock(time); Location gpsLocation = new Location(GPS_PROVIDER); gpsLocation.setAccuracy(100); gpsLocation.setElapsedRealtimeNanos(time - (2 * RECENT_UPDATE_THRESHOLD_IN_MILLIS)); shadowLocationManager.setLastKnownLocation(GPS_PROVIDER, gpsLocation); Location networkLocation = new Location(NETWORK_PROVIDER); networkLocation.setAccuracy(100); networkLocation.setElapsedRealtimeNanos(time - (3 * RECENT_UPDATE_THRESHOLD_IN_MILLIS)); shadowLocationManager.setLastKnownLocation(NETWORK_PROVIDER, networkLocation); assertThat(fusionEngine.getLastLocation()).isEqualTo(gpsLocation); }
@Override public Location getLastLocation() { final List<String> providers = locationManager.getAllProviders(); final long minTime = clock.getSystemElapsedTimeInNanos() - RECENT_UPDATE_THRESHOLD_IN_NANOS; Location bestLocation = null; float bestAccuracy = Float.MAX_VALUE; long bestTime = Long.MIN_VALUE; for (String provider : providers) { try { final Location location = locationManager.getLastKnownLocation(provider); if (location != null) { final float accuracy = location.getAccuracy(); final long time = clock.getElapsedTimeInNanos(location); if (time > minTime && accuracy < bestAccuracy) { bestLocation = location; bestAccuracy = accuracy; bestTime = time; } else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) { bestLocation = location; bestTime = time; } } } catch (SecurityException e) { Log.e(TAG, "Permissions not granted for provider: " + provider, e); } } return bestLocation; }
FusionEngine extends LocationEngine implements LocationListener { @Override public Location getLastLocation() { final List<String> providers = locationManager.getAllProviders(); final long minTime = clock.getSystemElapsedTimeInNanos() - RECENT_UPDATE_THRESHOLD_IN_NANOS; Location bestLocation = null; float bestAccuracy = Float.MAX_VALUE; long bestTime = Long.MIN_VALUE; for (String provider : providers) { try { final Location location = locationManager.getLastKnownLocation(provider); if (location != null) { final float accuracy = location.getAccuracy(); final long time = clock.getElapsedTimeInNanos(location); if (time > minTime && accuracy < bestAccuracy) { bestLocation = location; bestAccuracy = accuracy; bestTime = time; } else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) { bestLocation = location; bestTime = time; } } } catch (SecurityException e) { Log.e(TAG, "Permissions not granted for provider: " + provider, e); } } return bestLocation; } }
FusionEngine extends LocationEngine implements LocationListener { @Override public Location getLastLocation() { final List<String> providers = locationManager.getAllProviders(); final long minTime = clock.getSystemElapsedTimeInNanos() - RECENT_UPDATE_THRESHOLD_IN_NANOS; Location bestLocation = null; float bestAccuracy = Float.MAX_VALUE; long bestTime = Long.MIN_VALUE; for (String provider : providers) { try { final Location location = locationManager.getLastKnownLocation(provider); if (location != null) { final float accuracy = location.getAccuracy(); final long time = clock.getElapsedTimeInNanos(location); if (time > minTime && accuracy < bestAccuracy) { bestLocation = location; bestAccuracy = accuracy; bestTime = time; } else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) { bestLocation = location; bestTime = time; } } } catch (SecurityException e) { Log.e(TAG, "Permissions not granted for provider: " + provider, e); } } return bestLocation; } FusionEngine(Context context, Callback callback); }
FusionEngine extends LocationEngine implements LocationListener { @Override public Location getLastLocation() { final List<String> providers = locationManager.getAllProviders(); final long minTime = clock.getSystemElapsedTimeInNanos() - RECENT_UPDATE_THRESHOLD_IN_NANOS; Location bestLocation = null; float bestAccuracy = Float.MAX_VALUE; long bestTime = Long.MIN_VALUE; for (String provider : providers) { try { final Location location = locationManager.getLastKnownLocation(provider); if (location != null) { final float accuracy = location.getAccuracy(); final long time = clock.getElapsedTimeInNanos(location); if (time > minTime && accuracy < bestAccuracy) { bestLocation = location; bestAccuracy = accuracy; bestTime = time; } else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) { bestLocation = location; bestTime = time; } } } catch (SecurityException e) { Log.e(TAG, "Permissions not granted for provider: " + provider, e); } } return bestLocation; } FusionEngine(Context context, Callback callback); @Override Location getLastLocation(); @Override boolean isProviderEnabled(String provider); @Override void onLocationChanged(Location location); @Override void onStatusChanged(String provider, int status, Bundle extras); @Override void onProviderEnabled(String provider); @Override void onProviderDisabled(String provider); static boolean isBetterThan(Location locationA, Location locationB); }
FusionEngine extends LocationEngine implements LocationListener { @Override public Location getLastLocation() { final List<String> providers = locationManager.getAllProviders(); final long minTime = clock.getSystemElapsedTimeInNanos() - RECENT_UPDATE_THRESHOLD_IN_NANOS; Location bestLocation = null; float bestAccuracy = Float.MAX_VALUE; long bestTime = Long.MIN_VALUE; for (String provider : providers) { try { final Location location = locationManager.getLastKnownLocation(provider); if (location != null) { final float accuracy = location.getAccuracy(); final long time = clock.getElapsedTimeInNanos(location); if (time > minTime && accuracy < bestAccuracy) { bestLocation = location; bestAccuracy = accuracy; bestTime = time; } else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) { bestLocation = location; bestTime = time; } } } catch (SecurityException e) { Log.e(TAG, "Permissions not granted for provider: " + provider, e); } } return bestLocation; } FusionEngine(Context context, Callback callback); @Override Location getLastLocation(); @Override boolean isProviderEnabled(String provider); @Override void onLocationChanged(Location location); @Override void onStatusChanged(String provider, int status, Bundle extras); @Override void onProviderEnabled(String provider); @Override void onProviderDisabled(String provider); static boolean isBetterThan(Location locationA, Location locationB); static final long RECENT_UPDATE_THRESHOLD_IN_MILLIS; static final long RECENT_UPDATE_THRESHOLD_IN_NANOS; }