From 38bf54688d5591d2275811c2f0a11d60b1463854 Mon Sep 17 00:00:00 2001 From: tobrun Date: Mon, 10 Dec 2018 12:02:54 +0100 Subject: [android] - throw exception when another style is loading, correct RuntimeStyleTest when removing layer at, add javadoc to isFullyLoaded --- .../mapboxsdk/location/LocationComponent.java | 2 +- .../main/java/com/mapbox/mapboxsdk/maps/Style.java | 46 ++++++++++++++-------- .../mapboxsdk/testapp/style/RuntimeStyleTests.java | 2 +- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationComponent.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationComponent.java index 59ce2a900e..340e84abef 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationComponent.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationComponent.java @@ -916,7 +916,7 @@ public final class LocationComponent { } if (!style.isFullyLoaded()) { - throw new IllegalStateException("Style hasn't fully loaded yet, can't initialize LocationComponent."); + throw new IllegalStateException("Style is invalid, provide the most recently loaded one."); } this.style = style; diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java index c803042242..94b5b73378 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java @@ -103,8 +103,9 @@ public class Style { */ public void addSource(@NonNull Source source) { if (!fullyLoaded) { - // we are loading a new style - return; + throw new IllegalStateException( + "Calling addSource on old Style instance, use the more recently loaded Style instead." + ); } sources.put(source.getId(), source); @@ -195,8 +196,9 @@ public class Style { */ public void addLayer(@NonNull Layer layer) { if (!fullyLoaded) { - // we are loading a new style - return; + throw new IllegalStateException( + "Calling addLayer on old Style instance, use the more recently loaded Style instead." + ); } layers.put(layer.getId(), layer); @@ -211,8 +213,9 @@ public class Style { */ public void addLayerBelow(@NonNull Layer layer, @NonNull String below) { if (!fullyLoaded) { - // we are loading a new style - return; + throw new IllegalStateException( + "Calling addLayerBelow on old Style instance, use the more recently loaded Style instead." + ); } layers.put(layer.getId(), layer); @@ -227,8 +230,9 @@ public class Style { */ public void addLayerAbove(@NonNull Layer layer, @NonNull String above) { if (!fullyLoaded) { - // we are loading a new style - return; + throw new IllegalStateException( + "Calling addLayerAbove on old Style instance, use the more recently loaded Style instead." + ); } layers.put(layer.getId(), layer); @@ -244,8 +248,9 @@ public class Style { */ public void addLayerAt(@NonNull Layer layer, @IntRange(from = 0) int index) { if (!fullyLoaded) { - // we are loading a new style - return; + throw new IllegalStateException( + "Calling addLayerAt on old Style instance, use the more recently loaded Style instead." + ); } layers.put(layer.getId(), layer); @@ -368,8 +373,9 @@ public class Style { */ public void addImage(@NonNull String name, @NonNull Bitmap image, boolean sdf) { if (!fullyLoaded) { - // we are loading a new style - return; + throw new IllegalStateException( + "Calling addImage on old Style instance, use the more recently loaded Style instead." + ); } nativeMapView.addImage(name, image, sdf); @@ -380,8 +386,9 @@ public class Style { */ public void addImages(@NonNull HashMap images) { if (!fullyLoaded) { - // we are loading a new style - return; + throw new IllegalStateException( + "Calling addImages on old Style instance, use the more recently loaded Style instead." + ); } nativeMapView.addImages(images); @@ -431,8 +438,9 @@ public class Style { */ public void setTransition(@NonNull TransitionOptions transitionOptions) { if (!fullyLoaded) { - // we are loading a new style - return; + throw new IllegalStateException( + "Calling setTransition on old Style instance, use the more recently loaded Style instead." + ); } nativeMapView.setTransitionDuration(transitionOptions.getDuration()); @@ -541,6 +549,12 @@ public class Style { } } + /** + * Returns true if the style is fully loaded. Returns false if style hasn't been fully loaded or a new style is + * underway of being loaded. + * + * @return True if fully loaded, false otherwise + */ public boolean isFullyLoaded() { return fullyLoaded; } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java index 141ddb42da..e7e7c767a4 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java @@ -133,7 +133,7 @@ public class RuntimeStyleTests extends BaseActivityTest { // Test remove by index bounds checks Timber.i("Remove layer at index > size"); - assertNull(mapboxMap.getStyle().removeLayerAt(Integer.MAX_VALUE)); + assertFalse(mapboxMap.getStyle().removeLayerAt(Integer.MAX_VALUE)); } }); } -- cgit v1.2.1