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

Commit

Permalink
Merge pull request #54 from mapzen/reset-listeners-on-mock-mode
Browse files Browse the repository at this point in the history
Dont remove listeners on setMockMode
  • Loading branch information
ecgreb authored Jun 20, 2016
2 parents 11a470f + fa4873a commit 8c32e5b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.Context;
import android.location.Location;
import android.os.Looper;
import android.support.annotation.VisibleForTesting;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -42,9 +43,12 @@ public Location getLastLocation() {

@Override
public void requestLocationUpdates(LocationRequest request, LocationListener listener) {
LocationEngine existing = locationEngines.get(request);
LocationEngine engine = locationEngineForRequest(request);
addListenerForEngine(engine, listener);
engine.setRequest(request);
if (existing == null) {
engine.setRequest(request);
}
}

@Override
Expand Down Expand Up @@ -80,7 +84,7 @@ public void setMockMode(boolean isMockMode) {

private void toggleMockMode() {
mockMode = !mockMode;
shutdownAllEngines();
toggleAllEngines();
if (mockMode) {
lastLocationEngine = new MockEngine(context, null);
} else {
Expand Down Expand Up @@ -182,11 +186,55 @@ private LocationEngine removeListenerFromEngine(LocationListener listener) {
return null;
}

private void toggleAllEngines() {
HashMap<LocationRequest, LocationEngine> enginesToAdd = new HashMap<>();
HashMap<LocationEngine, List<LocationListener>> listenersToAdd = new HashMap<>();
for (LocationRequest request : locationEngines.keySet()) {
LocationEngine existing = locationEngines.get(request);
LocationEngine engineToAdd;
if (mockMode) {
engineToAdd = new MockEngine(context, this);
} else {
engineToAdd = new FusionEngine(context, this);
}
enginesToAdd.put(request, engineToAdd);

List<LocationListener> listeners = engineListeners.get(existing);
listenersToAdd.put(engineToAdd, listeners);

//Cleanup listeners
engineListeners.remove(existing);
existing.setRequest(null);
}
//Cleanup engines
locationEngines.clear();

//Now add new engines/listeners
for (LocationRequest request : enginesToAdd.keySet()) {
LocationEngine engine = enginesToAdd.get(request);
List<LocationListener> listeners = listenersToAdd.get(engine);
if (listeners != null) {
for (LocationListener listener : listeners) {
requestLocationUpdates(request, listener);
}
}
}
}

private void shutdownAllEngines() {
for (LocationEngine engine : locationEngines.values()) {
engineListeners.remove(engine);
engine.setRequest(null);
}
locationEngines.clear();
}

@VisibleForTesting
List<LocationListener> getListeners() {
List<LocationListener> listeners = new ArrayList<>();
for (LocationEngine engine : engineListeners.keySet()) {
listeners.addAll(engineListeners.get(engine));
}
return listeners;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ public void disconnect() {
public boolean isConnected() {
return LocationServices.FusedLocationApi != null && LocationServices.GeofencingApi != null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ public void setMockMode_shouldNotRegisterDuplicateListeners() throws Exception {
assertThat(shadowLocationManager.getRequestLocationUpdateListeners()).hasSize(2);
}

@Test
public void setMockMode_shouldToggleEngines() {
TestLocationListener listener = new TestLocationListener();
LocationRequest request = LocationRequest.create();
api.requestLocationUpdates(request, listener);
api.setMockMode(true);
api.requestLocationUpdates(request, listener);
assertThat(api.getListeners()).hasSize(2);
}

@Test
public void requestLocationUpdates_shouldNotRegisterListenersWithMockModeOn() throws Exception {
api.setMockMode(true);
Expand Down

0 comments on commit 8c32e5b

Please sign in to comment.