From 0e0536a8d7203fbf3883c0e148cb509188d2b496 Mon Sep 17 00:00:00 2001 From: Pablo Guardiola Date: Tue, 25 Jul 2017 13:01:31 +0200 Subject: [PATCH] [android] fix added to map checks because LongSparseArray#indexOfKey could return a negative number when the specified key is not mapped (#9602) --- .../mapboxsdk/maps/AnnotationManager.java | 20 +++++++++++++++++++ .../mapboxsdk/maps/MarkerContainer.java | 11 ---------- .../mapboxsdk/maps/PolygonContainer.java | 11 ---------- .../mapboxsdk/maps/PolylineContainer.java | 11 ---------- 4 files changed, 20 insertions(+), 33 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AnnotationManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AnnotationManager.java index 22e5cf4322e..960f71ca136 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AnnotationManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AnnotationManager.java @@ -181,6 +181,10 @@ List addMarkers(@NonNull List markerOptions } void updateMarker(@NonNull Marker updatedMarker, @NonNull MapboxMap mapboxMap) { + if (!isAddedToMap(updatedMarker)) { + logNonAdded(updatedMarker); + return; + } markers.update(updatedMarker, mapboxMap); } @@ -230,6 +234,10 @@ List addPolygons(@NonNull List polygonOptionsList, @Non } void updatePolygon(Polygon polygon) { + if (!isAddedToMap(polygon)) { + logNonAdded(polygon); + return; + } polygons.update(polygon); } @@ -256,6 +264,10 @@ List addPolylines(@NonNull List polylineOptionsList, } void updatePolyline(Polyline polyline) { + if (!isAddedToMap(polyline)) { + logNonAdded(polyline); + return; + } polylines.update(polyline); } @@ -365,6 +377,14 @@ void adjustTopOffsetPixels(MapboxMap mapboxMap) { } } + private boolean isAddedToMap(Annotation annotation) { + return annotation != null && annotation.getId() != -1 && annotationsArray.indexOfKey(annotation.getId()) > -1; + } + + private void logNonAdded(Annotation annotation) { + Timber.w("Attempting to update non-added %s with value %s", annotation.getClass().getCanonicalName(), annotation); + } + // // Click event // diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MarkerContainer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MarkerContainer.java index ef8054d70e2..b6bee199a30 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MarkerContainer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MarkerContainer.java @@ -18,8 +18,6 @@ import java.util.ArrayList; import java.util.List; -import timber.log.Timber; - /** * Encapsulates {@link Marker}'s functionality. */ @@ -92,11 +90,6 @@ public List addBy(@NonNull List markerOptio @Override public void update(@NonNull Marker updatedMarker, @NonNull MapboxMap mapboxMap) { - if (!isAddedToMap(updatedMarker)) { - Timber.w("Attempting to update non-added Marker with value %s", updatedMarker); - return; - } - ensureIconLoaded(updatedMarker, mapboxMap); nativeMapView.updateMarker(updatedMarker); annotations.setValueAt(annotations.indexOfKey(updatedMarker.getId()), updatedMarker); @@ -237,10 +230,6 @@ private Marker prepareMarker(BaseMarkerOptions markerOptions) { return marker; } - private boolean isAddedToMap(Annotation annotation) { - return annotation != null && annotation.getId() != -1 && annotations.indexOfKey(annotation.getId()) != -1; - } - private void ensureIconLoaded(Marker marker, MapboxMap mapboxMap) { if (!(marker instanceof MarkerView)) { iconManager.ensureIconLoaded(marker, mapboxMap); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/PolygonContainer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/PolygonContainer.java index bcb94a975cf..e626fc3dc06 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/PolygonContainer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/PolygonContainer.java @@ -11,8 +11,6 @@ import java.util.ArrayList; import java.util.List; -import timber.log.Timber; - /** * Encapsulates {@link Polygon}'s functionality. */ @@ -76,11 +74,6 @@ public List addBy(@NonNull List polygonOptionsList, @No @Override public void update(Polygon polygon) { - if (!isAddedToMap(polygon)) { - Timber.w("Attempting to update non-added Polygon with value %s", polygon); - return; - } - nativeMapView.updatePolygon(polygon); annotations.setValueAt(annotations.indexOfKey(polygon.getId()), polygon); } @@ -97,8 +90,4 @@ public List obtainAll() { } return polygons; } - - private boolean isAddedToMap(Annotation annotation) { - return annotation != null && annotation.getId() != -1 && annotations.indexOfKey(annotation.getId()) != -1; - } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/PolylineContainer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/PolylineContainer.java index 397b70a158f..b9a358c0f90 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/PolylineContainer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/PolylineContainer.java @@ -11,8 +11,6 @@ import java.util.ArrayList; import java.util.List; -import timber.log.Timber; - /** * Encapsulates {@link Polyline}'s functionality. */ @@ -78,11 +76,6 @@ public List addBy(@NonNull List polylineOptionsList, @Override public void update(Polyline polyline) { - if (!isAddedToMap(polyline)) { - Timber.w("Attempting to update non-added Polyline with value %s", polyline); - return; - } - nativeMapView.updatePolyline(polyline); annotations.setValueAt(annotations.indexOfKey(polyline.getId()), polyline); } @@ -99,8 +92,4 @@ public List obtainAll() { } return polylines; } - - private boolean isAddedToMap(Annotation annotation) { - return annotation != null && annotation.getId() != -1 && annotations.indexOfKey(annotation.getId()) != -1; - } }