Skip to content

Commit

Permalink
Merge pull request #77 from mvglasow/no-stale-location
Browse files Browse the repository at this point in the history
Report a new location only after receiving a non-null update
  • Loading branch information
mar-v-in committed Mar 18, 2016
2 parents bcf1cff + 1093f40 commit 7b31a55
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ public void bind() {
}

public void update() {
boolean hasUpdates = false;
fusing = true;
for (BackendHelper handler : backendHelpers) {
handler.update();
if (handler.update() != null)
hasUpdates = true;
}
fusing = false;
updateLocation();
if (hasUpdates)
updateLocation();
}

void updateLocation() {
Expand Down Expand Up @@ -132,11 +135,14 @@ public void reportLocation() {
}

public void forceLocation(Location location) {
if ((forcedLocation != null) && (location != null) && (forcedLocation.getTime() >= location.getTime()))
return;
forcedLocation = location;
if (forcedLocation != null) {
Bundle extras = new Bundle();
extras.putString(LOCATION_EXTRA_BACKEND_PROVIDER, "forced");
location.setExtras(extras);
reportLocation();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,31 @@ public Location getLastLocation() {
return lastLocation;
}

public void update() {
/**
* @brief Requests a location update from the backend.
*
* @return The location reported by the backend. This may be null if a backend cannot determine its
* location, or if it is going to return a location asynchronously.
*/
public Location update() {
Location result = null;
if (backend == null) {
Log.d(TAG, "Not (yet) bound.");
updateWaiting = true;
} else {
updateWaiting = false;
try {
setLastLocation(backend.update());
backendFuser.reportLocation();
result = backend.update();
if ((result != null) && (result.getTime() > lastLocation.getTime())) {
setLastLocation(result);
backendFuser.reportLocation();
}
} catch (Exception e) {
Log.w(TAG, e);
unbind();
}
}
return result;
}

private void setLastLocation(Location location) {
Expand Down Expand Up @@ -139,6 +150,8 @@ public void onServiceDisconnected(ComponentName name) {
private class Callback extends LocationCallback.Stub {
@Override
public void report(Location location) throws RemoteException {
if ((location == null) || (location.getTime() <= lastLocation.getTime()))
return;
setLastLocation(location);
backendFuser.reportLocation();
}
Expand Down

0 comments on commit 7b31a55

Please sign in to comment.