From 39423cc653c60d5948d6eda0161d05efd633dad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Tue, 6 Nov 2018 12:57:14 +0100 Subject: [android] log Java exception and fail silently instead of throwing a native exception in the map state callbacks --- .../mapbox/mapboxsdk/maps/MapChangeReceiver.java | 137 +++++++++++++++------ .../java/com/mapbox/mapboxsdk/maps/MapView.java | 6 + 2 files changed, 107 insertions(+), 36 deletions(-) (limited to 'platform/android/MapboxGLAndroidSDK/src/main') diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapChangeReceiver.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapChangeReceiver.java index 379f67aa7a..3eaa381239 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapChangeReceiver.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapChangeReceiver.java @@ -1,10 +1,15 @@ package com.mapbox.mapboxsdk.maps; +import com.mapbox.mapboxsdk.MapStrictMode; +import com.mapbox.mapboxsdk.log.Logger; + import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; class MapChangeReceiver implements NativeMapView.StateCallback { + private static final String TAG = "Mbgl-MapChangeReceiver"; + private final List onCameraWillChangeListenerList = new CopyOnWriteArrayList<>(); private final List onCameraIsChangingListenerList = new CopyOnWriteArrayList<>(); private final List onCameraDidChangeListenerList = new CopyOnWriteArrayList<>(); @@ -28,109 +33,169 @@ class MapChangeReceiver implements NativeMapView.StateCallback { @Override public void onCameraWillChange(boolean animated) { - if (!onCameraWillChangeListenerList.isEmpty()) { - for (MapView.OnCameraWillChangeListener onCameraWillChangeListener : onCameraWillChangeListenerList) { - onCameraWillChangeListener.onCameraWillChange(animated); + try { + if (!onCameraWillChangeListenerList.isEmpty()) { + for (MapView.OnCameraWillChangeListener onCameraWillChangeListener : onCameraWillChangeListenerList) { + onCameraWillChangeListener.onCameraWillChange(animated); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onCameraWillChange", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onCameraIsChanging() { - if (!onCameraIsChangingListenerList.isEmpty()) { - for (MapView.OnCameraIsChangingListener onCameraIsChangingListener : onCameraIsChangingListenerList) { - onCameraIsChangingListener.onCameraIsChanging(); + try { + if (!onCameraIsChangingListenerList.isEmpty()) { + for (MapView.OnCameraIsChangingListener onCameraIsChangingListener : onCameraIsChangingListenerList) { + onCameraIsChangingListener.onCameraIsChanging(); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onCameraIsChanging", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onCameraDidChange(boolean animated) { - if (!onCameraDidChangeListenerList.isEmpty()) { - for (MapView.OnCameraDidChangeListener onCameraDidChangeListener : onCameraDidChangeListenerList) { - onCameraDidChangeListener.onCameraDidChange(animated); + try { + if (!onCameraDidChangeListenerList.isEmpty()) { + for (MapView.OnCameraDidChangeListener onCameraDidChangeListener : onCameraDidChangeListenerList) { + onCameraDidChangeListener.onCameraDidChange(animated); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onCameraDidChange", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onWillStartLoadingMap() { - if (!onWillStartLoadingMapListenerList.isEmpty()) { - for (MapView.OnWillStartLoadingMapListener onWillStartLoadingMapListener : onWillStartLoadingMapListenerList) { - onWillStartLoadingMapListener.onWillStartLoadingMap(); + try { + if (!onWillStartLoadingMapListenerList.isEmpty()) { + for (MapView.OnWillStartLoadingMapListener onWillStartLoadingMapListener : onWillStartLoadingMapListenerList) { + onWillStartLoadingMapListener.onWillStartLoadingMap(); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onWillStartLoadingMap", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onDidFinishLoadingMap() { - if (!onDidFinishLoadingMapListenerList.isEmpty()) { - for (MapView.OnDidFinishLoadingMapListener onDidFinishLoadingMapListener : onDidFinishLoadingMapListenerList) { - onDidFinishLoadingMapListener.onDidFinishLoadingMap(); + try { + if (!onDidFinishLoadingMapListenerList.isEmpty()) { + for (MapView.OnDidFinishLoadingMapListener onDidFinishLoadingMapListener : onDidFinishLoadingMapListenerList) { + onDidFinishLoadingMapListener.onDidFinishLoadingMap(); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onDidFinishLoadingMap", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onDidFailLoadingMap(String error) { - if (!onDidFailLoadingMapListenerList.isEmpty()) { - for (MapView.OnDidFailLoadingMapListener onDidFailLoadingMapListener : onDidFailLoadingMapListenerList) { - onDidFailLoadingMapListener.onDidFailLoadingMap(error); + try { + if (!onDidFailLoadingMapListenerList.isEmpty()) { + for (MapView.OnDidFailLoadingMapListener onDidFailLoadingMapListener : onDidFailLoadingMapListenerList) { + onDidFailLoadingMapListener.onDidFailLoadingMap(error); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onDidFailLoadingMap", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onWillStartRenderingFrame() { - if (!onWillStartRenderingFrameList.isEmpty()) { - for (MapView.OnWillStartRenderingFrameListener listener : onWillStartRenderingFrameList) { - listener.onWillStartRenderingFrame(); + try { + if (!onWillStartRenderingFrameList.isEmpty()) { + for (MapView.OnWillStartRenderingFrameListener listener : onWillStartRenderingFrameList) { + listener.onWillStartRenderingFrame(); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onWillStartRenderingFrame", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onDidFinishRenderingFrame(boolean fully) { - if (!onDidFinishRenderingFrameList.isEmpty()) { - for (MapView.OnDidFinishRenderingFrameListener listener : onDidFinishRenderingFrameList) { - listener.onDidFinishRenderingFrame(fully); + try { + if (!onDidFinishRenderingFrameList.isEmpty()) { + for (MapView.OnDidFinishRenderingFrameListener listener : onDidFinishRenderingFrameList) { + listener.onDidFinishRenderingFrame(fully); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onDidFinishRenderingFrame", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onWillStartRenderingMap() { - if (!onWillStartRenderingMapListenerList.isEmpty()) { - for (MapView.OnWillStartRenderingMapListener listener : onWillStartRenderingMapListenerList) { - listener.onWillStartRenderingMap(); + try { + if (!onWillStartRenderingMapListenerList.isEmpty()) { + for (MapView.OnWillStartRenderingMapListener listener : onWillStartRenderingMapListenerList) { + listener.onWillStartRenderingMap(); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onWillStartRenderingMap", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onDidFinishRenderingMap(boolean fully) { - if (!onDidFinishRenderingMapListenerList.isEmpty()) { - for (MapView.OnDidFinishRenderingMapListener listener : onDidFinishRenderingMapListenerList) { - listener.onDidFinishRenderingMap(fully); + try { + if (!onDidFinishRenderingMapListenerList.isEmpty()) { + for (MapView.OnDidFinishRenderingMapListener listener : onDidFinishRenderingMapListenerList) { + listener.onDidFinishRenderingMap(fully); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onDidFinishRenderingMap", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onDidFinishLoadingStyle() { - if (!onDidFinishLoadingStyleListenerList.isEmpty()) { - for (MapView.OnDidFinishLoadingStyleListener listener : onDidFinishLoadingStyleListenerList) { - listener.onDidFinishLoadingStyle(); + try { + if (!onDidFinishLoadingStyleListenerList.isEmpty()) { + for (MapView.OnDidFinishLoadingStyleListener listener : onDidFinishLoadingStyleListenerList) { + listener.onDidFinishLoadingStyle(); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onDidFinishLoadingStyle", err); + MapStrictMode.strictModeViolation(err); } } @Override public void onSourceChanged(String sourceId) { - if (!onSourceChangedListenerList.isEmpty()) { - for (MapView.OnSourceChangedListener onSourceChangedListener : onSourceChangedListenerList) { - onSourceChangedListener.onSourceChangedListener(sourceId); + try { + if (!onSourceChangedListenerList.isEmpty()) { + for (MapView.OnSourceChangedListener onSourceChangedListener : onSourceChangedListenerList) { + onSourceChangedListener.onSourceChangedListener(sourceId); + } } + } catch (RuntimeException err) { + Logger.e(TAG, "Exception in onSourceChanged", err); + MapStrictMode.strictModeViolation(err); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 7cb8245301..20bb49f140 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -789,6 +789,12 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { mapChangeReceiver.removeOnSourceChangedListener(listener); } + /** + * Interface definition for a callback to be invoked when the camera will change. + *

+ * {@link MapView#addOnCameraWillChangeListener(OnCameraWillChangeListener)} + *

+ */ public interface OnCameraWillChangeListener { /** -- cgit v1.2.1