Skip to content

Commit

Permalink
Misc Android Crash Fixes (#963)
Browse files Browse the repository at this point in the history
* - Add null checks when queueing features and making region payload to avoid crashes.
- Modified disposeNativeMapView threading to fix a crash.

* Adding missing closing )
  • Loading branch information
ClayC-WeatherBug authored Aug 21, 2020
1 parent cb939f1 commit 9ab5259
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public class RCTMGLMapView extends MapView implements OnMapReadyCallback, Mapbox
private MapboxMap mMap;

private LocalizationPlugin mLocalizationPlugin;

private String mStyleURL;

private Integer mPreferredFramesPerSecond;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1188,27 +1188,34 @@ 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);
properties.putBoolean("animated",
(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);
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -319,21 +320,24 @@ public void dispose() {
private void diposeNativeMapView() {
final RCTMGLMapView mapView = mViewManager.getByReactTag(getReactTag());

RunnableFuture<Void> 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);
}
}
});
}
}
}
}

0 comments on commit 9ab5259

Please sign in to comment.