diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2016-12-09 15:38:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-09 15:38:09 +0100 |
commit | 970a0dab449ac669f75c6a037ec7d9b97cdfe2e1 (patch) | |
tree | 65afea49ba71957354d64dad211acb53caec70e1 | |
parent | d6b8866975d9d0b43119ab5cfadd0dc89e4aa51e (diff) | |
download | qtlocation-mapboxgl-970a0dab449ac669f75c6a037ec7d9b97cdfe2e1.tar.gz |
[android] - cleanup initialisation code for MapboxMap (#7330)
4 files changed, 133 insertions, 101 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 9cf09325b6..abd1f9542d 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -1,7 +1,6 @@ package com.mapbox.mapboxsdk.maps; import android.content.Context; -import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.PointF; import android.graphics.RectF; @@ -12,16 +11,11 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.UiThread; import android.support.v4.util.Pools; - -import timber.log.Timber; - import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; -import android.widget.ZoomButtonsController; import com.mapbox.mapboxsdk.MapboxAccountManager; -import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions; @@ -47,12 +41,13 @@ import com.mapbox.mapboxsdk.style.layers.Layer; import com.mapbox.mapboxsdk.style.layers.NoSuchLayerException; import com.mapbox.mapboxsdk.style.sources.NoSuchSourceException; import com.mapbox.mapboxsdk.style.sources.Source; -import com.mapbox.mapboxsdk.utils.ColorUtils; import com.mapbox.services.commons.geojson.Feature; import java.lang.reflect.ParameterizedType; import java.util.List; +import timber.log.Timber; + /** * The general class to interact with in the Android Mapbox SDK. It exposes the entry point for all * methods related to the MapView. You cannot instantiate {@link MapboxMap} object directly, rather, @@ -90,104 +85,21 @@ public final class MapboxMap { this.onRegisterTouchListener = listener; } - void initialise(Context context, MapboxMapOptions options) { - // TODO migrate code from this method to the classes they impact themselves - setDebugActive(options.getDebugActive()); - - CameraPosition position = options.getCamera(); - if (position != null && !position.equals(CameraPosition.DEFAULT)) { - transform.moveCamera(this, CameraUpdateFactory.newCameraPosition(position), null); - myLocationViewSettings.setTilt(position.tilt); - } + void initialise(@NonNull Context context, @NonNull MapboxMapOptions options) { + transform.initialise(this, options); + uiSettings.initialise(context, options); + myLocationViewSettings.initialise(options); // api base url - String apiBaseUrl = options.getApiBaseUrl(); - if (!TextUtils.isEmpty(apiBaseUrl)) { - nativeMapView.setApiBaseUrl(apiBaseUrl); - } - - // access token - String accessToken = options.getAccessToken(); - if (!TextUtils.isEmpty(accessToken)) { - nativeMapView.setAccessToken(accessToken); - }else{ - nativeMapView.setAccessToken(MapboxAccountManager.getInstance().getAccessToken()); - } - - // style url - String style = options.getStyle(); - if (!TextUtils.isEmpty(style)) { - nativeMapView.setStyleUrl(style); - } + setDebugActive(options.getDebugActive()); + setApiBaseUrl(options); + setAccessToken(options); + setStyleUrl(options); - // MyLocationView - myLocationViewSettings.setForegroundDrawable( - options.getMyLocationForegroundDrawable(), options.getMyLocationForegroundBearingDrawable()); - myLocationViewSettings.setForegroundTintColor(options.getMyLocationForegroundTintColor()); - myLocationViewSettings.setBackgroundDrawable( - options.getMyLocationBackgroundDrawable(), options.getMyLocationBackgroundPadding()); - myLocationViewSettings.setBackgroundTintColor(options.getMyLocationBackgroundTintColor()); - myLocationViewSettings.setAccuracyAlpha(options.getMyLocationAccuracyAlpha()); - myLocationViewSettings.setAccuracyTintColor(options.getMyLocationAccuracyTintColor()); + // todo migrate with other PR setMyLocationEnabled(options.getLocationEnabled()); - - // Enable gestures - uiSettings.setZoomGesturesEnabled(options.getZoomGesturesEnabled()); - uiSettings.setZoomGestureChangeAllowed(options.getZoomGesturesEnabled()); - uiSettings.setScrollGesturesEnabled(options.getScrollGesturesEnabled()); - uiSettings.setScrollGestureChangeAllowed(options.getScrollGesturesEnabled()); - uiSettings.setRotateGesturesEnabled(options.getRotateGesturesEnabled()); - uiSettings.setRotateGestureChangeAllowed(options.getRotateGesturesEnabled()); - uiSettings.setTiltGesturesEnabled(options.getTiltGesturesEnabled()); - uiSettings.setTiltGestureChangeAllowed(options.getTiltGesturesEnabled()); - - // Ui Controls - uiSettings.setZoomControlsEnabled(options.getZoomControlsEnabled()); - - // Zoom setMaxZoom(options.getMaxZoom()); setMinZoom(options.getMinZoom()); - - Resources resources = context.getResources(); - - // Compass - uiSettings.setCompassEnabled(options.getCompassEnabled()); - uiSettings.setCompassGravity(options.getCompassGravity()); - int[] compassMargins = options.getCompassMargins(); - if (compassMargins != null) { - uiSettings.setCompassMargins(compassMargins[0], compassMargins[1], compassMargins[2], compassMargins[3]); - } else { - int tenDp = (int) resources.getDimension(R.dimen.mapbox_ten_dp); - uiSettings.setCompassMargins(tenDp, tenDp, tenDp, tenDp); - } - uiSettings.setCompassFadeFacingNorth(options.getCompassFadeFacingNorth()); - - // Logo - uiSettings.setLogoEnabled(options.getLogoEnabled()); - uiSettings.setLogoGravity(options.getLogoGravity()); - int[] logoMargins = options.getLogoMargins(); - if (logoMargins != null) { - uiSettings.setLogoMargins(logoMargins[0], logoMargins[1], logoMargins[2], logoMargins[3]); - } else { - int sixteenDp = (int) resources.getDimension(R.dimen.mapbox_sixteen_dp); - uiSettings.setLogoMargins(sixteenDp, sixteenDp, sixteenDp, sixteenDp); - } - - // Attribution - uiSettings.setAttributionEnabled(options.getAttributionEnabled()); - uiSettings.setAttributionGravity(options.getAttributionGravity()); - int[] attributionMargins = options.getAttributionMargins(); - if (attributionMargins != null) { - uiSettings.setAttributionMargins(attributionMargins[0], attributionMargins[1], attributionMargins[2], attributionMargins[3]); - } else { - int sevenDp = (int) resources.getDimension(R.dimen.mapbox_seven_dp); - int seventySixDp = (int) resources.getDimension(R.dimen.mapbox_seventy_six_dp); - uiSettings.setAttributionMargins(seventySixDp, sevenDp, sevenDp, sevenDp); - } - - int attributionTintColor = options.getAttributionTintColor(); - uiSettings.setAttributionTintColor(attributionTintColor != -1 - ? attributionTintColor : ColorUtils.getPrimaryColor(context)); } // Style @@ -799,6 +711,17 @@ public final class MapboxMap { } // + // API endpoint config + // + + private void setApiBaseUrl(@NonNull MapboxMapOptions options) { + String apiBaseUrl = options.getApiBaseUrl(); + if (!TextUtils.isEmpty(apiBaseUrl)) { + nativeMapView.setApiBaseUrl(apiBaseUrl); + } + } + + // // Styling // @@ -859,6 +782,18 @@ public final class MapboxMap { } /** + * Loads a new map style from MapboxMapOptions if available. + * + * @param options the object containing the style url + */ + private void setStyleUrl(@NonNull MapboxMapOptions options) { + String style = options.getStyle(); + if (!TextUtils.isEmpty(style)) { + setStyleUrl(style); + } + } + + /** * <p> * Returns the map style currently displayed in the map view. * </p> @@ -912,6 +847,15 @@ public final class MapboxMap { return nativeMapView.getAccessToken(); } + private void setAccessToken(@NonNull MapboxMapOptions options) { + String accessToken = options.getAccessToken(); + if (!TextUtils.isEmpty(accessToken)) { + nativeMapView.setAccessToken(accessToken); + } else { + nativeMapView.setAccessToken(MapboxAccountManager.getInstance().getAccessToken()); + } + } + // // Annotations // diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Transform.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Transform.java index 4aefb0bb7e..4990e86772 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Transform.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Transform.java @@ -7,6 +7,7 @@ import android.support.annotation.UiThread; import com.mapbox.mapboxsdk.annotations.MarkerViewManager; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.camera.CameraUpdate; +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.widgets.MyLocationView; @@ -40,6 +41,13 @@ final class Transform implements MapView.OnMapChangedListener { mapView.addOnMapChangedListener(this); } + void initialise(@NonNull MapboxMap mapboxMap, @NonNull MapboxMapOptions options) { + CameraPosition position = options.getCamera(); + if (position != null && !position.equals(CameraPosition.DEFAULT)) { + moveCamera(mapboxMap, CameraUpdateFactory.newCameraPosition(position), null); + } + } + // // Camera API // diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java index 1523fcefa7..91d1c243f9 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java @@ -1,5 +1,7 @@ package com.mapbox.mapboxsdk.maps; +import android.content.Context; +import android.content.res.Resources; import android.graphics.Color; import android.graphics.PointF; import android.support.annotation.ColorInt; @@ -52,11 +54,74 @@ public final class UiSettings { this.compassView = compassView; this.attributionsView = attributionsView; this.logoView = logoView; - if(logoView.getResources()!=null) { + if (logoView.getResources() != null) { this.pixelRatio = logoView.getResources().getDisplayMetrics().density; } } + void initialise(@NonNull Context context, @NonNull MapboxMapOptions options) { + Resources resources = context.getResources(); + initialiseGestures(options); + initialiseCompass(options, resources); + initialiseLogo(options, resources); + initialiseAttribution(context, options); + } + + private void initialiseGestures(MapboxMapOptions options) { + setZoomGesturesEnabled(options.getZoomGesturesEnabled()); + setZoomGestureChangeAllowed(options.getZoomGesturesEnabled()); + setScrollGesturesEnabled(options.getScrollGesturesEnabled()); + setScrollGestureChangeAllowed(options.getScrollGesturesEnabled()); + setRotateGesturesEnabled(options.getRotateGesturesEnabled()); + setRotateGestureChangeAllowed(options.getRotateGesturesEnabled()); + setTiltGesturesEnabled(options.getTiltGesturesEnabled()); + setTiltGestureChangeAllowed(options.getTiltGesturesEnabled()); + setZoomControlsEnabled(options.getZoomControlsEnabled()); + } + + private void initialiseCompass(MapboxMapOptions options, Resources resources) { + setCompassEnabled(options.getCompassEnabled()); + setCompassGravity(options.getCompassGravity()); + int[] compassMargins = options.getCompassMargins(); + if (compassMargins != null) { + setCompassMargins(compassMargins[0], compassMargins[1], compassMargins[2], compassMargins[3]); + } else { + int tenDp = (int) resources.getDimension(R.dimen.mapbox_ten_dp); + setCompassMargins(tenDp, tenDp, tenDp, tenDp); + } + setCompassFadeFacingNorth(options.getCompassFadeFacingNorth()); + } + + private void initialiseLogo(MapboxMapOptions options, Resources resources) { + setLogoEnabled(options.getLogoEnabled()); + setLogoGravity(options.getLogoGravity()); + int[] logoMargins = options.getLogoMargins(); + if (logoMargins != null) { + setLogoMargins(logoMargins[0], logoMargins[1], logoMargins[2], logoMargins[3]); + } else { + int sixteenDp = (int) resources.getDimension(R.dimen.mapbox_sixteen_dp); + setLogoMargins(sixteenDp, sixteenDp, sixteenDp, sixteenDp); + } + } + + private void initialiseAttribution(Context context, MapboxMapOptions options) { + Resources resources = context.getResources(); + setAttributionEnabled(options.getAttributionEnabled()); + setAttributionGravity(options.getAttributionGravity()); + int[] attributionMargins = options.getAttributionMargins(); + if (attributionMargins != null) { + setAttributionMargins(attributionMargins[0], attributionMargins[1], attributionMargins[2], attributionMargins[3]); + } else { + int sevenDp = (int) resources.getDimension(R.dimen.mapbox_seven_dp); + int seventySixDp = (int) resources.getDimension(R.dimen.mapbox_seventy_six_dp); + setAttributionMargins(seventySixDp, sevenDp, sevenDp, sevenDp); + } + + int attributionTintColor = options.getAttributionTintColor(); + setAttributionTintColor(attributionTintColor != -1 + ? attributionTintColor : ColorUtils.getPrimaryColor(context)); + } + /** * <p> * Enables or disables the compass. The compass is an icon on the map that indicates the diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationViewSettings.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationViewSettings.java index 5ff97b3af0..e85ed96927 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationViewSettings.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationViewSettings.java @@ -4,9 +4,12 @@ import android.graphics.PointF; import android.graphics.drawable.Drawable; import android.support.annotation.ColorInt; import android.support.annotation.IntRange; +import android.support.annotation.NonNull; import com.mapbox.mapboxsdk.constants.MyLocationTracking; import com.mapbox.mapboxsdk.maps.FocalPointChangeListener; +import com.mapbox.mapboxsdk.camera.CameraPosition; +import com.mapbox.mapboxsdk.maps.MapboxMapOptions; import com.mapbox.mapboxsdk.maps.Projection; /** @@ -74,6 +77,19 @@ public class MyLocationViewSettings { this.focalPointChangeListener = focalPointChangedListener; } + public void initialise(@NonNull MapboxMapOptions options) { + CameraPosition position = options.getCamera(); + if (position != null && !position.equals(CameraPosition.DEFAULT)) { + setTilt(position.tilt); + } + setForegroundDrawable(options.getMyLocationForegroundDrawable(), options.getMyLocationForegroundBearingDrawable()); + setForegroundTintColor(options.getMyLocationForegroundTintColor()); + setBackgroundDrawable(options.getMyLocationBackgroundDrawable(), options.getMyLocationBackgroundPadding()); + setBackgroundTintColor(options.getMyLocationBackgroundTintColor()); + setAccuracyAlpha(options.getMyLocationAccuracyAlpha()); + setAccuracyTintColor(options.getMyLocationAccuracyTintColor()); + } + /** * Returns if the MyLocationView is enabled * @@ -277,5 +293,4 @@ public class MyLocationViewSettings { focalPointChangeListener.onFocalPointChanged(null); } } - } |