Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Report last known location when engine enabled #148

Merged
merged 2 commits into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,35 @@ public FusionEngine(Context context, Callback callback) {
}
}

if (networkInterval < Long.MAX_VALUE) {
enableNetwork(networkInterval);
}
boolean checkGps = false;
if (gpsInterval < Long.MAX_VALUE) {
enableGps(gpsInterval);
checkGps = true;
}
if (networkInterval < Long.MAX_VALUE) {
enableNetwork(networkInterval);
if (checkGps) {
Location lastGps = locationManager.getLastKnownLocation(GPS_PROVIDER);
Location lastNetwork = locationManager.getLastKnownLocation(NETWORK_PROVIDER);
if (lastGps != null && lastNetwork != null) {
boolean useGps = isBetterThan(lastGps, lastNetwork);
if (useGps) {
checkLastKnownGps();
} else {
checkLastKnownNetwork();
}
} else if (lastGps != null) {
checkLastKnownGps();
} else {
checkLastKnownNetwork();
}
} else {
checkLastKnownNetwork();
}
}
if (passiveInterval < Long.MAX_VALUE) {
enablePassive(passiveInterval);
checkLastKnownPassive();
}
}

Expand Down Expand Up @@ -144,6 +165,25 @@ private void enablePassive(long interval) throws SecurityException {
}
}

private void checkLastKnownGps() {
checkLastKnownAndNotify(GPS_PROVIDER);
}

private void checkLastKnownNetwork() {
checkLastKnownAndNotify(NETWORK_PROVIDER);
}

private void checkLastKnownPassive() {
checkLastKnownAndNotify(PASSIVE_PROVIDER);
}

private void checkLastKnownAndNotify(String provider) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For location requests with PRIORITY_HIGH_ACCURACY this method would be invoked for both GPS_PROVIDER and NETWORK_PROVIDER resulting in two initial location updates.

We should only send one initial location update and use the logic in FusionEngine.getLastLocation() to decide which one is best.

Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
}

@Override public void onLocationChanged(Location location) {
if (GPS_PROVIDER.equals(location.getProvider())) {
gpsLocation = location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,53 @@ public void getLastLocation_shouldReturnNullIfNoLocationPermissionsGranted() thr
assertThat(callback.location).isEqualTo(networkLocation);
}

@Test public void onLocationChanged_gps_shouldReportLastKnownLocation() throws Exception {
Location gpsLocation = new Location(GPS_PROVIDER);
shadowLocationManager.setLastKnownLocation(GPS_PROVIDER, gpsLocation);

LocationRequest request = LocationRequest.create().setPriority(PRIORITY_HIGH_ACCURACY);
fusionEngine.setRequest(request);

assertThat(callback.location).isEqualTo(gpsLocation);
}

@Test public void onLocationChanged_network_shouldReportLastKnownLocation() throws Exception {
Location networkLocation = new Location(NETWORK_PROVIDER);
shadowLocationManager.setLastKnownLocation(NETWORK_PROVIDER, networkLocation);

LocationRequest request = LocationRequest.create().setPriority(
PRIORITY_BALANCED_POWER_ACCURACY);
fusionEngine.setRequest(request);

assertThat(callback.location).isEqualTo(networkLocation);
}

@Test public void onLocationChanged_gpsNetwork_shouldReportLastKnownLocation() throws Exception {
Location gpsLocation = new Location(GPS_PROVIDER);
shadowLocationManager.setLastKnownLocation(GPS_PROVIDER, gpsLocation);
Location networkLocation = new Location(NETWORK_PROVIDER);
shadowLocationManager.setLastKnownLocation(NETWORK_PROVIDER, networkLocation);

LocationRequest request = LocationRequest.create().setPriority(PRIORITY_HIGH_ACCURACY);
fusionEngine.setRequest(request);

assertThat(callback.location).isEqualTo(networkLocation);
}

@Test public void onLocationChanged_gpsNetwork_shouldReportOneLastKnownLocation() throws
Exception {
Location gpsLocation = new Location(GPS_PROVIDER);
shadowLocationManager.setLastKnownLocation(GPS_PROVIDER, gpsLocation);
Location networkLocation = new Location(NETWORK_PROVIDER);
shadowLocationManager.setLastKnownLocation(NETWORK_PROVIDER, networkLocation);

LocationRequest request = LocationRequest.create().setPriority(PRIORITY_HIGH_ACCURACY);
fusionEngine.setRequest(request);

assertThat(callback.numLocationReports).isEqualTo(1);
}


@Test public void isBetterThan_shouldReturnFalseIfLocationAIsNull() throws Exception {
Location locationA = null;
Location locationB = new Location("test");
Expand Down Expand Up @@ -369,9 +416,11 @@ private static Location getTestLocation(String provider, float lat, float lng, l

class TestCallback implements LocationEngine.Callback {
private Location location;
private int numLocationReports = 0;

@Override public void reportLocation(Location location) {
this.location = location;
numLocationReports++;
}

@Override public void reportProviderDisabled(String provider) {
Expand Down