diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com')
6 files changed, 70 insertions, 34 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java index 280877d61a..2cd8e9245e 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java @@ -8,6 +8,7 @@ import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; + import com.mapbox.mapboxsdk.utils.MapFragmentUtils; import java.util.ArrayList; @@ -22,6 +23,10 @@ import java.util.List; * using a FragmentManager. * </p> * <p> + * If you are planning on using multiple MapView instances in one lifecycle + * you have to specify a unique ID for each instance with {@link MapboxMapOptions#setMapId(String)} or in xml attributes. + * </p> + * <p> * To get a reference to the MapView, use {@link #getMapAsync(OnMapReadyCallback)}} * </p> * @@ -143,17 +148,6 @@ public final class MapFragment extends Fragment implements OnMapReadyCallback { } /** - * Called when the fragment state needs to be saved. - * - * @param outState The saved state - */ - @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - super.onSaveInstanceState(outState); - map.onSaveInstanceState(outState); - } - - /** * Called when the fragment is no longer visible for the user. */ @Override 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 73fc023ee4..edd93f6ae7 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 @@ -72,6 +72,10 @@ import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_WAIT_IDLE; * and style the features of the map to fit your application's use case. * </p> * <p> + * If you are planning on using multiple MapView instances in one lifecycle + * you have to specify a unique ID for each instance with {@link MapboxMapOptions#setMapId(String)} or in xml attributes. + * </p> + * <p> * Use of {@code MapView} requires a Mapbox API access token. * Obtain an access token on the <a href="https://www.mapbox.com/studio/account/tokens/">Mapbox account page</a>. * </p> @@ -135,16 +139,28 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { // in IDE layout editor, just return return; } + + if (!(context instanceof FragmentActivity)) { + throw new IllegalArgumentException("You need to instantiate MapView from FragmentActivity context. " + + "If your LayoutInflater works with a different context try creating MapView programmatically."); + } + mapboxMapOptions = options; + uiSettings = ViewModelProviders.of((FragmentActivity) context) + .get(mapboxMapOptions.getMapId() + UiSettings.class.getCanonicalName(), UiSettings.class); + mapSettings = ViewModelProviders.of((FragmentActivity) context) + .get(mapboxMapOptions.getMapId() + MapSettings.class.getCanonicalName(), MapSettings.class); + // inflate view View view = LayoutInflater.from(context).inflate(R.layout.mapbox_mapview_internal, this); compassView = (CompassView) view.findViewById(R.id.compassView); + compassView.injectUiSettings(uiSettings); attrView = (ImageView) view.findViewById(R.id.attributionView); logoView = (ImageView) view.findViewById(R.id.logoView); // create widget updater - WidgetUpdater widgetUpdater = new WidgetUpdater(context, compassView, attrView, logoView); + WidgetUpdater widgetUpdater = new WidgetUpdater(uiSettings, compassView, attrView, logoView); widgetUpdater.initialiseCompassObservableSettings(context); widgetUpdater.initialiseAttributionObservableSettings(context); widgetUpdater.initialiseLogoObservableSettings(context); @@ -169,12 +185,9 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { // setup components for MapboxMap creation Projection proj = new Projection(nativeMapView); - uiSettings = ViewModelProviders.of((FragmentActivity) context).get(UiSettings.class); uiSettings.initialiseProjection(proj); uiSettings.getFocalPointObservable().observe((LifecycleOwner) context, point -> this.focalPoint = point); - mapSettings = ViewModelProviders.of((FragmentActivity) context).get(MapSettings.class); - LongSparseArray<Annotation> annotationsArray = new LongSparseArray<>(); MarkerViewManager markerViewManager = new MarkerViewManager((ViewGroup) findViewById(R.id.markerViewContainer)); IconManager iconManager = new IconManager(nativeMapView); @@ -208,7 +221,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { compassView.injectCompassAnimationListener(createCompassAnimationListener(cameraChangeDispatcher)); compassView.setOnClickListener(createCompassClickListener(cameraChangeDispatcher)); // inject widgets with MapboxMap - attributionClickListener = new AttributionClickListener(context, mapboxMap); + attributionClickListener = new AttributionClickListener(context, uiSettings, mapboxMap); attrView.setOnClickListener(attributionClickListener); // Ensure this view is interactable @@ -1203,11 +1216,10 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { private final AttributionDialogManager defaultDialogManager; private AttributionDialogManager currentDialogManager; - private AttributionClickListener(Context context, MapboxMap mapboxMap) { + private AttributionClickListener(Context context, UiSettings uiSettings, MapboxMap mapboxMap) { this.defaultDialogManager = new AttributionDialogManager(context, mapboxMap); currentDialogManager = defaultDialogManager; - UiSettings uiSettings = ViewModelProviders.of((FragmentActivity) context).get(UiSettings.class); uiSettings.getAttributionDialogManagerObservable().observe( (LifecycleOwner) context, this::setCurrentDialogManager); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java index bb4e2f9212..c15598fba9 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java @@ -76,6 +76,8 @@ public class MapboxMapOptions implements Parcelable { private String style; + private String mapId; + /** * Creates a new MapboxMapOptions object. */ @@ -122,6 +124,8 @@ public class MapboxMapOptions implements Parcelable { prefetchesTiles = in.readByte() != 0; zMediaOverlay = in.readByte() != 0; localIdeographFontFamily = in.readString(); + + mapId = in.readString(); } /** @@ -139,6 +143,7 @@ public class MapboxMapOptions implements Parcelable { mapboxMapOptions.camera(new CameraPosition.Builder(typedArray).build()); mapboxMapOptions.styleUrl(typedArray.getString(R.styleable.mapbox_MapView_mapbox_styleUrl)); mapboxMapOptions.apiBaseUrl(typedArray.getString(R.styleable.mapbox_MapView_mapbox_apiBaseUrl)); + mapboxMapOptions.setMapId(typedArray.getString(R.styleable.mapbox_MapView_mapbox_mapId)); mapboxMapOptions.zoomGesturesEnabled( typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_uiZoomGestures, true)); @@ -258,6 +263,20 @@ public class MapboxMapOptions implements Parcelable { } /** + * Sets a unique ID for this {@link MapView} used in a multi-map setup. + * <p> + * If you are using multiple MapViews or {@link MapFragment}/{@link SupportMapFragment} + * you need to assign a unique ID to each instance of the MapView for state management purposes. + * + * @param id Map ID used in multi-map setup. + * @return This + */ + public MapboxMapOptions setMapId(String id) { + mapId = id; + return this; + } + + /** * Specifies the used debug type for a map view. * * @param enabled True is debug is enabled @@ -691,6 +710,16 @@ public class MapboxMapOptions implements Parcelable { } /** + * Get this map ID used in a multi-map setup. + * + * @return Map ID used in multi-map setup. + * @see #setMapId(String) + */ + public String getMapId() { + return mapId; + } + + /** * Get the current configured rotate gesture state for a map view. * * @return True indicates gesture is enabled @@ -866,6 +895,8 @@ public class MapboxMapOptions implements Parcelable { dest.writeByte((byte) (prefetchesTiles ? 1 : 0)); dest.writeByte((byte) (zMediaOverlay ? 1 : 0)); dest.writeString(localIdeographFontFamily); + + dest.writeString(mapId); } @Override @@ -962,6 +993,9 @@ public class MapboxMapOptions implements Parcelable { if (localIdeographFontFamily != options.localIdeographFontFamily) { return false; } + if (mapId != null ? !mapId.equals(options.mapId) : options.mapId != null) { + return false; + } return false; } @@ -1001,6 +1035,7 @@ public class MapboxMapOptions implements Parcelable { result = 31 * result + (prefetchesTiles ? 1 : 0); result = 31 * result + (zMediaOverlay ? 1 : 0); result = 31 * result + (localIdeographFontFamily != null ? localIdeographFontFamily.hashCode() : 0); + result = 31 * result + (mapId != null ? mapId.hashCode() : 0); return result; } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java index 307b33b0c7..3b30424c63 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java @@ -23,6 +23,10 @@ import java.util.List; * using a FragmentManager. * </p> * <p> + * If you are planning on using multiple MapView instances in one lifecycle + * you have to specify a unique ID for each instance with {@link MapboxMapOptions#setMapId(String)} or in xml attributes. + * </p> + * <p> * To get a reference to the MapView, use {@link #getMapAsync(OnMapReadyCallback)}} * </p> * @@ -139,17 +143,6 @@ public class SupportMapFragment extends Fragment implements OnMapReadyCallback { } /** - * Called when the fragment state needs to be saved. - * - * @param outState The saved state - */ - @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - super.onSaveInstanceState(outState); - map.onSaveInstanceState(outState); - } - - /** * Called when the fragment is no longer visible for the user. */ @Override diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java index 0c8f6f48d3..712e137238 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java @@ -59,8 +59,6 @@ public final class CompassView extends ImageView implements Runnable { } private void initialize(Context context) { - uiSettings = ViewModelProviders.of((FragmentActivity) context).get(UiSettings.class); - // Layout params float screenDensity = context.getResources().getDisplayMetrics().density; ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams((int) (48 * screenDensity), (int) (48 * screenDensity)); @@ -71,6 +69,10 @@ public final class CompassView extends ImageView implements Runnable { this.compassAnimationListener = compassAnimationListener; } + public void injectUiSettings(@NonNull UiSettings uiSettings) { + this.uiSettings = uiSettings; + } + public void isAnimating(boolean isAnimating) { this.isAnimating = isAnimating; } @@ -118,7 +120,7 @@ public final class CompassView extends ImageView implements Runnable { */ @Deprecated public void update(final double bearing) { - if (uiSettings.getCompassRotationObservable().getValue() != bearing) { + if (uiSettings != null && uiSettings.getCompassRotationObservable().getValue() != bearing) { uiSettings.getCompassRotationObservable().setValue(bearing); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/WidgetUpdater.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/WidgetUpdater.java index 26b34e4bb9..0f3dbac309 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/WidgetUpdater.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/WidgetUpdater.java @@ -25,12 +25,12 @@ public final class WidgetUpdater { private final UiSettings uiSettings; - public WidgetUpdater(@NonNull Context context, @NonNull CompassView compassView, + public WidgetUpdater(@NonNull UiSettings uiSettings, @NonNull CompassView compassView, @NonNull ImageView attributionView, @NonNull View logoView) { this.compassView = compassView; this.attributionView = attributionView; this.logoView = logoView; - uiSettings = ViewModelProviders.of((FragmentActivity) context).get(UiSettings.class); + this.uiSettings = uiSettings; } public void initialiseCompassObservableSettings(Context context) { |