Implement Android error handling. Add Android telemetry toggle.

This commit is contained in:
Marius Petcu
2016-06-30 21:31:59 +03:00
parent ed92dd8187
commit 0abd92837c
2 changed files with 34 additions and 15 deletions
@@ -8,7 +8,9 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
@@ -20,6 +22,7 @@ import com.mapbox.mapboxsdk.MapboxAccountManager;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager;
import javax.annotation.Nullable;
@@ -71,6 +74,7 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
HashMap<String, Object> userTrackingMode = new HashMap<String, Object>();
HashMap<String, Object> mapStyles = new HashMap<String, Object>();
HashMap<String, Object> userLocationVerticalAlignment = new HashMap<String, Object>();
// User tracking constants
userTrackingMode.put("none", 0);
@@ -86,28 +90,42 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
mapStyles.put("satellite", Style.SATELLITE);
mapStyles.put("hybrid", Style.SATELLITE_STREETS);
// These need to be here for compatibility, even if they're not supported on Android
userLocationVerticalAlignment.put("center", 0);
userLocationVerticalAlignment.put("top", 1);
userLocationVerticalAlignment.put("bottom", 2);
// Other constants
constants.put("unknownResourceCount", Long.MAX_VALUE);
constants.put("metricsEnabled", MapboxEventManager.getMapboxEventManager().isTelemetryEnabled());
constants.put("userTrackingMode", userTrackingMode);
constants.put("mapStyles", mapStyles);
constants.put("userLocationVerticalAlignment", userLocationVerticalAlignment);
return constants;
}
@ReactMethod
public void setAccessToken(String accessToken) {
if (accessToken == null || accessToken.length() == 0 || accessToken == "your-mapbox.com-access-token") {
Log.e(TAG, "Invalid access token. Register to mapbox.com and request an access token, then pass it to setAccessToken()");
return;
if (accessToken == null || accessToken.length() == 0 || accessToken.equals("your-mapbox.com-access-token")) {
throw new JSApplicationIllegalArgumentException("Invalid access token. Register to mapbox.com and request an access token, then pass it to setAccessToken()");
}
if (initialized) {
if (MapboxAccountManager.getInstance().getAccessToken() != accessToken) {
Log.e(TAG, "Access token cannot be initialized twice with different values");
String oldToken = MapboxAccountManager.getInstance().getAccessToken();
if (!oldToken.equals(accessToken)) {
throw new JSApplicationIllegalArgumentException("Mapbox access token cannot be initialized twice with different values");
}
return;
}
initialized = true;
MapboxAccountManager.start(context, accessToken);
}
@ReactMethod
public void setMetricsEnabled(boolean value) {
MapboxEventManager.getMapboxEventManager().setTelemetryEnabled(value);
}
@ReactMethod
public void spliceAnnotations(int mapRef, boolean removeAll, ReadableArray itemsToRemove, ReadableArray itemsToAdd) {
// TODO
@@ -1,9 +1,9 @@
package com.mapbox.reactnativemapboxgl;
import android.content.Context;
import android.util.Log;
import android.widget.LinearLayout;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.LifecycleEventListener;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
@@ -102,9 +102,10 @@ public class ReactNativeMapboxGLView extends LinearLayout implements OnMapReadyC
// Utils
private void assertNotChangeable(String propName) {
private void assertPropNotChangeable(String propName) {
if (_mapView != null) {
Log.e(getContext().getPackageName(), "Changing MapView." + propName + " after component has been mounted is not currently supported");
throw new JSApplicationIllegalArgumentException("Changing prop MapView." + propName +
" after component has been mounted is not currently supported");
}
}
@@ -131,19 +132,19 @@ public class ReactNativeMapboxGLView extends LinearLayout implements OnMapReadyC
public void setRotateEnabled(boolean value) {
if (_mapOptions.getRotateGesturesEnabled() == value) { return; }
_mapOptions.rotateGesturesEnabled(value);
assertNotChangeable("rotateEnabled");
assertPropNotChangeable("rotateEnabled");
}
public void setScrollEnabled(boolean value) {
if (_mapOptions.getScrollGesturesEnabled() == value) { return; }
_mapOptions.scrollGesturesEnabled(value);
assertNotChangeable("scrollEnabled");
assertPropNotChangeable("scrollEnabled");
}
public void setZoomEnabled(boolean value) {
if (_mapOptions.getZoomGesturesEnabled() == value) { return; }
_mapOptions.zoomGesturesEnabled(value);
assertNotChangeable("zoomEnabled");
assertPropNotChangeable("zoomEnabled");
}
public void setStyleURL(String styleURL) {
@@ -173,19 +174,19 @@ public class ReactNativeMapboxGLView extends LinearLayout implements OnMapReadyC
public void setAttributionButtonIsHidden(boolean value) {
if (_mapOptions.getAttributionEnabled() == !value) { return; }
_mapOptions.attributionEnabled(!value);
assertNotChangeable("attributionButtonIsHidden");
assertPropNotChangeable("attributionButtonIsHidden");
}
public void setLogoIsHidden(boolean value) {
if (_mapOptions.getLogoEnabled() == !value) { return; }
_mapOptions.logoEnabled(!value);
assertNotChangeable("logoIsHidden");
assertPropNotChangeable("logoIsHidden");
}
public void setCompassIsHidden(boolean value) {
if (_mapOptions.getCompassEnabled() == !value) { return; }
_mapOptions.compassEnabled(!value);
assertNotChangeable("compassIsHidden");
assertPropNotChangeable("compassIsHidden");
}
public void setContentInset(int top, int right, int bottom, int left) {