BaseLocationClient implements ILocationClient { protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { return true; } long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; if (isSignificantlyNewer) { return true; } else if (isSignificantlyOlder) { return false; } int accuracyDelta = (int)(location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; }  @Override void setListener(LocationClientListener locationClientListener); @Nullable @Override LocationClientListener getListener(); @Deprecated @Override void setLocationListener(@Nullable LocationListener locationListener); @Deprecated @Nullable @Override LocationListener getLocationListener(); @Override boolean addLocationListener(@NonNull LocationListener locationListener); @Override ArrayList<LocationListener> getLocationListeners(); @Override boolean removeLocationListener(@NonNull LocationListener locationListener); @Override boolean isProviderEnabled(); @Override void close(); @Override boolean isMonitoringLocation(); @Override void clearLocationListeners();  }
@Test public void isBetterLocationWhenGivenSignificantlyNewerLocationShouldReturnTrue() { int significantlyOlder = (2 * 60 * 1000) + 20; long timeNow = System.currentTimeMillis(); Location newLocation = generateLocation(12, timeNow); Location currentLocation = generateLocation(3, timeNow - significantlyOlder); assertTrue(testLocationClient.isBetterLocation(newLocation, currentLocation)); }
@Test public void isBetterLocationWhenGivenSignificantlyOlderLocationShouldReturnFalse() { int significantlyOlder = (2 * 60 * 1000) + 20; long timeNow = System.currentTimeMillis(); Location currentLocation = generateLocation(12, timeNow); Location newLocation = generateLocation(3, timeNow - significantlyOlder); assertFalse(testLocationClient.isBetterLocation(newLocation, currentLocation)); }
@Test public void setIsBetterLocationWhenGivenMoreAccurateLocationShouldReturnTrue() { int olderTime = 30 * 1000; long timeNow = System.currentTimeMillis(); Location currentLocation = generateLocation(12, timeNow - olderTime); Location newLocation = generateLocation(3, timeNow); assertTrue(testLocationClient.isBetterLocation(newLocation, currentLocation)); }
@Test public void setIsBetterLocationWhenGivenNewerAndNotLessAccurateLocationShouldReturnTrue() { int olderTime = 30 * 1000; long timeNow = System.currentTimeMillis(); Location currentLocation = generateLocation(12, timeNow - olderTime); Location newLocation = generateLocation(12, timeNow); assertTrue(testLocationClient.isBetterLocation(newLocation, currentLocation)); }
@Test public void setIsBetterLocationWhenGivenNewerAndNotSignificantlyLessAccurateLocationFromSameProviderShouldReturnTrue() { int olderTime = 30 * 1000; long timeNow = System.currentTimeMillis(); Location currentLocation = generateLocation(12, timeNow - olderTime); Location newLocation = generateLocation(20, timeNow); assertTrue(testLocationClient.isBetterLocation(newLocation, currentLocation)); }
@Test public void setIsBetterLocationWhenGivenNewerAndSignificantlyLessAccurateLocationFromSameProviderShouldReturnFalse() { int olderTime = 30 * 1000; long timeNow = System.currentTimeMillis(); Location currentLocation = generateLocation(12, timeNow - olderTime); Location newLocation = generateLocation(220, timeNow); testLocationClient.isBetterLocation(newLocation, currentLocation); }
@Test public void setIsBetterLocationWhenGivenOlderAndSameAccuracyLocationFromSameProviderShouldReturnFalse() { int olderTime = 30 * 1000; long timeNow = System.currentTimeMillis(); Location currentLocation = generateLocation(12, timeNow); Location newLocation = generateLocation(12, timeNow - olderTime); testLocationClient.isBetterLocation(newLocation, currentLocation); }