From eadc51aea0e73a9cde18f56cf73f48caa46a52d2 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Mon, 26 Feb 2018 13:00:04 -0500 Subject: [PATCH 1/3] Fix NPE with MapRoute click listener --- .../ui/v5/route/NavigationMapRoute.java | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java index 90c0c922454..6f961bf9d26 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java @@ -106,14 +106,14 @@ public class NavigationMapRoute implements ProgressChangeListener, MapView.OnMap @DrawableRes private int destinationWaypointIcon; - private List directionsRoutes; - private HashMap routeLineStrings; private final MapboxNavigation navigation; private final MapboxMap mapboxMap; - private List layerIds; + private final HashMap routeLineStrings; + private final List featureCollections; + private final List directionsRoutes; + private final List layerIds; private final MapView mapView; private int primaryRouteIndex; - private final List featureCollections; private float routeScale; private float alternativeRouteScale; private String belowLayer; @@ -206,17 +206,18 @@ public NavigationMapRoute(@Nullable MapboxNavigation navigation, @NonNull MapVie this.navigation = navigation; this.belowLayer = belowLayer; featureCollections = new ArrayList<>(); - alternativesVisible = true; - addListeners(); + directionsRoutes = new ArrayList<>(); + routeLineStrings = new HashMap<>(); + layerIds = new ArrayList<>(); initialize(); + addListeners(); } /** * Adds source and layers to the map. */ private void initialize() { - layerIds = new ArrayList<>(); - routeLineStrings = new HashMap<>(); + alternativesVisible = true; getAttributes(); placeRouteBelow(); } @@ -244,12 +245,10 @@ public void addRoute(DirectionsRoute directionsRoute) { * @since 0.8.0 */ public void addRoutes(@NonNull @Size(min = 1) List directionsRoutes) { - this.directionsRoutes = directionsRoutes; - primaryRouteIndex = 0; clearRoutes(); - if (directionsRoutes.size() == 1) { - alternativesVisible = false; - } + this.directionsRoutes.addAll(directionsRoutes); + primaryRouteIndex = 0; + alternativesVisible = directionsRoutes.size() > 1; generateFeatureCollectionList(directionsRoutes); drawRoutes(); addDirectionWaypoints(); @@ -342,7 +341,15 @@ private void clearRoutes() { mapboxMap.removeLayer(id); } } - featureCollections.clear(); + if (!directionsRoutes.isEmpty()) { + directionsRoutes.clear(); + } + if (!routeLineStrings.isEmpty()) { + routeLineStrings.clear(); + } + if (!featureCollections.isEmpty()) { + featureCollections.clear(); + } } private void generateFeatureCollectionList(List directionsRoutes) { @@ -670,8 +677,10 @@ private void checkNewRouteFound(int currentRouteIndex) { if (currentRouteIndex != primaryRouteIndex) { updateRoute(); if (onRouteSelectionChangeListener != null) { - onRouteSelectionChangeListener.onNewPrimaryRouteSelected( - directionsRoutes.get(primaryRouteIndex)); + if (primaryRouteIndex > 0 && primaryRouteIndex < directionsRoutes.size()) { + DirectionsRoute selectedRoute = directionsRoutes.get(primaryRouteIndex); + onRouteSelectionChangeListener.onNewPrimaryRouteSelected(selectedRoute); + } } } } @@ -720,8 +729,7 @@ public void onMapChanged(int change) { public void onProgressChange(Location location, RouteProgress routeProgress) { // Check if the route's the same as the route currently drawn if (!routeProgress.directionsRoute().equals(directionsRoutes.get(primaryRouteIndex))) { - directionsRoutes.clear(); - directionsRoutes.add(routeProgress.directionsRoute()); + addRoute(routeProgress.directionsRoute()); drawRoutes(); addDirectionWaypoints(); } From e80f0141fb441392cff01aeceb8312d412cea664 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Mon, 26 Feb 2018 13:25:28 -0500 Subject: [PATCH 2/3] Remove unnecessary comments --- .../ui/v5/route/NavigationMapRoute.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java index 6f961bf9d26..2b0aed28353 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java @@ -213,15 +213,6 @@ public NavigationMapRoute(@Nullable MapboxNavigation navigation, @NonNull MapVie addListeners(); } - /** - * Adds source and layers to the map. - */ - private void initialize() { - alternativesVisible = true; - getAttributes(); - placeRouteBelow(); - } - /** * Allows adding a single primary route for the user to traverse along. No alternative routes will * be drawn on top of the map. @@ -594,9 +585,12 @@ private static Feature getPointFromLineString(RouteLeg leg, int stepIndex) { return feature; } - /** - * Adds the necessary listeners - */ + private void initialize() { + alternativesVisible = true; + getAttributes(); + placeRouteBelow(); + } + private void addListeners() { mapboxMap.addOnMapClickListener(this); if (navigation != null) { From 76031abc8071d3cf55a028fc0be8c26820a3d071 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Mon, 26 Feb 2018 13:31:56 -0500 Subject: [PATCH 3/3] Cleanup --- .../ui/v5/route/NavigationMapRoute.java | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java index 2b0aed28353..262eb3d27dc 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/route/NavigationMapRoute.java @@ -327,20 +327,8 @@ private void drawRoutes() { } private void clearRoutes() { - if (!layerIds.isEmpty()) { - for (String id : layerIds) { - mapboxMap.removeLayer(id); - } - } - if (!directionsRoutes.isEmpty()) { - directionsRoutes.clear(); - } - if (!routeLineStrings.isEmpty()) { - routeLineStrings.clear(); - } - if (!featureCollections.isEmpty()) { - featureCollections.clear(); - } + removeLayerIds(); + clearRouteListData(); } private void generateFeatureCollectionList(List directionsRoutes) { @@ -449,6 +437,26 @@ private void addRouteLayer(String layerId, String sourceId, int index) { MapUtils.addLayerToMap(mapboxMap, routeLayer, belowLayer); } + private void removeLayerIds() { + if (!layerIds.isEmpty()) { + for (String id : layerIds) { + mapboxMap.removeLayer(id); + } + } + } + + private void clearRouteListData() { + if (!directionsRoutes.isEmpty()) { + directionsRoutes.clear(); + } + if (!routeLineStrings.isEmpty()) { + routeLineStrings.clear(); + } + if (!featureCollections.isEmpty()) { + featureCollections.clear(); + } + } + /** * Add the route shield layer to the map either using the custom style values or the default. */ @@ -670,11 +678,10 @@ private com.mapbox.geojson.Point findPointOnLine(com.mapbox.geojson.Point clickP private void checkNewRouteFound(int currentRouteIndex) { if (currentRouteIndex != primaryRouteIndex) { updateRoute(); - if (onRouteSelectionChangeListener != null) { - if (primaryRouteIndex > 0 && primaryRouteIndex < directionsRoutes.size()) { - DirectionsRoute selectedRoute = directionsRoutes.get(primaryRouteIndex); - onRouteSelectionChangeListener.onNewPrimaryRouteSelected(selectedRoute); - } + boolean isValidPrimaryIndex = primaryRouteIndex > 0 && primaryRouteIndex < directionsRoutes.size(); + if (isValidPrimaryIndex && onRouteSelectionChangeListener != null) { + DirectionsRoute selectedRoute = directionsRoutes.get(primaryRouteIndex); + onRouteSelectionChangeListener.onNewPrimaryRouteSelected(selectedRoute); } } }