Dbrant has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/160509

Change subject: Make sure that a new location is "better" than the previous one.
......................................................................

Make sure that a new location is "better" than the previous one.

Change-Id: I64fbd0c41f828cf82de92698be55420c642cbbf9
---
M wikipedia/src/main/java/org/wikipedia/nearby/NearbyActivity.java
1 file changed, 60 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/09/160509/1

diff --git a/wikipedia/src/main/java/org/wikipedia/nearby/NearbyActivity.java 
b/wikipedia/src/main/java/org/wikipedia/nearby/NearbyActivity.java
index 0f589c8..7770e5b 100644
--- a/wikipedia/src/main/java/org/wikipedia/nearby/NearbyActivity.java
+++ b/wikipedia/src/main/java/org/wikipedia/nearby/NearbyActivity.java
@@ -223,6 +223,9 @@
     }
 
     private void makeUseOfNewLocation(Location location) {
+        if (!isBetterLocation(location, lastLocation)) {
+            return;
+        }
         nextLocation = location;
         if (lastLocation == null || (refreshing && getDistance(lastLocation) 
>= MIN_DISTANCE_METERS)) {
 
@@ -253,6 +256,63 @@
         }
     }
 
+    /** Determines whether one Location reading is better than the current 
Location fix.
+     * lifted from 
http://developer.android.com/guide/topics/location/strategies.html
+     * @param location  The new Location that you want to evaluate
+     * @param currentBestLocation  The current Location fix, to which you want 
to compare the new one
+     */
+    protected boolean isBetterLocation(Location location, Location 
currentBestLocation) {
+        if (currentBestLocation == null) {
+            // A new location is always better than no location
+            return true;
+        }
+
+        // Check whether the new location fix is newer or older
+        final int twoMinutes = 1000 * 60 * 2;
+        final int accuracyThreshold = 200;
+        long timeDelta = location.getTime() - currentBestLocation.getTime();
+        boolean isSignificantlyNewer = timeDelta > twoMinutes;
+        boolean isSignificantlyOlder = timeDelta < -twoMinutes;
+        boolean isNewer = timeDelta > 0;
+
+        // If it's been more than two minutes since the current location, use 
the new location
+        // because the user has likely moved
+        if (isSignificantlyNewer) {
+            return true;
+            // If the new location is more than two minutes older, it must be 
worse
+        } else if (isSignificantlyOlder) {
+            return false;
+        }
+
+        // Check whether the new location fix is more or less accurate
+        int accuracyDelta = (int) (location.getAccuracy() - 
currentBestLocation.getAccuracy());
+        boolean isLessAccurate = accuracyDelta > 0;
+        boolean isMoreAccurate = accuracyDelta < 0;
+        boolean isSignificantlyLessAccurate = accuracyDelta > 
accuracyThreshold;
+
+        // Check if the old and new location are from the same provider
+        boolean isFromSameProvider = isSameProvider(location.getProvider(),
+                                                    
currentBestLocation.getProvider());
+
+        // Determine location quality using a combination of timeliness and 
accuracy
+        if (isMoreAccurate) {
+            return true;
+        } else if (isNewer && !isLessAccurate) {
+            return true;
+        } else if (isNewer && !isSignificantlyLessAccurate && 
isFromSameProvider) {
+            return true;
+        }
+        return false;
+    }
+
+    /** Checks whether two providers are the same */
+    private boolean isSameProvider(String provider1, String provider2) {
+        if (provider1 == null) {
+            return provider2 == null;
+        }
+        return provider1.equals(provider2);
+    }
+
     private void showNearbyPages(List<NearbyPage> result) {
         nearbyList.setEmptyView(nearbyEmptyContainer);
         lastLocation = nextLocation;

-- 
To view, visit https://gerrit.wikimedia.org/r/160509
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I64fbd0c41f828cf82de92698be55420c642cbbf9
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Dbrant <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to