diff --git a/unifiednlp-base/src/main/java/org/microg/nlp/location/BackendFuser.java b/unifiednlp-base/src/main/java/org/microg/nlp/location/BackendFuser.java index 63a2b09..08b2fe1 100644 --- a/unifiednlp-base/src/main/java/org/microg/nlp/location/BackendFuser.java +++ b/unifiednlp-base/src/main/java/org/microg/nlp/location/BackendFuser.java @@ -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() { @@ -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(); } } diff --git a/unifiednlp-base/src/main/java/org/microg/nlp/location/BackendHelper.java b/unifiednlp-base/src/main/java/org/microg/nlp/location/BackendHelper.java index 53f98a2..c7fccd4 100644 --- a/unifiednlp-base/src/main/java/org/microg/nlp/location/BackendHelper.java +++ b/unifiednlp-base/src/main/java/org/microg/nlp/location/BackendHelper.java @@ -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) { @@ -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(); }