From daafc0b545185d62b0691c204e0486b2f75c7c9d Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Sun, 26 Sep 2021 04:10:12 +0300 Subject: [PATCH] Add OnDidFailLoadingTileListener. (#704) * Add OnDidFailLoadingTileListener. * Fix typo. * Add unit tests. * Bump gl-core to 5.3.0 * Add changelog for 9.7.0 release. --- CHANGELOG.md | 10 ++++- .../mapboxsdk/maps/MapChangeReceiver.java | 25 ++++++++++++ .../com/mapbox/mapboxsdk/maps/MapView.java | 39 +++++++++++++++++-- .../mapbox/mapboxsdk/maps/NativeMapView.java | 9 +++++ .../mapboxsdk/maps/MapChangeReceiverTest.java | 33 ++++++++++++++++ gradle/dependencies.gradle | 2 +- 6 files changed, 113 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c90d13fa1..22b427700 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,16 @@ Mapbox welcomes participation and contributions from everyone. Please read [`Contributing Guide`](https://github.com/mapbox/mapbox-gl-native/blob/master/CONTRIBUTING.md) to get started. +## 9.7.0 - September 24, 2021 +[Changes](https://github.com/mapbox/mapbox-gl-native-android/compare/android-v9.6.2...android-v9.7.0) since [Mapbox Maps SDK for Android 9.6.2](https://github.com/mapbox/mapbox-gl-native-android/releases/tag/android-v9.6.2) +### Improvements and bug fixes + - Fix the race condition when updating LocationCompoent's position. ([#703](https://github.com/mapbox/mapbox-gl-native-android/pull/703)) + - Handle exceptions thrown at layout initialization (including those thrown from vector tile parsing) and add `OnDidFailLoadingTileListener`, which will be trigged when tile failed to load. ([#704](https://github.com/mapbox/mapbox-gl-native-android/pull/704)) +### Dependencies + - Update core library to 5.3.0 + ## 9.6.2 - July 7, 2021 -[Changes](https://github.com/mapbox/mapbox-gl-native-android/compare/android-v9.6.1...android-v9.6.2) since [Mapbox Maps SDK for Android 9.6.1](https://github.com/mapbox/mapbox-gl-native-android/releases/tag/android-v9.6.0) +[Changes](https://github.com/mapbox/mapbox-gl-native-android/compare/android-v9.6.1...android-v9.6.2) since [Mapbox Maps SDK for Android 9.6.1](https://github.com/mapbox/mapbox-gl-native-android/releases/tag/android-v9.6.1) ### Dependencies - Update telemetry to 8.1.0 (okhttp3 variant) and events-core 5.0.0 to make project compatible with Android 12 diff --git a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapChangeReceiver.java b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapChangeReceiver.java index 5932f6fbf..fd6dfd24b 100644 --- a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapChangeReceiver.java +++ b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapChangeReceiver.java @@ -18,6 +18,8 @@ class MapChangeReceiver implements NativeMapView.StateCallback { = new CopyOnWriteArrayList<>(); private final List onDidFailLoadingMapListenerList = new CopyOnWriteArrayList<>(); + private final List onDidFailLoadingTileListenerList + = new CopyOnWriteArrayList<>(); private final List onWillStartRenderingFrameList = new CopyOnWriteArrayList<>(); private final List onDidFinishRenderingFrameList @@ -120,6 +122,20 @@ public void onDidFailLoadingMap(String error) { } } + @Override + public void onDidFailLoadingTile(String error) { + try { + if (!onDidFailLoadingTileListenerList.isEmpty()) { + for (MapView.OnDidFailLoadingTileListener onDidFailLoadingTileListener : onDidFailLoadingTileListenerList) { + onDidFailLoadingTileListener.onDidFailLoadingTile(error); + } + } + } catch (Throwable err) { + Logger.e(TAG, "Exception in onDidFailLoadingTile", err); + throw err; + } + } + @Override public void onWillStartRenderingFrame() { try { @@ -303,6 +319,14 @@ void removeOnDidFailLoadingMapListener(MapView.OnDidFailLoadingMapListener liste onDidFailLoadingMapListenerList.remove(listener); } + void addOnDidFailLoadingTileListener(MapView.OnDidFailLoadingTileListener listener) { + onDidFailLoadingTileListenerList.add(listener); + } + + void removeOnDidFailLoadingTileListener(MapView.OnDidFailLoadingTileListener listener) { + onDidFailLoadingTileListenerList.remove(listener); + } + void addOnWillStartRenderingFrameListener(MapView.OnWillStartRenderingFrameListener listener) { onWillStartRenderingFrameList.add(listener); } @@ -382,6 +406,7 @@ void clear() { onWillStartLoadingMapListenerList.clear(); onDidFinishLoadingMapListenerList.clear(); onDidFailLoadingMapListenerList.clear(); + onDidFailLoadingTileListenerList.clear(); onWillStartRenderingFrameList.clear(); onDidFinishRenderingFrameList.clear(); onWillStartRenderingMapListenerList.clear(); diff --git a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 1c8d8ebb7..f349ed1ca 100644 --- a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -767,6 +767,24 @@ public void removeOnDidFailLoadingMapListener(@NonNull OnDidFailLoadingMapListen mapChangeReceiver.removeOnDidFailLoadingMapListener(listener); } + /** + * Set a callback that's invoked when the tile failed to load. + * + * @param listener The callback that's invoked when the tile failed to load + */ + public void addOnDidFailLoadingTileListener(@NonNull OnDidFailLoadingTileListener listener) { + mapChangeReceiver.addOnDidFailLoadingTileListener(listener); + } + + /** + * Set a callback that's invoked when the tile failed to load. + * + * @param listener The callback that's invoked when the tile failed to load + */ + public void removeOnDidFailLoadingTileListener(@NonNull OnDidFailLoadingTileListener listener) { + mapChangeReceiver.removeOnDidFailLoadingTileListener(listener); + } + /** * Set a callback that's invoked when the map will start rendering a frame. * @@ -1022,6 +1040,21 @@ public interface OnDidFailLoadingMapListener { void onDidFailLoadingMap(String errorMessage); } + /** + * Interface definition for a callback to be invoked when the tile failed to load. + *

+ * {@link MapView#addOnDidFailLoadingTileListener(OnDidFailLoadingTileListener)} + *

+ */ + public interface OnDidFailLoadingTileListener { + /** + * Called when the tile failed to load. + * + * @param errorMessage The reason why the tile failed to load + */ + void onDidFailLoadingTile(String errorMessage); + } + /** * Interface definition for a callback to be invoked when the map will start rendering a frame. *

@@ -1053,7 +1086,7 @@ public interface OnDidFinishRenderingFrameListener { /** * Interface definition for a callback to be invoked when the map will start rendering the map. *

- * {@link MapView#addOnDidFailLoadingMapListener(OnDidFailLoadingMapListener)} + * {@link MapView#addOnWillStartRenderingMapListener(OnWillStartRenderingMapListener)} *

*/ public interface OnWillStartRenderingMapListener { @@ -1098,7 +1131,7 @@ public interface OnDidBecomeIdleListener { /** * Interface definition for a callback to be invoked when the map has loaded the style. *

- * {@link MapView#addOnDidFailLoadingMapListener(OnDidFailLoadingMapListener)} + * {@link MapView#addOnDidFinishLoadingStyleListener(OnDidFinishLoadingStyleListener)} *

*/ public interface OnDidFinishLoadingStyleListener { @@ -1111,7 +1144,7 @@ public interface OnDidFinishLoadingStyleListener { /** * Interface definition for a callback to be invoked when a map source has changed. *

- * {@link MapView#addOnDidFailLoadingMapListener(OnDidFailLoadingMapListener)} + * {@link MapView#addOnSourceChangedListener(OnSourceChangedListener)} *

*/ public interface OnSourceChangedListener { diff --git a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java index 85943f04c..1ac4046b8 100755 --- a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java +++ b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java @@ -1125,6 +1125,13 @@ private void onDidFailLoadingMap(String error) { } } + @Keep + private void onDidFailLoadingTile(String error) { + if (stateCallback != null) { + stateCallback.onDidFailLoadingTile(error); + } + } + @Keep private void onWillStartRenderingFrame() { if (stateCallback != null) { @@ -1645,6 +1652,8 @@ interface StateCallback extends StyleCallback { void onDidFailLoadingMap(String error); + void onDidFailLoadingTile(String error); + void onWillStartRenderingFrame(); void onDidFinishRenderingFrame(boolean fully); diff --git a/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/MapChangeReceiverTest.java b/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/MapChangeReceiverTest.java index 025f06752..a00c8c482 100644 --- a/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/MapChangeReceiverTest.java +++ b/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/MapChangeReceiverTest.java @@ -43,6 +43,9 @@ public class MapChangeReceiverTest { @Mock private MapView.OnDidFailLoadingMapListener onDidFailLoadingMapListener; + @Mock + private MapView.OnDidFailLoadingTileListener onDidFailLoadingTileListener; + @Mock private MapView.OnWillStartRenderingFrameListener onWillStartRenderingFrameListener; @@ -314,6 +317,36 @@ public void testOnDidFailLoadingMapListener() { } } + @Test + public void testOnDidFailLoadingTileListener() { + mapChangeEventManager.addOnDidFailLoadingTileListener(onDidFailLoadingTileListener); + mapChangeEventManager.onDidFailLoadingTile(TEST_STRING); + verify(onDidFailLoadingTileListener).onDidFailLoadingTile(TEST_STRING); + mapChangeEventManager.removeOnDidFailLoadingTileListener(onDidFailLoadingTileListener); + mapChangeEventManager.onDidFailLoadingTile(TEST_STRING); + verify(onDidFailLoadingTileListener).onDidFailLoadingTile(TEST_STRING); + + mapChangeEventManager.addOnDidFailLoadingTileListener(onDidFailLoadingTileListener); + Logger.setLoggerDefinition(loggerDefinition); + Exception exc = new RuntimeException(); + doThrow(exc).when(onDidFailLoadingTileListener).onDidFailLoadingTile(TEST_STRING); + try { + mapChangeEventManager.onDidFailLoadingTile(TEST_STRING); + Assert.fail("The exception should've been re-thrown."); + } catch (RuntimeException throwable) { + verify(loggerDefinition).e(anyString(), anyString(), eq(exc)); + } + + Error err = new ExecutionError("", new Error()); + doThrow(err).when(onDidFailLoadingTileListener).onDidFailLoadingTile(TEST_STRING); + try { + mapChangeEventManager.onDidFailLoadingTile(TEST_STRING); + Assert.fail("The exception should've been re-thrown."); + } catch (ExecutionError throwable) { + verify(loggerDefinition).e(anyString(), anyString(), eq(err)); + } + } + @Test public void testOnWillStartRenderingFrameListener() { mapChangeEventManager.addOnWillStartRenderingFrameListener(onWillStartRenderingFrameListener); diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index 7db3d9f31..8bdec2c2d 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -40,7 +40,7 @@ ext { ktlint : '0.34.0', commonsIO : '2.6', mapboxSdkVersions: '1.1.0', - mapboxSdkCore : '5.2.2', + mapboxSdkCore : '5.3.0', mapboxSdkRegistryPlugin: '0.3.0', okio : '2.4.3' ]