2015-12-12 12:41:50 -08:00
package com.mapbox.reactnativemapboxgl ;
import android.content.Context ;
2016-07-01 02:33:30 +03:00
import android.support.annotation.UiThread ;
2015-12-12 12:41:50 -08:00
import android.util.Log ;
2016-06-30 20:25:01 +03:00
import java.util.ArrayList ;
2015-12-17 11:47:22 -08:00
import java.util.HashMap ;
import java.util.Map ;
2015-12-12 12:41:50 -08:00
2016-06-30 21:31:59 +03:00
import com.facebook.common.logging.FLog ;
2015-12-12 12:41:50 -08:00
import com.facebook.react.bridge.Arguments ;
2016-07-01 02:33:30 +03:00
import com.facebook.react.bridge.Callback ;
2016-06-30 21:31:59 +03:00
import com.facebook.react.bridge.JSApplicationIllegalArgumentException ;
2015-12-12 12:41:50 -08:00
import com.facebook.react.bridge.ReactApplicationContext ;
import com.facebook.react.bridge.ReactContextBaseJavaModule ;
import com.facebook.react.bridge.ReactMethod ;
import com.facebook.react.bridge.ReadableArray ;
import com.facebook.react.bridge.ReadableMap ;
2016-06-30 16:59:10 +03:00
import com.facebook.react.bridge.WritableArray ;
import com.facebook.react.bridge.WritableNativeArray ;
import com.mapbox.mapboxsdk.MapboxAccountManager ;
2015-12-17 11:47:22 -08:00
import com.mapbox.mapboxsdk.constants.MyLocationTracking ;
2016-06-30 16:59:10 +03:00
import com.mapbox.mapboxsdk.constants.MyBearingTracking ;
import com.mapbox.mapboxsdk.constants.Style ;
2016-06-30 21:31:59 +03:00
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager ;
2015-12-12 12:41:50 -08:00
import javax.annotation.Nullable ;
public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule {
private static final String TAG = ReactNativeMapboxGLModule . class . getSimpleName ();
2016-07-01 02:33:30 +03:00
private ReactApplicationContext context ;
2015-12-12 12:41:50 -08:00
private ReactNativeMapboxGLPackage aPackage ;
2016-06-30 16:59:10 +03:00
private static boolean initialized = false ;
2015-12-12 12:41:50 -08:00
2016-06-30 16:59:10 +03:00
public ReactNativeMapboxGLModule ( ReactApplicationContext reactContext , ReactNativeMapboxGLPackage thePackage ) {
2015-12-12 12:41:50 -08:00
super ( reactContext );
this . context = reactContext ;
2016-06-30 16:59:10 +03:00
this . aPackage = thePackage ;
2015-12-12 12:41:50 -08:00
Log . d ( TAG , "Context " + context );
Log . d ( TAG , "reactContext " + reactContext );
}
@Override
public String getName () {
2015-12-14 09:30:47 -08:00
return "MapboxGLManager" ;
2015-12-12 12:41:50 -08:00
}
2016-06-30 20:25:01 +03:00
static private ArrayList < Integer > serializeTracking ( int locationTracking , int bearingTracking ) {
ArrayList < Integer > result = new ArrayList < Integer > ();
result . add ( locationTracking );
result . add ( bearingTracking );
2016-06-30 16:59:10 +03:00
return result ;
}
2016-06-30 20:25:01 +03:00
public static final int [] locationTrackingModes = new int [] {
MyLocationTracking . TRACKING_NONE ,
MyLocationTracking . TRACKING_FOLLOW ,
MyLocationTracking . TRACKING_FOLLOW ,
MyLocationTracking . TRACKING_FOLLOW
};
public static final int [] bearingTrackingModes = new int [] {
MyBearingTracking . NONE ,
MyBearingTracking . NONE ,
MyBearingTracking . GPS ,
MyBearingTracking . COMPASS
};
2015-12-17 11:47:22 -08:00
@Override
public @Nullable Map < String , Object > getConstants () {
HashMap < String , Object > constants = new HashMap < String , Object > ();
HashMap < String , Object > userTrackingMode = new HashMap < String , Object > ();
HashMap < String , Object > mapStyles = new HashMap < String , Object > ();
2016-06-30 21:31:59 +03:00
HashMap < String , Object > userLocationVerticalAlignment = new HashMap < String , Object > ();
2015-12-17 11:47:22 -08:00
2016-06-30 20:25:01 +03:00
// User tracking constants
userTrackingMode . put ( "none" , 0 );
userTrackingMode . put ( "follow" , 1 );
userTrackingMode . put ( "followWithCourse" , 2 );
userTrackingMode . put ( "followWithHeading" , 3 );
2015-12-17 11:47:22 -08:00
// Style constants
mapStyles . put ( "light" , Style . LIGHT );
mapStyles . put ( "dark" , Style . DARK );
mapStyles . put ( "streets" , Style . MAPBOX_STREETS );
mapStyles . put ( "emerald" , Style . EMERALD );
mapStyles . put ( "satellite" , Style . SATELLITE );
mapStyles . put ( "hybrid" , Style . SATELLITE_STREETS );
2016-06-30 21:31:59 +03:00
// 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 ());
2015-12-17 11:47:22 -08:00
constants . put ( "userTrackingMode" , userTrackingMode );
constants . put ( "mapStyles" , mapStyles );
2016-06-30 21:31:59 +03:00
constants . put ( "userLocationVerticalAlignment" , userLocationVerticalAlignment );
2015-12-17 11:47:22 -08:00
return constants ;
}
2016-06-30 16:59:10 +03:00
@ReactMethod
public void setAccessToken ( String accessToken ) {
2016-06-30 21:31:59 +03:00
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()" );
2016-06-30 16:59:10 +03:00
}
if ( initialized ) {
2016-06-30 21:31:59 +03:00
String oldToken = MapboxAccountManager . getInstance (). getAccessToken ();
if ( ! oldToken . equals ( accessToken )) {
throw new JSApplicationIllegalArgumentException ( "Mapbox access token cannot be initialized twice with different values" );
2016-06-30 16:59:10 +03:00
}
}
initialized = true ;
MapboxAccountManager . start ( context , accessToken );
}
2016-06-30 21:31:59 +03:00
@ReactMethod
public void setMetricsEnabled ( boolean value ) {
MapboxEventManager . getMapboxEventManager (). setTelemetryEnabled ( value );
}
2015-12-12 12:41:50 -08:00
}