Skip to content

Commit

Permalink
Update dependencies (#181)
Browse files Browse the repository at this point in the history
* Updates pelias-android-sdk, on-the-road, and lost

* Overlay manager should wait for Lost connection callbacks before manipulating map data

Also adds logic to make sure current overlay manager (recreated when the activity is destroyed) receives the callback. Fixes crash in sample app on rotation.

* Hooray for one line fixes!

Tests now passing.
  • Loading branch information
ecgreb authored Sep 26, 2016
1 parent a90e960 commit 694958c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 17 deletions.
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ assemble.doFirst {
dependencies {
compile 'com.android.support:appcompat-v7:23.4.0'
compile "com.mapzen.tangram:tangram:$tangram_version"
compile 'com.mapzen.android:lost:1.1.1'
compile 'com.mapzen:on-the-road:0.8.4'
compile 'com.mapzen.android:pelias-android-sdk:0.7.2'
compile 'com.mapzen.android:lost:2.0.0'
compile 'com.mapzen:on-the-road:1.0.0'
compile 'com.mapzen.android:pelias-android-sdk:1.0.0'
compile 'com.google.dagger:dagger:2.0'
compile 'javax.annotation:javax.annotation-api:1.2'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public class OverlayManager implements TouchInput.PanResponder {
@Override public void onLocationChanged(Location location) {
handleLocationChange(location);
}

@Override public void onProviderEnabled(String provider) {
}

@Override public void onProviderDisabled(String provider) {
}
};

View.OnClickListener findMeExternalClickListener;
Expand All @@ -96,14 +102,34 @@ public class OverlayManager implements TouchInput.PanResponder {
private static MapDataManager mapDataManager;
private MapStateManager mapStateManager;

private static class OverlayManagerConnectionCallbacks
implements LostApiClient.ConnectionCallbacks {
private OverlayManager overlayManager;

@Override public void onConnected() {
if (overlayManager != null) {
overlayManager.enableLocationLayer();
}
}

@Override public void onConnectionSuspended() {
}

public void setOverlayManager(OverlayManager overlayManager) {
this.overlayManager = overlayManager;
}
};

private static OverlayManagerConnectionCallbacks connectionCallbacks =
new OverlayManagerConnectionCallbacks();

/**
* Create a new {@link OverlayManager} object for handling functionality between map and
* location services using the {@link LocationFactory}'s shared {@link LostApiClient}.
*/
OverlayManager(MapView mapView, MapController mapController, MapDataManager mapDataManager,
MapStateManager mapStateManager) {
this(mapView, mapController, mapDataManager, mapStateManager, LocationFactory.sharedClient(
mapView.getContext()));
this(mapView, mapController, mapDataManager, mapStateManager, null);
}

/**
Expand All @@ -112,6 +138,10 @@ public class OverlayManager implements TouchInput.PanResponder {
*/
OverlayManager(MapView mapView, MapController mapController, MapDataManager mapDataManager,
MapStateManager mapStateManager, LostApiClient lostApiClient) {
if (lostApiClient == null) {
lostApiClient = LocationFactory.sharedClient(mapView.getContext(), connectionCallbacks);
}

this.mapView = mapView;
this.mapController = mapController;
this.mapDataManager = mapDataManager;
Expand Down Expand Up @@ -614,16 +644,28 @@ private void removeCurrentLocationMapData() {

private void handleMyLocationEnabledChanged() {
if (myLocationEnabled) {
enableLocationLayer();
} else {
disableLocationLayer();
}
}

private void enableLocationLayer() {
if (!lostApiClient.isConnected()) {
connectionCallbacks.setOverlayManager(this);
lostApiClient.connect();
} else {
showLastKnownLocation();
showFindMe();
requestLocationUpdates();
} else {
hideFindMe();
removeLocationUpdates();
}
}

private void disableLocationLayer() {
hideFindMe();
removeLocationUpdates();
}

private void showFindMe() {
final ImageButton button = mapView.showFindMe();
button.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -651,14 +693,14 @@ private void centerMap() {
}

private void showLastKnownLocation() {
final Location current = LocationServices.FusedLocationApi.getLastLocation();
final Location current = LocationServices.FusedLocationApi.getLastLocation(lostApiClient);
if (current != null) {
updateCurrentLocationMapData(current);
}
}

private void centerMapOnLastKnownLocation() {
final Location current = LocationServices.FusedLocationApi.getLastLocation();
final Location current = LocationServices.FusedLocationApi.getLastLocation(lostApiClient);
if (current != null) {
updateMapPosition(current);
}
Expand All @@ -669,11 +711,14 @@ private void requestLocationUpdates() {
.setInterval(LOCATION_REQUEST_INTERVAL_MILLIS)
.setFastestInterval(LOCATION_REQUEST_DISPLACEMENT_MILLIS)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(locationRequest, locationListener);
LocationServices.FusedLocationApi.requestLocationUpdates(lostApiClient, locationRequest,
locationListener);
}

private void removeLocationUpdates() {
lostApiClient.disconnect();
if (lostApiClient.isConnected()) {
lostApiClient.disconnect();
}
}

private void handleLocationChange(Location location) {
Expand All @@ -696,8 +741,10 @@ private void checkFindMeAndCenterMap(Location location) {
}

private void updateCurrentLocationMapData(final Location location) {
currentLocationMapData.clear();
currentLocationMapData.addPoint(convertLocation(location), null);
if (currentLocationMapData != null) {
currentLocationMapData.clear();
currentLocationMapData.addPoint(convertLocation(location), null);
}
}

private void updateMapPosition(Location location) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapzen.android.location;

import com.mapzen.android.lost.api.LostApiClient;
import com.mapzen.android.lost.api.LostApiClient.ConnectionCallbacks;

import android.content.Context;

Expand All @@ -21,6 +22,16 @@ public static LostApiClient sharedClient(Context context) {
return shared;
}

/**
* Returns shared {@link LostApiClient} with {@link ConnectionCallbacks}.
*/
public static LostApiClient sharedClient(Context context, ConnectionCallbacks callbacks) {
if (shared == null) {
shared = new LostApiClient.Builder(context).addConnectionCallbacks(callbacks).build();
}
return shared;
}

/**
* Returns a new instance of {@link LostApiClient}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import android.view.View;
import android.widget.ImageButton;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -57,12 +59,15 @@
lostApiClient = mock(LostApiClient.class);
mapView = mock(MapView.class);
mapData = new TestMapData("test");
LocationServices.FusedLocationApi = Mockito.mock(FusedLocationProviderApiImpl.class);
setFinalStatic(LocationServices.class.getDeclaredField("FusedLocationApi"),
Mockito.mock(FusedLocationProviderApiImpl.class));
MapDataManager mapDataManager = new MapDataManager();
mapStateManager = mock(MapStateManager.class);
overlayManager = new OverlayManager(mapView, mapController, mapDataManager, mapStateManager,
lostApiClient);
when(LocationServices.FusedLocationApi.getLastLocation()).thenReturn(new Location("test"));
when(LocationServices.FusedLocationApi.getLastLocation(lostApiClient))
.thenReturn(new Location("test"));
when(lostApiClient.isConnected()).thenReturn(true);
findMeButton = new TestButton(null);
when(mapController.addDataLayer(any(String.class))).thenReturn(mapData);
when(mapView.showFindMe()).thenReturn(findMeButton);
Expand Down Expand Up @@ -136,7 +141,7 @@
overlayManager.setMyLocationEnabled(true);
findMeButton.performClick();
verify(mapStateManager).setZoom(16f);
Location loc = LocationServices.FusedLocationApi.getLastLocation();
Location loc = LocationServices.FusedLocationApi.getLastLocation(lostApiClient);
LngLat lngLat = new LngLat(loc.getLongitude(), loc.getLatitude());
verify(mapStateManager).setPosition(lngLat);
}
Expand Down Expand Up @@ -471,4 +476,13 @@ public TestButton(Context context) {
return activated;
}
}

static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
// remove final modifier from field
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
}
2 changes: 2 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apply plugin: 'com.android.application'
apply plugin: 'checkstyle'

def VECTOR_TILES_KEY = hasProperty('vectorTilesKey') ? '"' + vectorTilesKey + '"' : "null";
def SEARCH_KEY = hasProperty('searchKey') ? '"' + searchKey + '"' : "null";
def TURN_BY_TURN_KEY = hasProperty('turnByTurnKey') ? '"' + turnByTurnKey + '"' : "null";

android {
Expand All @@ -15,6 +16,7 @@ android {
versionCode 1
versionName "1.0"
buildConfigField "String", "VECTOR_TILES_KEY", VECTOR_TILES_KEY
buildConfigField "String", "SEARCH_KEY", SEARCH_KEY
buildConfigField "String", "TURN_BY_TURN_KEY", TURN_BY_TURN_KEY
}
buildTypes {
Expand Down

0 comments on commit 694958c

Please sign in to comment.