[android] - remove location enabled flag from MapboxMap (#7308)
* [android] - remove location enabled flag from MapboxMap, decouple TrackingSettings from MyLocationViewSettings using a FocalPointChangeListener interface, Move state related to tracking modes to MyLocationView (shared object between MyLocationViewSettings and FocalPointChangeListener). * fixup tests
This commit is contained in:
@@ -173,7 +173,7 @@ public class MapView extends FrameLayout {
|
||||
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(projection, myLocationView, trackingSettings);
|
||||
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);
|
||||
|
||||
@@ -75,8 +75,6 @@ public final class MapboxMap {
|
||||
|
||||
private MapboxMap.OnFpsChangedListener onFpsChangedListener;
|
||||
|
||||
private boolean myLocationEnabled;
|
||||
|
||||
private double maxZoomLevel = -1;
|
||||
private double minZoomLevel = -1;
|
||||
|
||||
@@ -1526,7 +1524,7 @@ public final class MapboxMap {
|
||||
*/
|
||||
@UiThread
|
||||
public boolean isMyLocationEnabled() {
|
||||
return myLocationEnabled;
|
||||
return trackingSettings.isMyLocationEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1543,12 +1541,6 @@ public final class MapboxMap {
|
||||
*/
|
||||
@UiThread
|
||||
public void setMyLocationEnabled(boolean enabled) {
|
||||
if (!trackingSettings.isPermissionsAccepted()) {
|
||||
Timber.e("Could not activate user location tracking: "
|
||||
+ "user did not accept the permission or permissions were not requested.");
|
||||
return;
|
||||
}
|
||||
myLocationEnabled = enabled;
|
||||
trackingSettings.setMyLocationEnabled(enabled);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ import com.mapbox.mapboxsdk.location.LocationListener;
|
||||
import com.mapbox.mapboxsdk.location.LocationServices;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationView;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
* Settings for the user location and bearing tracking of a MapboxMap.
|
||||
*/
|
||||
@@ -26,16 +28,12 @@ public final class TrackingSettings {
|
||||
private final FocalPointChangeListener focalPointChangedListener;
|
||||
private LocationListener myLocationListener;
|
||||
|
||||
@MyLocationTracking.Mode
|
||||
private int myLocationTrackingMode;
|
||||
|
||||
@MyBearingTracking.Mode
|
||||
private int myBearingTrackingMode;
|
||||
private boolean myLocationEnabled;
|
||||
private boolean dismissLocationTrackingOnGesture = true;
|
||||
private boolean dismissBearingTrackingOnGesture = true;
|
||||
|
||||
private MapboxMap.OnMyLocationTrackingModeChangeListener onMyLocationTrackingModeChangeListener;
|
||||
private MapboxMap.OnMyBearingTrackingModeChangeListener onMyBearingTrackingModeChangeListener;
|
||||
private boolean dismissLocationTrackingOnGesture = true;
|
||||
private boolean dismissBearingTrackingOnGesture = true;
|
||||
|
||||
TrackingSettings(@NonNull MyLocationView myLocationView, UiSettings uiSettings, FocalPointChangeListener focalPointChangedListener) {
|
||||
this.myLocationView = myLocationView;
|
||||
@@ -58,7 +56,6 @@ public final class TrackingSettings {
|
||||
*/
|
||||
@UiThread
|
||||
public void setMyLocationTrackingMode(@MyLocationTracking.Mode int myLocationTrackingMode) {
|
||||
this.myLocationTrackingMode = myLocationTrackingMode;
|
||||
myLocationView.setMyLocationTrackingMode(myLocationTrackingMode);
|
||||
|
||||
if (myLocationTrackingMode == MyLocationTracking.TRACKING_FOLLOW) {
|
||||
@@ -82,7 +79,7 @@ public final class TrackingSettings {
|
||||
@UiThread
|
||||
@MyLocationTracking.Mode
|
||||
public int getMyLocationTrackingMode() {
|
||||
return myLocationTrackingMode;
|
||||
return myLocationView.getMyLocationTrackingMode();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +99,6 @@ public final class TrackingSettings {
|
||||
*/
|
||||
@UiThread
|
||||
public void setMyBearingTrackingMode(@MyBearingTracking.Mode int myBearingTrackingMode) {
|
||||
this.myBearingTrackingMode = myBearingTrackingMode;
|
||||
myLocationView.setMyBearingTrackingMode(myBearingTrackingMode);
|
||||
if (onMyBearingTrackingModeChangeListener != null) {
|
||||
onMyBearingTrackingModeChangeListener.onMyBearingTrackingModeChange(myBearingTrackingMode);
|
||||
@@ -119,7 +115,7 @@ public final class TrackingSettings {
|
||||
@UiThread
|
||||
@MyBearingTracking.Mode
|
||||
public int getMyBearingTrackingMode() {
|
||||
return myBearingTrackingMode;
|
||||
return myLocationView.getMyBearingTrackingMode();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,7 +201,7 @@ public final class TrackingSettings {
|
||||
* @return True if location tracking is disabled.
|
||||
*/
|
||||
public boolean isLocationTrackingDisabled() {
|
||||
return myLocationTrackingMode == MyLocationTracking.TRACKING_NONE;
|
||||
return myLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,7 +210,7 @@ public final class TrackingSettings {
|
||||
* @return True if bearing tracking is disabled.
|
||||
*/
|
||||
public boolean isBearingTrackingDisabled() {
|
||||
return myBearingTrackingMode == MyBearingTracking.NONE;
|
||||
return myLocationView.getMyBearingTrackingMode() == MyBearingTracking.NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,7 +223,9 @@ public final class TrackingSettings {
|
||||
// The user settings are enabled AND;
|
||||
// EITHER bearing tracking is dismissed on gesture OR there is no bearing tracking
|
||||
return uiSettings.isRotateGesturesEnabled() &&
|
||||
(dismissBearingTrackingOnGesture || myBearingTrackingMode == MyBearingTracking.NONE || (myLocationTrackingMode == MyLocationTracking.TRACKING_NONE));
|
||||
(dismissBearingTrackingOnGesture
|
||||
|| myLocationView.getMyBearingTrackingMode() == MyBearingTracking.NONE
|
||||
|| myLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,7 +235,8 @@ public final class TrackingSettings {
|
||||
*/
|
||||
public boolean isScrollGestureCurrentlyEnabled() {
|
||||
return uiSettings.isScrollGesturesEnabled() &&
|
||||
(dismissLocationTrackingOnGesture || myLocationTrackingMode == MyLocationTracking.TRACKING_NONE);
|
||||
(dismissLocationTrackingOnGesture
|
||||
|| myLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,18 +262,6 @@ public final class TrackingSettings {
|
||||
resetTrackingModesIfRequired(cameraPosition.target != null, cameraPosition.bearing != -1);
|
||||
}
|
||||
|
||||
public void invalidateFocalPointForTracking(MyLocationView myLocationView) {
|
||||
if (!isLocationTrackingDisabled()) {
|
||||
focalPointChangedListener.onFocalPointChanged(new PointF(myLocationView.getCenterX(), myLocationView.getCenterY()));
|
||||
} else {
|
||||
focalPointChangedListener.onFocalPointChanged(null);
|
||||
}
|
||||
}
|
||||
|
||||
void setMyLocationEnabled(boolean enabled) {
|
||||
myLocationView.setEnabled(enabled);
|
||||
}
|
||||
|
||||
Location getMyLocation() {
|
||||
return myLocationView.getLocation();
|
||||
}
|
||||
@@ -309,7 +296,22 @@ public final class TrackingSettings {
|
||||
this.onMyBearingTrackingModeChangeListener = onMyBearingTrackingModeChangeListener;
|
||||
}
|
||||
|
||||
MyLocationView getMyLocationView(){
|
||||
MyLocationView getMyLocationView() {
|
||||
return myLocationView;
|
||||
}
|
||||
|
||||
|
||||
boolean isMyLocationEnabled() {
|
||||
return myLocationEnabled;
|
||||
}
|
||||
|
||||
void setMyLocationEnabled(boolean locationEnabled) {
|
||||
if (!isPermissionsAccepted()) {
|
||||
Timber.e("Could not activate user location tracking: "
|
||||
+ "user did not accept the permission or permissions were not requested.");
|
||||
return;
|
||||
}
|
||||
myLocationEnabled = locationEnabled;
|
||||
myLocationView.setEnabled(locationEnabled);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -488,6 +488,17 @@ public class MyLocationView extends View {
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@MyLocationTracking.Mode
|
||||
public int getMyLocationTrackingMode() {
|
||||
return myLocationTrackingMode;
|
||||
}
|
||||
|
||||
|
||||
@MyBearingTracking.Mode
|
||||
public int getMyBearingTrackingMode() {
|
||||
return myBearingTrackingMode;
|
||||
}
|
||||
|
||||
private void setCompass(double bearing) {
|
||||
setCompass(bearing, 0 /* no animation */);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.mapbox.mapboxsdk.maps.widgets;
|
||||
|
||||
import android.graphics.PointF;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.ColorInt;
|
||||
import android.support.annotation.IntRange;
|
||||
|
||||
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
|
||||
import com.mapbox.mapboxsdk.maps.FocalPointChangeListener;
|
||||
import com.mapbox.mapboxsdk.maps.Projection;
|
||||
import com.mapbox.mapboxsdk.maps.TrackingSettings;
|
||||
|
||||
/**
|
||||
* Settings to configure the visual appearance of the MyLocationView.
|
||||
@@ -13,8 +15,8 @@ import com.mapbox.mapboxsdk.maps.TrackingSettings;
|
||||
public class MyLocationViewSettings {
|
||||
|
||||
private Projection projection;
|
||||
private TrackingSettings trackingSettings;
|
||||
private MyLocationView myLocationView;
|
||||
private FocalPointChangeListener focalPointChangeListener;
|
||||
|
||||
//
|
||||
// State
|
||||
@@ -59,15 +61,17 @@ public class MyLocationViewSettings {
|
||||
|
||||
/**
|
||||
* Creates an instance of MyLocationViewSettings
|
||||
* <p>
|
||||
*
|
||||
* @param projection the MapView projection
|
||||
* @param myLocationView the MyLocationView to apply the settings to
|
||||
* @param myLocationView the MyLocationView to apply the settings to
|
||||
* @param projection the MapView projection
|
||||
* @param focalPointChangedListener the interface to be invoked when focal points changes
|
||||
* @see MyLocationView
|
||||
*/
|
||||
public MyLocationViewSettings(Projection projection, MyLocationView myLocationView, TrackingSettings trackingSettings) {
|
||||
this.projection = projection;
|
||||
public MyLocationViewSettings(MyLocationView myLocationView, Projection projection, FocalPointChangeListener focalPointChangedListener) {
|
||||
this.myLocationView = myLocationView;
|
||||
this.trackingSettings = trackingSettings;
|
||||
this.projection = projection;
|
||||
this.focalPointChangeListener = focalPointChangedListener;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,7 +216,7 @@ public class MyLocationViewSettings {
|
||||
padding = new int[]{left, top, right, bottom};
|
||||
myLocationView.setContentPadding(padding);
|
||||
projection.invalidateContentPadding(padding);
|
||||
trackingSettings.invalidateFocalPointForTracking(myLocationView);
|
||||
invalidateFocalPointForTracking(myLocationView);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,4 +269,13 @@ public class MyLocationViewSettings {
|
||||
public void setTilt(double tilt) {
|
||||
myLocationView.setTilt(tilt);
|
||||
}
|
||||
|
||||
private void invalidateFocalPointForTracking(MyLocationView myLocationView) {
|
||||
if (!(myLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE)) {
|
||||
focalPointChangeListener.onFocalPointChanged(new PointF(myLocationView.getCenterX(), myLocationView.getCenterY()));
|
||||
} else {
|
||||
focalPointChangeListener.onFocalPointChanged(null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user