diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2016-12-17 20:25:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-17 20:25:16 +0100 |
commit | 83f1350747be9a60eb0275bd1a8dcb8e5f027abe (patch) | |
tree | 3027bd324d9785ec06356383363a0bdea282fb71 /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java | |
parent | 20b958301eb208fe9ed0ae8edfb14b6f3741d8f2 (diff) | |
download | qtlocation-mapboxgl-83f1350747be9a60eb0275bd1a8dcb8e5f027abe.tar.gz |
MapboxAccountManager refactor, removal deprecated accesstoken (#7462)
* [android] - move object ownership, move depenencies and responsibility.
* [android] - MapboxAccountManager refactor, removal deprecated accesstoken.
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java | 624 |
1 files changed, 151 insertions, 473 deletions
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 5bbba8682a..5dee9bf43d 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 @@ -8,7 +8,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; -import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; @@ -38,19 +37,13 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.FrameLayout; import android.widget.ImageView; -import android.widget.ZoomButtonsController; -import com.mapbox.mapboxsdk.MapboxAccountManager; +import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.R; -import com.mapbox.mapboxsdk.annotations.InfoWindow; import com.mapbox.mapboxsdk.annotations.MarkerViewManager; import com.mapbox.mapboxsdk.camera.CameraPosition; -import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; -import com.mapbox.mapboxsdk.constants.MyBearingTracking; -import com.mapbox.mapboxsdk.constants.MyLocationTracking; +import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.constants.Style; -import com.mapbox.mapboxsdk.location.LocationListener; -import com.mapbox.mapboxsdk.location.LocationServices; import com.mapbox.mapboxsdk.maps.widgets.CompassView; import com.mapbox.mapboxsdk.maps.widgets.MyLocationView; import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings; @@ -60,51 +53,9 @@ import com.mapbox.mapboxsdk.telemetry.MapboxEventManager; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; -import java.util.Hashtable; import java.util.Iterator; import java.util.List; -import timber.log.Timber; - -import static com.mapbox.mapboxsdk.constants.MapboxConstants.ANIMATION_DURATION; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ATTRIBUTION_ENABLED; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ATTRIBUTION_GRAVITY; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ATTRIBUTION_MARGIN_BOTTOM; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ATTRIBUTION_MARGIN_LEFT; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ATTRIBUTION_MARGIN_RIGHT; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ATTRIBUTION_MARGIN_TOP; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_CAMERA_POSITION; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_COMPASS_ENABLED; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_COMPASS_GRAVITY; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_COMPASS_MARGIN_LEFT; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_COMPASS_MARGIN_RIGHT; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_COMPASS_MARGIN_TOP; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_DEBUG_ACTIVE; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_HAS_SAVED_STATE; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_LOGO_ENABLED; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_LOGO_GRAVITY; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_LOGO_MARGIN_BOTTOM; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_LOGO_MARGIN_LEFT; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_LOGO_MARGIN_RIGHT; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_LOGO_MARGIN_TOP; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_MY_BEARING_TRACKING_DISMISS; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_MY_BEARING_TRACKING_MODE; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_MY_LOCATION_ENABLED; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_MY_LOCATION_TRACKING_DISMISS; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_MY_LOCATION_TRACKING_MODE; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ROTATE_ENABLED; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ROTATE_ENABLED_CHANGE; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_SCROLL_ENABLED; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_SCROLL_ENABLED_CHANGE; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_STYLE_URL; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_TILT_ENABLED; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_TILT_ENABLED_CHANGE; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ZOOM_CONTROLS_ENABLED; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ZOOM_ENABLED; -import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ZOOM_ENABLED_CHANGE; - /** * <p> * A {@code MapView} provides an embeddable map interface. @@ -121,112 +72,89 @@ import static com.mapbox.mapboxsdk.constants.MapboxConstants.STATE_ZOOM_ENABLED_ */ public class MapView extends FrameLayout { - private MapboxMap mapboxMap; - - private boolean initialLoad; - private boolean destroyed; - private NativeMapView nativeMapView; + private boolean destroyed; private boolean hasSurface = false; - private CompassView compassView; - private MyLocationView myLocationView; - private LocationListener myLocationListener; + private MapboxMap mapboxMap; + private MapCallback mapCallback; + private boolean onStartCalled; + private boolean onStopCalled; private MapGestureDetector mapGestureDetector; private MapKeyListener mapKeyListener; + private MapZoomButtonController mapZoomButtonController; private ConnectivityReceiver connectivityReceiver; - - private List<OnMapReadyCallback> onMapReadyCallbackList = new ArrayList<>(); private SnapshotRequest snapshotRequest; - private ZoomButtonsController zoomButtonsController; - - private boolean onStartCalled; - private boolean onStopCalled; @UiThread public MapView(@NonNull Context context) { super(context); - initialize(context, MapboxMapOptions.createFromAttributes(context, null)); + initialise(context, MapboxMapOptions.createFromAttributes(context, null)); } @UiThread public MapView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); - initialize(context, MapboxMapOptions.createFromAttributes(context, attrs)); + initialise(context, MapboxMapOptions.createFromAttributes(context, attrs)); } @UiThread public MapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); - initialize(context, MapboxMapOptions.createFromAttributes(context, attrs)); + initialise(context, MapboxMapOptions.createFromAttributes(context, attrs)); } @UiThread public MapView(@NonNull Context context, @NonNull MapboxMapOptions options) { super(context); - initialize(context, options); + initialise(context, options); } - private void initialize(@NonNull final Context context, @NonNull final MapboxMapOptions options) { + private void initialise(@NonNull final Context context, @NonNull final MapboxMapOptions options) { if (isInEditMode()) { - // if we are in an editor mode we show an image of a map + // in IDE, show preview map LayoutInflater.from(context).inflate(R.layout.mapbox_mapview_preview, this); return; } - // TODO distill into singular purpose methods/classes - initialLoad = true; + // inflate view View view = LayoutInflater.from(context).inflate(R.layout.mapbox_mapview_internal, this); - setWillNotDraw(false); - - if (options.getTextureMode()) { - TextureView textureView = new TextureView(context); - textureView.setSurfaceTextureListener(new SurfaceTextureListener()); - addView(textureView, 0); - } else { - SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView); - surfaceView.getHolder().addCallback(new SurfaceCallback()); - surfaceView.setVisibility(View.VISIBLE); - } + CompassView compassView = (CompassView) view.findViewById(R.id.compassView); + MyLocationView myLocationView = (MyLocationView) view.findViewById(R.id.userLocationView); + ImageView attrView = (ImageView) view.findViewById(R.id.attributionView); + initalizeDrawingSurface(context, options); + // create native Map object nativeMapView = new NativeMapView(this); - // inflate overlain Views - compassView = (CompassView) view.findViewById(R.id.compassView); - myLocationView = (MyLocationView) view.findViewById(R.id.userLocationView); - ImageView logoView = (ImageView) view.findViewById(R.id.logoView); - ImageView attributionsView = (ImageView) view.findViewById(R.id.attributionView); - attributionsView.setOnClickListener(new AttributionOnClickListener(this)); - ViewGroup markerViewContainer = (ViewGroup) findViewById(R.id.markerViewContainer); + // callback for focal point invalidation + FocalPointInvalidator focalPoint = new FocalPointInvalidator(); - // interface for focal point invalidation - FocalPointInvalidator focalPointInvalidator = new FocalPointInvalidator(); - - // interface for registering touch listeners + // callback for registering touch listeners RegisterTouchListener registerTouchListener = new RegisterTouchListener(); // setup components for MapboxMap creation - Projection projection = new Projection(nativeMapView); - UiSettings uiSettings = new UiSettings(projection, focalPointInvalidator, compassView, attributionsView, logoView); - TrackingSettings trackingSettings = new TrackingSettings(myLocationView, uiSettings, focalPointInvalidator); - MyLocationViewSettings myLocationViewSettings = new MyLocationViewSettings(myLocationView, projection, - focalPointInvalidator); - MarkerViewManager markerViewManager = new MarkerViewManager(markerViewContainer); - AnnotationManager annotationManager = new AnnotationManager(nativeMapView, this, markerViewManager); - Transform transform = new Transform(nativeMapView, annotationManager.getMarkerViewManager(), trackingSettings); - mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, trackingSettings, myLocationViewSettings, - projection, registerTouchListener, annotationManager); - - // active user input - mapGestureDetector = new MapGestureDetector(context, mapboxMap.getTransform(), projection, uiSettings, - trackingSettings, annotationManager); - mapKeyListener = new MapKeyListener(mapboxMap.getTransform(), trackingSettings, uiSettings); - - // attach widgets to MapboxMap + Projection proj = new Projection(nativeMapView); + UiSettings uiSettings = new UiSettings(proj, focalPoint, compassView, attrView, view.findViewById(R.id.logoView)); + TrackingSettings trackingSettings = new TrackingSettings(myLocationView, uiSettings, focalPoint); + MyLocationViewSettings myLocationViewSettings = new MyLocationViewSettings(myLocationView, proj, focalPoint); + MarkerViewManager markerViewManager = new MarkerViewManager((ViewGroup) findViewById(R.id.markerViewContainer)); + AnnotationManager annotations = new AnnotationManager(nativeMapView, this, markerViewManager); + Transform transform = new Transform(nativeMapView, annotations.getMarkerViewManager(), trackingSettings); + mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, trackingSettings, myLocationViewSettings, proj, + registerTouchListener, annotations); + + // user input + mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings, trackingSettings, annotations); + mapKeyListener = new MapKeyListener(transform, trackingSettings, uiSettings); + mapZoomButtonController = new MapZoomButtonController(this, uiSettings, transform); + + // inject widgets with MapboxMap compassView.setMapboxMap(mapboxMap); myLocationView.setMapboxMap(mapboxMap); + attrView.setOnClickListener(new AttributionOnClickListener(context, transform)); // Ensure this view is interactable setClickable(true); @@ -234,21 +162,26 @@ public class MapView extends FrameLayout { setFocusable(true); setFocusableInTouchMode(true); requestDisallowInterceptTouchEvent(true); - requestFocus(); - // Connectivity - onConnectivityChanged(isConnected()); + // allow onDraw invocation + setWillNotDraw(false); - // configure the zoom button controller - zoomButtonsController = new ZoomButtonsController(MapView.this); - zoomButtonsController.setZoomSpeed(ANIMATION_DURATION); - zoomButtonsController.setOnZoomListener(new OnZoomListener(mapboxMap)); + // notify Map object about current connectivity state + nativeMapView.setReachability(isConnected()); + // initialise MapboxMap mapboxMap.initialise(context, options); + } - // Shows the zoom controls - if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH)) { - uiSettings.setZoomControlsEnabled(true); + private void initalizeDrawingSurface(Context context, MapboxMapOptions options) { + if (options.getTextureMode()) { + TextureView textureView = new TextureView(context); + textureView.setSurfaceTextureListener(new SurfaceTextureListener()); + addView(textureView, 0); + } else { + SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView); + surfaceView.getHolder().addCallback(new SurfaceCallback()); + surfaceView.setVisibility(View.VISIBLE); } } @@ -261,155 +194,28 @@ public class MapView extends FrameLayout { * You must call this method from the parent's {@link android.app.Activity#onCreate(Bundle)} or * {@link android.app.Fragment#onCreate(Bundle)}. * </p> - * You must set a valid access token with {@link MapView#setAccessToken(String)} before you this method + * You must set a valid access token with {@link Mapbox#getInstance(Context, String)}) before you call this method * or an exception will be thrown. * * @param savedInstanceState Pass in the parent's savedInstanceState. - * @see MapView#setAccessToken(String) + * @see Mapbox#getInstance(Context, String) */ @UiThread public void onCreate(@Nullable Bundle savedInstanceState) { - // TODO distill into singular purpose methods/classes - String accessToken = mapboxMap.getAccessToken(); - if (TextUtils.isEmpty(accessToken)) { - accessToken = MapboxAccountManager.getInstance().getAccessToken(); - nativeMapView.setAccessToken(accessToken); - } else { - // user provided access token through xml attributes, need to start MapboxAccountManager - MapboxAccountManager.start(getContext(), accessToken); - nativeMapView.setAccessToken(accessToken); - } - - // Force a check for an access token - MapboxAccountManager.validateAccessToken(accessToken); - nativeMapView.setAccessToken(accessToken); + Mapbox.validateAccessToken(); + nativeMapView.setAccessToken(Mapbox.getAccessToken()); - if (savedInstanceState != null && savedInstanceState.getBoolean(STATE_HAS_SAVED_STATE)) { - - // Get previous camera position - CameraPosition cameraPosition = savedInstanceState.getParcelable(STATE_CAMERA_POSITION); - if (cameraPosition != null) { - mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder(cameraPosition).build())); - } - - UiSettings uiSettings = mapboxMap.getUiSettings(); - uiSettings.setZoomGesturesEnabled(savedInstanceState.getBoolean(STATE_ZOOM_ENABLED)); - uiSettings.setZoomGestureChangeAllowed(savedInstanceState.getBoolean(STATE_ZOOM_ENABLED_CHANGE)); - uiSettings.setScrollGesturesEnabled(savedInstanceState.getBoolean(STATE_SCROLL_ENABLED)); - uiSettings.setScrollGestureChangeAllowed(savedInstanceState.getBoolean(STATE_SCROLL_ENABLED_CHANGE)); - uiSettings.setRotateGesturesEnabled(savedInstanceState.getBoolean(STATE_ROTATE_ENABLED)); - uiSettings.setRotateGestureChangeAllowed(savedInstanceState.getBoolean(STATE_ROTATE_ENABLED_CHANGE)); - uiSettings.setTiltGesturesEnabled(savedInstanceState.getBoolean(STATE_TILT_ENABLED)); - uiSettings.setTiltGestureChangeAllowed(savedInstanceState.getBoolean(STATE_TILT_ENABLED_CHANGE)); - uiSettings.setZoomControlsEnabled(savedInstanceState.getBoolean(STATE_ZOOM_CONTROLS_ENABLED)); - - // Compass - uiSettings.setCompassEnabled(savedInstanceState.getBoolean(STATE_COMPASS_ENABLED)); - uiSettings.setCompassGravity(savedInstanceState.getInt(STATE_COMPASS_GRAVITY)); - uiSettings.setCompassMargins(savedInstanceState.getInt(STATE_COMPASS_MARGIN_LEFT), - savedInstanceState.getInt(STATE_COMPASS_MARGIN_TOP), - savedInstanceState.getInt(STATE_COMPASS_MARGIN_RIGHT), - savedInstanceState.getInt(STATE_COMPASS_MARGIN_BOTTOM)); - uiSettings.setCompassFadeFacingNorth(savedInstanceState.getBoolean(STATE_COMPASS_FADE_WHEN_FACING_NORTH)); - - // Logo - uiSettings.setLogoEnabled(savedInstanceState.getBoolean(STATE_LOGO_ENABLED)); - uiSettings.setLogoGravity(savedInstanceState.getInt(STATE_LOGO_GRAVITY)); - uiSettings.setLogoMargins(savedInstanceState.getInt(STATE_LOGO_MARGIN_LEFT), - savedInstanceState.getInt(STATE_LOGO_MARGIN_TOP), - savedInstanceState.getInt(STATE_LOGO_MARGIN_RIGHT), - savedInstanceState.getInt(STATE_LOGO_MARGIN_BOTTOM)); - - // Attribution - uiSettings.setAttributionEnabled(savedInstanceState.getBoolean(STATE_ATTRIBUTION_ENABLED)); - uiSettings.setAttributionGravity(savedInstanceState.getInt(STATE_ATTRIBUTION_GRAVITY)); - uiSettings.setAttributionMargins(savedInstanceState.getInt(STATE_ATTRIBUTION_MARGIN_LEFT), - savedInstanceState.getInt(STATE_ATTRIBUTION_MARGIN_TOP), - savedInstanceState.getInt(STATE_ATTRIBUTION_MARGIN_RIGHT), - savedInstanceState.getInt(STATE_ATTRIBUTION_MARGIN_BOTTOM)); - - mapboxMap.setDebugActive(savedInstanceState.getBoolean(STATE_DEBUG_ACTIVE)); - - String styleUrl = savedInstanceState.getString(STATE_STYLE_URL); - if (!TextUtils.isEmpty(styleUrl)) { - nativeMapView.setStyleUrl(savedInstanceState.getString(STATE_STYLE_URL)); - } - - // User location - try { - mapboxMap.setMyLocationEnabled(savedInstanceState.getBoolean(STATE_MY_LOCATION_ENABLED)); - } catch (SecurityException ignore) { - // User did not accept location permissions - } - - TrackingSettings trackingSettings = mapboxMap.getTrackingSettings(); - //noinspection ResourceType - trackingSettings.setMyLocationTrackingMode( - savedInstanceState.getInt(STATE_MY_LOCATION_TRACKING_MODE, MyLocationTracking.TRACKING_NONE)); - //noinspection ResourceType - trackingSettings.setMyBearingTrackingMode( - savedInstanceState.getInt(STATE_MY_BEARING_TRACKING_MODE, MyBearingTracking.NONE)); - trackingSettings.setDismissLocationTrackingOnGesture( - savedInstanceState.getBoolean(STATE_MY_LOCATION_TRACKING_DISMISS, true)); - trackingSettings.setDismissBearingTrackingOnGesture( - savedInstanceState.getBoolean(STATE_MY_BEARING_TRACKING_DISMISS, true)); - } else if (savedInstanceState == null) { - // Start Telemetry (authorization determined in initial MapboxEventManager constructor) - Timber.i("MapView start Telemetry..."); - MapboxEventManager eventManager = MapboxEventManager.getMapboxEventManager(); - eventManager.initialize(getContext(), getAccessToken()); + if (savedInstanceState == null) { + MapboxEvent.trackMapLoadEvent(); + } else if (savedInstanceState.getBoolean(MapboxConstants.STATE_HAS_SAVED_STATE)) { + mapboxMap.onRestoreInstanceState(savedInstanceState); } // Initialize EGL nativeMapView.initializeDisplay(); nativeMapView.initializeContext(); - // Add annotation deselection listener - addOnMapChangedListener(new OnMapChangedListener() { - @Override - public void onMapChanged(@MapChange int change) { - - // TODO extract logic into separate OnMapReady and Update Component - if (change == DID_FINISH_LOADING_STYLE && initialLoad) { - initialLoad = false; - mapboxMap.getAnnotationManager().reloadMarkers(); - mapboxMap.getAnnotationManager().adjustTopOffsetPixels(mapboxMap); - - // Notify listeners the map is ready - if (onMapReadyCallbackList.size() > 0) { - Iterator<OnMapReadyCallback> iterator = onMapReadyCallbackList.iterator(); - while (iterator.hasNext()) { - OnMapReadyCallback callback = iterator.next(); - callback.onMapReady(mapboxMap); - iterator.remove(); - } - } - - // invalidate camera to update overlain views with correct tilt value - mapboxMap.invalidateCameraPosition(); - - } else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE || change == DID_FINISH_LOADING_MAP) { - mapboxMap.getMarkerViewManager().scheduleViewMarkerInvalidation(); - - compassView.update(mapboxMap.getTransform().getBearing()); - myLocationView.update(); - mapboxMap.getMarkerViewManager().update(); - - for (InfoWindow infoWindow : mapboxMap.getInfoWindows()) { - infoWindow.update(); - } - } - - } - }); - - // Fire MapLoad - if (savedInstanceState == null) { - Hashtable<String, Object> evt = new Hashtable<>(); - evt.put(MapboxEvent.ATTRIBUTE_EVENT, MapboxEvent.TYPE_MAP_LOAD); - evt.put(MapboxEvent.ATTRIBUTE_CREATED, MapboxEventManager.generateCreateDate()); - MapboxEventManager.getMapboxEventManager().pushEvent(evt); - } + addOnMapChangedListener(mapCallback = new MapCallback(mapboxMap)); } /** @@ -421,55 +227,8 @@ public class MapView extends FrameLayout { @UiThread public void onSaveInstanceState(@NonNull Bundle outState) { - outState.putBoolean(STATE_HAS_SAVED_STATE, true); - outState.putParcelable(STATE_CAMERA_POSITION, mapboxMap.getCameraPosition()); - outState.putBoolean(STATE_DEBUG_ACTIVE, mapboxMap.isDebugActive()); - outState.putString(STATE_STYLE_URL, nativeMapView.getStyleUrl()); - outState.putBoolean(STATE_MY_LOCATION_ENABLED, mapboxMap.isMyLocationEnabled()); - - // TrackingSettings - TrackingSettings trackingSettings = mapboxMap.getTrackingSettings(); - outState.putInt(STATE_MY_LOCATION_TRACKING_MODE, trackingSettings.getMyLocationTrackingMode()); - outState.putInt(STATE_MY_BEARING_TRACKING_MODE, trackingSettings.getMyBearingTrackingMode()); - outState.putBoolean(STATE_MY_LOCATION_TRACKING_DISMISS, trackingSettings.isDismissLocationTrackingOnGesture()); - outState.putBoolean(STATE_MY_BEARING_TRACKING_DISMISS, trackingSettings.isDismissBearingTrackingOnGesture()); - - // UiSettings - UiSettings uiSettings = mapboxMap.getUiSettings(); - outState.putBoolean(STATE_ZOOM_ENABLED, uiSettings.isZoomGesturesEnabled()); - outState.putBoolean(STATE_ZOOM_ENABLED_CHANGE, uiSettings.isZoomGestureChangeAllowed()); - outState.putBoolean(STATE_SCROLL_ENABLED, uiSettings.isScrollGesturesEnabled()); - outState.putBoolean(STATE_SCROLL_ENABLED_CHANGE, uiSettings.isScrollGestureChangeAllowed()); - outState.putBoolean(STATE_ROTATE_ENABLED, uiSettings.isRotateGesturesEnabled()); - outState.putBoolean(STATE_ROTATE_ENABLED_CHANGE, uiSettings.isRotateGestureChangeAllowed()); - outState.putBoolean(STATE_TILT_ENABLED, uiSettings.isTiltGesturesEnabled()); - outState.putBoolean(STATE_TILT_ENABLED_CHANGE, uiSettings.isTiltGestureChangeAllowed()); - outState.putBoolean(STATE_ZOOM_CONTROLS_ENABLED, uiSettings.isZoomControlsEnabled()); - - // UiSettings - Compass - outState.putBoolean(STATE_COMPASS_ENABLED, uiSettings.isCompassEnabled()); - outState.putInt(STATE_COMPASS_GRAVITY, uiSettings.getCompassGravity()); - outState.putInt(STATE_COMPASS_MARGIN_LEFT, uiSettings.getCompassMarginLeft()); - outState.putInt(STATE_COMPASS_MARGIN_TOP, uiSettings.getCompassMarginTop()); - outState.putInt(STATE_COMPASS_MARGIN_BOTTOM, uiSettings.getCompassMarginBottom()); - outState.putInt(STATE_COMPASS_MARGIN_RIGHT, uiSettings.getCompassMarginRight()); - outState.putBoolean(STATE_COMPASS_FADE_WHEN_FACING_NORTH, uiSettings.isCompassFadeWhenFacingNorth()); - - // UiSettings - Logo - outState.putInt(STATE_LOGO_GRAVITY, uiSettings.getLogoGravity()); - outState.putInt(STATE_LOGO_MARGIN_LEFT, uiSettings.getLogoMarginLeft()); - outState.putInt(STATE_LOGO_MARGIN_TOP, uiSettings.getLogoMarginTop()); - outState.putInt(STATE_LOGO_MARGIN_RIGHT, uiSettings.getLogoMarginRight()); - outState.putInt(STATE_LOGO_MARGIN_BOTTOM, uiSettings.getLogoMarginBottom()); - outState.putBoolean(STATE_LOGO_ENABLED, uiSettings.isLogoEnabled()); - - // UiSettings - Attribution - outState.putInt(STATE_ATTRIBUTION_GRAVITY, uiSettings.getAttributionGravity()); - outState.putInt(STATE_ATTRIBUTION_MARGIN_LEFT, uiSettings.getAttributionMarginLeft()); - outState.putInt(STATE_ATTRIBUTION_MARGIN_TOP, uiSettings.getAttributionMarginTop()); - outState.putInt(STATE_ATTRIBUTION_MARGIN_RIGHT, uiSettings.getAttributionMarginRight()); - outState.putInt(STATE_ATTRIBUTION_MARGIN_BOTTOM, uiSettings.getAttributionMarginBottom()); - outState.putBoolean(STATE_ATTRIBUTION_ENABLED, uiSettings.isAttributionEnabled()); + outState.putBoolean(MapboxConstants.STATE_HAS_SAVED_STATE, true); + mapboxMap.onSaveInstanceState(outState); } /** @@ -478,18 +237,8 @@ public class MapView extends FrameLayout { @UiThread public void onStart() { onStartCalled = true; - - // Register for connectivity changes - connectivityReceiver = new ConnectivityReceiver(); - getContext().registerReceiver(connectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); - - nativeMapView.update(); - myLocationView.onStart(); - - if (TextUtils.isEmpty(nativeMapView.getStyleUrl())) { - // if user hasn't loaded a Style yet, load default for them instead - nativeMapView.setStyleUrl(Style.MAPBOX_STREETS); - } + mapboxMap.onStart(); + registerConnectivityReceiver(); } /** @@ -518,14 +267,8 @@ public class MapView extends FrameLayout { @UiThread public void onStop() { onStopCalled = true; - - // Unregister for connectivity changes - if (connectivityReceiver != null) { - getContext().unregisterReceiver(connectivityReceiver); - connectivityReceiver = null; - } - - myLocationView.onStop(); + mapboxMap.onStop(); + unregisterConnectivityReceiver(); } /** @@ -547,12 +290,22 @@ public class MapView extends FrameLayout { nativeMapView = null; } + private void registerConnectivityReceiver() { + getContext().registerReceiver(connectivityReceiver = new ConnectivityReceiver(), + new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); + } + + private void unregisterConnectivityReceiver() { + if (connectivityReceiver != null) { + getContext().unregisterReceiver(connectivityReceiver); + connectivityReceiver = null; + } + } + @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { - if (mapboxMap.getUiSettings().isZoomControlsEnabled()) { - zoomButtonsController.setVisible(true); - } + mapZoomButtonController.setVisible(true); } return mapGestureDetector.onTouchEvent(event) || super.onTouchEvent(event); } @@ -587,17 +340,11 @@ public class MapView extends FrameLayout { switch (event.getActionMasked()) { case MotionEvent.ACTION_HOVER_ENTER: case MotionEvent.ACTION_HOVER_MOVE: - // Show the zoom controls - if (mapboxMap.getUiSettings().isZoomControlsEnabled()) { - zoomButtonsController.setVisible(true); - } + mapZoomButtonController.setVisible(true); return true; case MotionEvent.ACTION_HOVER_EXIT: - // Hide the zoom controls - if (mapboxMap.getUiSettings().isZoomControlsEnabled()) { - zoomButtonsController.setVisible(false); - } + mapZoomButtonController.setVisible(false); return true; default: @@ -618,15 +365,15 @@ public class MapView extends FrameLayout { // Called via JNI from NativeMapView // Forward to any listener protected void onFpsChanged(final double fps) { - post(new Runnable() { - @Override - public void run() { - MapboxMap.OnFpsChangedListener listener = mapboxMap.getOnFpsChangedListener(); - if (listener != null) { + final MapboxMap.OnFpsChangedListener listener = mapboxMap.getOnFpsChangedListener(); + if (listener != null) { + post(new Runnable() { + @Override + public void run() { listener.onFpsChanged(fps); } - } - }); + }); + } } /** @@ -664,64 +411,13 @@ public class MapView extends FrameLayout { // stopgap for https://github.com/mapbox/mapbox-gl-native/issues/6242 if (TextUtils.isEmpty(nativeMapView.getAccessToken())) { - setAccessToken(MapboxAccountManager.getInstance().getAccessToken()); + Mapbox.validateAccessToken(); + nativeMapView.setAccessToken(Mapbox.getAccessToken()); } nativeMapView.setStyleUrl(url); } - /** - * <p> - * DEPRECATED @see MapboxAccountManager#start(String) - * </p> - * <p> - * Sets the current Mapbox access token used to load map styles and tiles. - * </p> - * <p> - * You must set a valid access token before you call {@link MapView#onCreate(Bundle)} - * or an exception will be thrown. - * </p> - * - * @param accessToken Your public Mapbox access token. - * @see MapView#onCreate(Bundle) - * @deprecated As of release 4.1.0, replaced by - * {@link com.mapbox.mapboxsdk.MapboxAccountManager#start(Context, String)} - */ - @Deprecated - @UiThread - public void setAccessToken(@NonNull String accessToken) { - if (destroyed) { - return; - } - // validateAccessToken does the null check - if (!TextUtils.isEmpty(accessToken)) { - accessToken = accessToken.trim(); - } - MapboxAccountManager.validateAccessToken(accessToken); - nativeMapView.setAccessToken(accessToken); - } - - /** - * <p> - * DEPRECATED @see MapboxAccountManager#getAccessToken() - * </p> - * <p> - * Returns the current Mapbox access token used to load map styles and tiles. - * </p> - * - * @return The current Mapbox access token. - * @deprecated As of release 4.1.0, replaced by {@link MapboxAccountManager#getAccessToken()} - */ - @Deprecated - @UiThread - @Nullable - public String getAccessToken() { - if (destroyed) { - return ""; - } - return nativeMapView.getAccessToken(); - } - // // Rendering // @@ -835,14 +531,7 @@ public class MapView extends FrameLayout { if (destroyed) { return; } - // TODO move to transform. java - compassView.update(mapboxMap.getTransform().getBearing()); - myLocationView.update(); - mapboxMap.getMarkerViewManager().update(); - - for (InfoWindow infoWindow : mapboxMap.getInfoWindows()) { - infoWindow.update(); - } + mapboxMap.onUpdate(); } } @@ -855,18 +544,7 @@ public class MapView extends FrameLayout { @CallSuper protected void onDetachedFromWindow() { super.onDetachedFromWindow(); - // Required by ZoomButtonController (from Android SDK documentation) - if (mapboxMap.getUiSettings().isZoomControlsEnabled()) { - zoomButtonsController.setVisible(false); - } - - // make sure we don't leak location listener - if (myLocationListener != null) { - // cleanup to prevent memory leak - LocationServices services = LocationServices.getLocationServices(getContext()); - services.removeLocationListener(myLocationListener); - myLocationListener = null; - } + mapZoomButtonController.setVisible(false); } // Called when view is hidden and shown @@ -875,10 +553,7 @@ public class MapView extends FrameLayout { if (isInEditMode()) { return; } - - if (mapboxMap != null && mapboxMap.getUiSettings().isZoomControlsEnabled()) { - zoomButtonsController.setVisible(visibility == View.VISIBLE); - } + mapZoomButtonController.setVisible(visibility == View.VISIBLE); } // @@ -892,26 +567,19 @@ public class MapView extends FrameLayout { @Override public void onReceive(Context context, Intent intent) { if (!destroyed && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { - boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); - onConnectivityChanged(!noConnectivity); + nativeMapView.setReachability(!intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)); } } } // Called when MapView is being created private boolean isConnected() { - Context appContext = getContext().getApplicationContext(); ConnectivityManager connectivityManager = (ConnectivityManager) - appContext.getSystemService(Context.CONNECTIVITY_SERVICE); + getContext().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); return (activeNetwork != null) && activeNetwork.isConnectedOrConnecting(); } - // Called when our Internet connectivity has changed - private void onConnectivityChanged(boolean isConnected) { - nativeMapView.setReachability(isConnected); - } - // // Map events // @@ -958,11 +626,11 @@ public class MapView extends FrameLayout { */ @UiThread public void getMapAsync(final OnMapReadyCallback callback) { - if (!initialLoad && callback != null) { + if (!mapCallback.isInitialLoad() && callback != null) { callback.onMapReady(mapboxMap); } else { if (callback != null) { - onMapReadyCallbackList.add(callback); + mapCallback.addOnMapReadyCallback(callback); } } } @@ -1026,20 +694,21 @@ public class MapView extends FrameLayout { private static final int ATTRIBUTION_INDEX_IMPROVE_THIS_MAP = 2; private static final int ATTRIBUTION_INDEX_TELEMETRY_SETTINGS = 3; - private MapView mapView; + private Context context; + private Transform transform; - AttributionOnClickListener(MapView mapView) { - super(); - this.mapView = mapView; + public AttributionOnClickListener(Context context, Transform transform) { + this.context = context; + this.transform = transform; } // Called when someone presses the attribution icon @Override public void onClick(View view) { - AlertDialog.Builder builder = new AlertDialog.Builder(mapView.getContext(), R.style.mapbox_AlertDialogStyle); + AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.mapbox_AlertDialogStyle); builder.setTitle(R.string.mapbox_attributionsDialogTitle); - String[] items = mapView.getContext().getResources().getStringArray(R.array.mapbox_attribution_names); - builder.setAdapter(new ArrayAdapter<>(mapView.getContext(), R.layout.mapbox_attribution_list_item, items), this); + String[] items = context.getResources().getStringArray(R.array.mapbox_attribution_names); + builder.setAdapter(new ArrayAdapter<>(context, R.layout.mapbox_attribution_list_item, items), this); builder.show(); } @@ -1081,7 +750,7 @@ public class MapView extends FrameLayout { } String url = context.getResources().getStringArray(R.array.mapbox_attribution_links)[which]; if (which == ATTRIBUTION_INDEX_IMPROVE_THIS_MAP) { - CameraPosition cameraPosition = mapView.getMapboxMap().getCameraPosition(); + CameraPosition cameraPosition = transform.getCameraPosition(); if (cameraPosition != null) { url = String.format(url, cameraPosition.target.getLongitude(), cameraPosition.target.getLatitude(), (int) cameraPosition.zoom); @@ -1277,7 +946,6 @@ public class MapView extends FrameLayout { */ public static final int DID_FINISH_RENDERING_MAP_FULLY_RENDERED = 13; - /** * This {@link MapChange} is triggered when a style has finished loading. * <p> @@ -1289,7 +957,6 @@ public class MapView extends FrameLayout { */ public static final int DID_FINISH_LOADING_STYLE = 14; - /** * This {@link MapChange} is triggered when a source attribution changes. * <p> @@ -1332,34 +999,6 @@ public class MapView extends FrameLayout { void onMapChanged(@MapChange int change); } - // This class handles input events from the zoom control buttons - // Zoom controls allow single touch only devices to zoom in and out - private class OnZoomListener implements ZoomButtonsController.OnZoomListener { - - private UiSettings uiSettings; - private Transform transform; - - OnZoomListener(MapboxMap mapboxMap) { - this.uiSettings = mapboxMap.getUiSettings(); - this.transform = mapboxMap.getTransform(); - } - - // Not used - @Override - public void onVisibilityChanged(boolean visible) { - // Ignore - } - - // Called when user pushes a zoom button - @Override - public void onZoom(boolean zoomIn) { - if (!uiSettings.isZoomGesturesEnabled()) { - return; - } - transform.zoom(zoomIn); - } - } - private class FocalPointInvalidator implements FocalPointChangeListener { @Override @@ -1391,7 +1030,46 @@ public class MapView extends FrameLayout { } } - NativeMapView getNativeMapView() { - return nativeMapView; + private static class MapCallback implements OnMapChangedListener { + + private final MapboxMap mapboxMap; + private final List<OnMapReadyCallback> onMapReadyCallbackList = new ArrayList<>(); + private boolean initialLoad = true; + + MapCallback(MapboxMap mapboxMap) { + this.mapboxMap = mapboxMap; + } + + @Override + public void onMapChanged(@MapChange int change) { + if (change == DID_FINISH_LOADING_STYLE && initialLoad) { + initialLoad = false; + mapboxMap.onPreMapReady(); + onMapReady(); + mapboxMap.onPostMapReady(); + } else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE || change == DID_FINISH_LOADING_MAP) { + mapboxMap.onUpdate(); + } + } + + private void onMapReady() { + if (onMapReadyCallbackList.size() > 0) { + // Notify listeners, clear when done + Iterator<OnMapReadyCallback> iterator = onMapReadyCallbackList.iterator(); + while (iterator.hasNext()) { + OnMapReadyCallback callback = iterator.next(); + callback.onMapReady(mapboxMap); + iterator.remove(); + } + } + } + + public boolean isInitialLoad() { + return initialLoad; + } + + void addOnMapReadyCallback(OnMapReadyCallback callback) { + onMapReadyCallbackList.add(callback); + } } } |