From 9ab5259a9ee650602e34d3ad08375ee6395cc433 Mon Sep 17 00:00:00 2001 From: Clay Christmas <65256865+ClayC-WeatherBug@users.noreply.github.com> Date: Fri, 21 Aug 2020 14:19:06 -0400 Subject: [PATCH] Misc Android Crash Fixes (#963) * - Add null checks when queueing features and making region payload to avoid crashes. - Modified disposeNativeMapView threading to fix a crash. * Adding missing closing ) --- .../components/mapview/RCTMGLMapView.java | 19 +++++++---- .../mapview/RCTMGLMapViewManager.java | 32 +++++++++++-------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.java b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.java index 9fdecea9c..98766110f 100644 --- a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.java +++ b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.java @@ -118,7 +118,7 @@ public class RCTMGLMapView extends MapView implements OnMapReadyCallback, Mapbox private MapboxMap mMap; private LocalizationPlugin mLocalizationPlugin; - + private String mStyleURL; private Integer mPreferredFramesPerSecond; @@ -528,7 +528,7 @@ public void onAnnotationDragFinished(Symbol symbol) { } public void addQueuedFeatures() { - if (mQueuedFeatures.size() > 0) { + if (mQueuedFeatures != null && mQueuedFeatures.size() > 0) { for (int i = 0; i < mQueuedFeatures.size(); i++) { AbstractMapFeature feature = mQueuedFeatures.get(i); feature.addToMap(this); @@ -1188,10 +1188,13 @@ public void onHostDestroy() { private WritableMap makeRegionPayload(Boolean isAnimated) { CameraPosition position = mMap.getCameraPosition(); + if(position == null || position.target == null) { + return new WritableNativeMap(); + } LatLng latLng = new LatLng(position.target.getLatitude(), position.target.getLongitude()); WritableMap properties = new WritableNativeMap(); - + properties.putDouble("zoomLevel", position.zoom); properties.putDouble("heading", position.bearing); properties.putDouble("pitch", position.tilt); @@ -1199,8 +1202,12 @@ private WritableMap makeRegionPayload(Boolean isAnimated) { (null == isAnimated) ? mCameraChangeTracker.isAnimated() : isAnimated.booleanValue()); properties.putBoolean("isUserInteraction", mCameraChangeTracker.isUserInteraction()); - VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion(); - properties.putArray("visibleBounds", GeoJSONUtils.fromLatLngBounds(visibleRegion.latLngBounds)); + try { + VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion(); + properties.putArray("visibleBounds", GeoJSONUtils.fromLatLngBounds(visibleRegion.latLngBounds)); + } catch(Exception ex) { + Logger.e(LOG_TAG, String.format("An error occurred while attempting to make the region: %s", ex.getMessage())); + } return GeoJSONUtils.toPointFeature(latLng, properties); } @@ -1208,7 +1215,7 @@ private WritableMap makeRegionPayload(Boolean isAnimated) { public void sendRegionChangeEvent(boolean isAnimated) { IEvent event = new MapChangeEvent(this, EventTypes.REGION_DID_CHANGE, makeRegionPayload(new Boolean(isAnimated))); - + mManager.handleEvent(event); mCameraChangeTracker.setReason(CameraChangeTracker.EMPTY); } diff --git a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapViewManager.java b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapViewManager.java index 99b08abe0..c6cd92c03 100644 --- a/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapViewManager.java +++ b/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/mapview/RCTMGLMapViewManager.java @@ -12,6 +12,7 @@ import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.annotations.ReactProp; import com.mapbox.mapboxsdk.geometry.LatLngBounds; +import com.mapbox.mapboxsdk.log.Logger; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.rctmgl.components.AbstractEventEmitter; import com.mapbox.rctmgl.events.constants.EventKeys; @@ -319,21 +320,24 @@ public void dispose() { private void diposeNativeMapView() { final RCTMGLMapView mapView = mViewManager.getByReactTag(getReactTag()); - RunnableFuture task = new FutureTask<>(new Runnable() { - @Override - public void run() { - mapView.dispose(); - } - }, null); - - runOnUiThread(task); - - try { - task.get(); // this will block until Runnable completes - } catch (InterruptedException | ExecutionException e) { - // handle exception - Log.e(getClass().getSimpleName() , " diposeNativeMapView() exception destroying map view", e); + if (mapView != null) + { + runOnUiThread(new Runnable() { + @Override + public void run() + { + try + { + mapView.dispose(); + } + catch (Exception ex) + { + Log.e(getClass().getSimpleName() , " disposeNativeMapView() exception destroying map view", ex); + } + } + }); } } } } +