Skip to content

Commit

Permalink
Remove GeoJson sources onDestroy
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Dec 14, 2018
1 parent ce44b28 commit 2848504
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ private void adjustMapPaddingForNavigation() {
}

private void resetMapAfterNavigation() {
navigationMap.updateRouteVisibility(false);
navigationMap.removeRoute();
navigationMap.clearMarkers();
navigation.stopNavigation();
moveCameraOverhead();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public void onMapLongClick(@NonNull LatLng point) {
} else {
originMarker = null;
destinationMarker = null;
navigationMapRoute.updateRouteVisibilityTo(false);
navigationMapRoute.updateRouteArrowVisibilityTo(false);
navigationMapRoute.removeRoute();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class ExampleActivity : HistoryActivity(), ExampleView {
}

override fun removeRoute() {
map?.updateRouteVisibility(false)
map?.removeRoute()
}

override fun clearMarkers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import com.mapbox.api.directions.v5.models.DirectionsRoute;
Expand Down Expand Up @@ -283,16 +284,11 @@ public void showAlternativeRoutes(boolean alternativesVisible) {
}

/**
* This method will update the route visibility.
* <p>
* The visibility will include the main route, alternatives,
* and the upcoming maneuver arrow.
*
* @param isVisible true to show, false otherwise.
* Will remove the drawn route displayed on the map. Does nothing
* if no route is drawn.
*/
public void updateRouteVisibility(boolean isVisible) {
mapRoute.updateRouteVisibilityTo(isVisible);
mapRoute.updateRouteArrowVisibilityTo(isVisible);
public void removeRoute() {
mapRoute.removeRoute();
}

/**
Expand Down Expand Up @@ -427,7 +423,7 @@ public void onStop() {
}

/**
* Should be used in {@link FragmentActivity#onDestroy()} to ensure proper
* Should be used in {@link FragmentActivity#onDestroy()} or {@link Fragment#onDestroyView()} to ensure proper
* accounting for the lifecycle.
*/
public void onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.Point;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -19,13 +20,13 @@
class FeatureProcessingTask extends AsyncTask<Void, Void, Void> {

private final List<DirectionsRoute> routes;
private final OnRouteFeaturesProcessedCallback callback;
private final List<FeatureCollection> routeFeatureCollections = new ArrayList<>();
private final WeakReference<OnRouteFeaturesProcessedCallback> callbackWeakReference;
private final HashMap<LineString, DirectionsRoute> routeLineStrings = new HashMap<>();

FeatureProcessingTask(List<DirectionsRoute> routes, OnRouteFeaturesProcessedCallback callback) {
this.routes = routes;
this.callback = callback;
this.callbackWeakReference = new WeakReference<>(callback);
}

@Override
Expand All @@ -42,7 +43,11 @@ protected Void doInBackground(Void... voids) {
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
callback.onRouteFeaturesProcessed(routeFeatureCollections, routeLineStrings);
Runtime.getRuntime().gc();
OnRouteFeaturesProcessedCallback callback = callbackWeakReference.get();
if (callback != null) {
callback.onRouteFeaturesProcessed(routeFeatureCollections, routeLineStrings);
}
}

private FeatureCollection createRouteFeatureCollection(DirectionsRoute route, boolean isPrimary) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.support.v7.content.res.AppCompatResources;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.maps.MapView;
Expand Down Expand Up @@ -86,8 +87,6 @@ class MapRouteArrow {
private List<Layer> arrowLayers;
private GeoJsonSource arrowShaftGeoJsonSource;
private GeoJsonSource arrowHeadGeoJsonSource;
private Feature arrowShaftGeoJsonFeature = Feature.fromGeometry(Point.fromLngLat(0, 0));
private Feature arrowHeadGeoJsonFeature = Feature.fromGeometry(Point.fromLngLat(0, 0));

private final MapView mapView;
private final MapboxMap mapboxMap;
Expand Down Expand Up @@ -135,6 +134,8 @@ void onDestroy() {
for (Layer arrowLayer : arrowLayers) {
mapboxMap.removeLayer(arrowLayer);
}
mapboxMap.removeSource(arrowHeadGeoJsonSource);
mapboxMap.removeSource(arrowShaftGeoJsonSource);
}

private List<Point> obtainArrowPointsFrom(RouteProgress routeProgress) {
Expand All @@ -157,13 +158,13 @@ private List<Point> obtainArrowPointsFrom(RouteProgress routeProgress) {

private void updateArrowShaftWith(List<Point> points) {
LineString shaft = LineString.fromLngLats(points);
arrowShaftGeoJsonFeature = Feature.fromGeometry(shaft);
Feature arrowShaftGeoJsonFeature = Feature.fromGeometry(shaft);
arrowShaftGeoJsonSource.setGeoJson(arrowShaftGeoJsonFeature);
}

private void updateArrowHeadWith(List<Point> points) {
double azimuth = TurfMeasurement.bearing(points.get(points.size() - 2), points.get(points.size() - 1));
arrowHeadGeoJsonFeature = Feature.fromGeometry(points.get(points.size() - 1));
Feature arrowHeadGeoJsonFeature = Feature.fromGeometry(points.get(points.size() - 1));
arrowHeadGeoJsonFeature.addNumberProperty(ARROW_BEARING, (float) MathUtils.wrap(azimuth, 0, MAX_DEGREES));
arrowHeadGeoJsonSource.setGeoJson(arrowHeadGeoJsonFeature);
}
Expand All @@ -186,13 +187,13 @@ private void initialize() {
mapboxMap.addLayerAbove(shaftLayer, headCasingLayer.getId());
mapboxMap.addLayerAbove(headLayer, shaftLayer.getId());

initializeArrowLayers(shaftLayer, shaftCasingLayer, headLayer, headCasingLayer);
createArrowLayerList(shaftLayer, shaftCasingLayer, headLayer, headCasingLayer);
}

private void initializeArrowShaft() {
arrowShaftGeoJsonSource = new GeoJsonSource(
ARROW_SHAFT_SOURCE_ID,
arrowShaftGeoJsonFeature,
FeatureCollection.fromFeatures(new Feature[] {}),
new GeoJsonOptions().withMaxZoom(16)
);
mapboxMap.addSource(arrowShaftGeoJsonSource);
Expand All @@ -201,7 +202,7 @@ private void initializeArrowShaft() {
private void initializeArrowHead() {
arrowHeadGeoJsonSource = new GeoJsonSource(
ARROW_HEAD_SOURCE_ID,
arrowShaftGeoJsonFeature,
FeatureCollection.fromFeatures(new Feature[] {}),
new GeoJsonOptions().withMaxZoom(16)
);
mapboxMap.addSource(arrowHeadGeoJsonSource);
Expand Down Expand Up @@ -340,8 +341,8 @@ private SymbolLayer createArrowHeadCasingLayer() {
);
}

private void initializeArrowLayers(LineLayer shaftLayer, LineLayer shaftCasingLayer, SymbolLayer headLayer,
SymbolLayer headCasingLayer) {
private void createArrowLayerList(LineLayer shaftLayer, LineLayer shaftCasingLayer, SymbolLayer headLayer,
SymbolLayer headCasingLayer) {
arrowLayers = new ArrayList<>();
arrowLayers.add(shaftCasingLayer);
arrowLayers.add(shaftLayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void updateVisibilityTo(boolean isVisible) {
updateAllLayersVisibilityTo(isVisible);
}

boolean retrieveVisibilty() {
boolean retrieveVisibility() {
return isVisible;
}

Expand Down Expand Up @@ -236,6 +236,8 @@ void onDestroy() {
for (Layer routeLayer : routeLayers) {
mapboxMap.removeLayer(routeLayer);
}
mapboxMap.removeSource(routeLineSource);
mapboxMap.removeSource(wayPointSource);
}

private void drawRoutes(List<FeatureCollection> routeFeatureCollections) {
Expand Down Expand Up @@ -266,18 +268,20 @@ private void clearRouteListData() {
}

private void generateRouteFeatureCollectionsFrom(List<DirectionsRoute> routes) {
new FeatureProcessingTask(routes, new OnRouteFeaturesProcessedCallback() {
@Override
public void onRouteFeaturesProcessed(List<FeatureCollection> routeFeatureCollections,
HashMap<LineString, DirectionsRoute> routeLineStrings) {
MapRouteLine.this.routeFeatureCollections.addAll(routeFeatureCollections);
MapRouteLine.this.routeLineStrings.putAll(routeLineStrings);
drawRoutes(routeFeatureCollections);
drawWayPoints();
}
}).execute();
new FeatureProcessingTask(routes, routeFeaturesProcessedCallback).execute();
}

private OnRouteFeaturesProcessedCallback routeFeaturesProcessedCallback = new OnRouteFeaturesProcessedCallback() {
@Override
public void onRouteFeaturesProcessed(List<FeatureCollection> routeFeatureCollections,
HashMap<LineString, DirectionsRoute> routeLineStrings) {
MapRouteLine.this.routeFeatureCollections.addAll(routeFeatureCollections);
MapRouteLine.this.routeLineStrings.putAll(routeLineStrings);
drawRoutes(routeFeatureCollections);
drawWayPoints();
}
};

private void drawWayPoints() {
DirectionsRoute primaryRoute = directionsRoutes.get(primaryRouteIndex);
wayPointFeatureCollection = buildWayPointFeatureCollectionFrom(primaryRoute);
Expand Down Expand Up @@ -306,14 +310,16 @@ private void updateRoutesFor(int newPrimaryIndex) {
if (newPrimaryIndex < 0 || newPrimaryIndex > routeFeatureCollections.size() - 1) {
return;
}
new PrimaryRouteUpdateTask(newPrimaryIndex, routeFeatureCollections, new OnPrimaryRouteUpdatedCallback() {
@Override
public void onPrimaryRouteUpdated(List<FeatureCollection> updatedRouteCollections) {
drawRoutes(updatedRouteCollections);
}
}).execute();
new PrimaryRouteUpdateTask(newPrimaryIndex, routeFeatureCollections, primaryRouteUpdatedCallback).execute();
}

private OnPrimaryRouteUpdatedCallback primaryRouteUpdatedCallback = new OnPrimaryRouteUpdatedCallback() {
@Override
public void onPrimaryRouteUpdated(List<FeatureCollection> updatedRouteCollections) {
drawRoutes(updatedRouteCollections);
}
};

private void findRouteBelowLayerId() {
if (belowLayer == null || belowLayer.isEmpty()) {
List<Layer> styleLayers = mapboxMap.getLayers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public class NavigationMapRoute implements LifecycleObserver {
private final String belowLayer;
private final MapboxMap mapboxMap;
private final MapView mapView;
private final MapRouteClickListener mapRouteClickListener;
private final MapRouteProgressChangeListener mapRouteProgressChangeListener;
private MapRouteClickListener mapRouteClickListener;
private MapRouteProgressChangeListener mapRouteProgressChangeListener;
private boolean isMapClickListenerAdded = false;
private MapView.OnDidFinishLoadingStyleListener didFinishLoadingStyleListener;
private boolean isDidFinishLoadingStyleListenerAdded = false;
Expand Down Expand Up @@ -207,6 +207,14 @@ public void addRoutes(@NonNull @Size(min = 1) List<DirectionsRoute> directionsRo
routeLine.draw(directionsRoutes);
}

/**
* Hides all routes / route arrows on the map drawn by this class.
*/
public void removeRoute() {
updateRouteVisibilityTo(false);
updateRouteArrowVisibilityTo(false);
}

/**
* Hides all routes on the map drawn by this class.
*
Expand Down Expand Up @@ -301,8 +309,8 @@ public void onStop() {
}

/**
* This method should be added in your {@link Activity#onDestroy()} or {@link Fragment#onDestroyView()}
* to handle removing resources that were added to the map.
* This method should be added in your {@link Activity#onDestroy()} or
* {@link android.support.v4.app.Fragment#onDestroyView()} to handle removing resources that were added to the map.
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
Expand Down Expand Up @@ -352,8 +360,16 @@ private void redraw() {
List<DirectionsRoute> routes = routeLine.retrieveDirectionsRoutes();
boolean alternativesVisible = routeLine.retrieveAlternativesVisible();
int primaryRouteIndex = routeLine.retrievePrimaryRouteIndex();
boolean isVisible = routeLine.retrieveVisibilty();
routeLine = new MapRouteLine(mapView.getContext(), mapboxMap, styleRes, belowLayer);
boolean isVisible = routeLine.retrieveVisibility();
buildNewRouteLine();
routeLine.redraw(routes, alternativesVisible, primaryRouteIndex, isVisible);
}

private void buildNewRouteLine() {
routeLine = new MapRouteLine(mapView.getContext(), mapboxMap, styleRes, belowLayer);
mapboxMap.removeOnMapClickListener(mapRouteClickListener);
mapRouteClickListener = new MapRouteClickListener(routeLine);
mapboxMap.addOnMapClickListener(mapRouteClickListener);
mapRouteProgressChangeListener = new MapRouteProgressChangeListener(routeLine, routeArrow);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -15,13 +16,13 @@ class PrimaryRouteUpdateTask extends AsyncTask<Void, Void, List<FeatureCollectio

private final int newPrimaryIndex;
private final List<FeatureCollection> routeFeatureCollections;
private final OnPrimaryRouteUpdatedCallback callback;
private final WeakReference<OnPrimaryRouteUpdatedCallback> callbackWeakReference;

PrimaryRouteUpdateTask(int newPrimaryIndex, List<FeatureCollection> routeFeatureCollections,
OnPrimaryRouteUpdatedCallback callback) {
OnPrimaryRouteUpdatedCallback callback) {
this.newPrimaryIndex = newPrimaryIndex;
this.routeFeatureCollections = routeFeatureCollections;
this.callback = callback;
this.callbackWeakReference = new WeakReference<>(callback);
}

@Override
Expand Down Expand Up @@ -52,6 +53,10 @@ protected List<FeatureCollection> doInBackground(Void... voids) {

@Override
protected void onPostExecute(List<FeatureCollection> updatedRouteCollections) {
callback.onPrimaryRouteUpdated(updatedRouteCollections);
Runtime.getRuntime().gc();
OnPrimaryRouteUpdatedCallback callback = callbackWeakReference.get();
if (callback != null) {
callback.onPrimaryRouteUpdated(updatedRouteCollections);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,48 @@ public void updateRouteVisibilityTo_routeLineVisibilityIsUpdated() {
verify(mockedMapRouteLine).updateVisibilityTo(isVisible);
}

@Test
public void removeRoute_routeLineVisibilityIsUpdated() {
MapboxNavigation mockedNavigation = mock(MapboxNavigation.class);
MapView mockedMapView = mock(MapView.class);
MapboxMap mockedMapboxMap = mock(MapboxMap.class);
int mockedStyleRes = 0;
MapRouteClickListener mockedMapClickListener = mock(MapRouteClickListener.class);
MapView.OnDidFinishLoadingStyleListener mockedDidFinishLoadingStyleListener =
mock(MapView.OnDidFinishLoadingStyleListener.class);
MapRouteProgressChangeListener mockedProgressChangeListener = mock(MapRouteProgressChangeListener.class);
MapRouteLine mockedMapRouteLine = mock(MapRouteLine.class);
MapRouteArrow mockedMapRouteArrow = mock(MapRouteArrow.class);
NavigationMapRoute theNavigationMapRoute = new NavigationMapRoute(mockedNavigation, mockedMapView, mockedMapboxMap,
mockedStyleRes, "", mockedMapClickListener, mockedDidFinishLoadingStyleListener,
mockedProgressChangeListener, mockedMapRouteLine, mockedMapRouteArrow);

theNavigationMapRoute.removeRoute();

verify(mockedMapRouteLine).updateVisibilityTo(false);
}

@Test
public void removeRoute_routeArrowVisibilityIsUpdated() {
MapboxNavigation mockedNavigation = mock(MapboxNavigation.class);
MapView mockedMapView = mock(MapView.class);
MapboxMap mockedMapboxMap = mock(MapboxMap.class);
int mockedStyleRes = 0;
MapRouteClickListener mockedMapClickListener = mock(MapRouteClickListener.class);
MapView.OnDidFinishLoadingStyleListener mockedDidFinishLoadingStyleListener =
mock(MapView.OnDidFinishLoadingStyleListener.class);
MapRouteProgressChangeListener mockedProgressChangeListener = mock(MapRouteProgressChangeListener.class);
MapRouteLine mockedMapRouteLine = mock(MapRouteLine.class);
MapRouteArrow mockedMapRouteArrow = mock(MapRouteArrow.class);
NavigationMapRoute theNavigationMapRoute = new NavigationMapRoute(mockedNavigation, mockedMapView, mockedMapboxMap,
mockedStyleRes, "", mockedMapClickListener, mockedDidFinishLoadingStyleListener,
mockedProgressChangeListener, mockedMapRouteLine, mockedMapRouteArrow);

theNavigationMapRoute.removeRoute();

verify(mockedMapRouteArrow).updateVisibilityTo(false);
}

@Test
public void updateRouteVisibilityTo_progressChangeVisibilityIsUpdated() {
MapboxNavigation mockedNavigation = mock(MapboxNavigation.class);
Expand Down

0 comments on commit 2848504

Please sign in to comment.