Custom location source fix (#9142)
* [android] - custom location engine fixes (#9139) * Update to latest LOST dependency, fixup internal location source integration
This commit is contained in:
@@ -30,6 +30,7 @@ public final class Mapbox {
|
||||
private Context context;
|
||||
private String accessToken;
|
||||
private Boolean connected;
|
||||
private LocationSource locationSource;
|
||||
|
||||
/**
|
||||
* Get an instance of Mapbox.
|
||||
@@ -45,8 +46,8 @@ public final class Mapbox {
|
||||
public static synchronized Mapbox getInstance(@NonNull Context context, @NonNull String accessToken) {
|
||||
if (INSTANCE == null) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
INSTANCE = new Mapbox(appContext, accessToken);
|
||||
LocationEngine locationEngine = LocationSource.getLocationEngine(appContext);
|
||||
INSTANCE = new Mapbox(appContext, accessToken, new LocationSource(appContext));
|
||||
LocationEngine locationEngine = new LocationSource(appContext);
|
||||
locationEngine.setPriority(LocationEnginePriority.NO_POWER);
|
||||
MapboxTelemetry.getInstance().initialize(
|
||||
appContext, accessToken, BuildConfig.MAPBOX_EVENTS_USER_AGENT, locationEngine);
|
||||
@@ -55,9 +56,10 @@ public final class Mapbox {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
Mapbox(@NonNull Context context, @NonNull String accessToken) {
|
||||
Mapbox(@NonNull Context context, @NonNull String accessToken, LocationSource locationSource) {
|
||||
this.context = context;
|
||||
this.accessToken = accessToken;
|
||||
this.locationSource = locationSource;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,4 +130,8 @@ public final class Mapbox {
|
||||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
|
||||
return (activeNetwork != null && activeNetwork.isConnected());
|
||||
}
|
||||
|
||||
public static LocationSource getLocationSource() {
|
||||
return INSTANCE.locationSource;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,5 +134,5 @@ public class MapboxConstants {
|
||||
public static final String STATE_ATTRIBUTION_MARGIN_BOTTOM = "mapbox_atrrMarginBottom";
|
||||
public static final String STATE_ATTRIBUTION_ENABLED = "mapbox_atrrEnabled";
|
||||
public static final String STATE_LOCATION_CHANGE_ANIMATION_ENABLED = "mapbox_locationChangeAnimationEnabled";
|
||||
|
||||
public static final String STATE_USING_CUSTOM_LOCATION_SOURCE = "mapbox_usingCustomLocationSource";
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.mapbox.mapboxsdk.location;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.mapbox.services.android.telemetry.location.LocationEngine;
|
||||
import com.mapbox.services.android.telemetry.location.LocationEngineListener;
|
||||
@@ -13,10 +13,6 @@ import com.mapzen.android.lost.api.LocationRequest;
|
||||
import com.mapzen.android.lost.api.LocationServices;
|
||||
import com.mapzen.android.lost.api.LostApiClient;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
* Manages locational updates. Contains methods to register and unregister location listeners.
|
||||
* <ul>
|
||||
@@ -35,40 +31,24 @@ import timber.log.Timber;
|
||||
public class LocationSource extends LocationEngine implements
|
||||
LostApiClient.ConnectionCallbacks, LocationListener {
|
||||
|
||||
private static LocationEngine instance;
|
||||
|
||||
private WeakReference<Context> context;
|
||||
private Context context;
|
||||
private LostApiClient lostApiClient;
|
||||
|
||||
private LocationSource(Context context) {
|
||||
public LocationSource(Context context) {
|
||||
super();
|
||||
this.context = new WeakReference<>(context);
|
||||
lostApiClient = new LostApiClient.Builder(this.context.get())
|
||||
this.context = context.getApplicationContext();
|
||||
lostApiClient = new LostApiClient.Builder(this.context)
|
||||
.addConnectionCallbacks(this)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the LocationEngine instance.
|
||||
*
|
||||
* @param context a Context from which the application context is derived
|
||||
* @return the LocationEngine instance
|
||||
*/
|
||||
public static synchronized LocationEngine getLocationEngine(@NonNull Context context) {
|
||||
if (instance == null) {
|
||||
instance = new LocationSource(context.getApplicationContext());
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate the location engine which will connect whichever location provider you are using. You'll need to call
|
||||
* this before requesting user location updates using {@link LocationEngine#requestLocationUpdates()}.
|
||||
*/
|
||||
@Override
|
||||
public void activate() {
|
||||
if (lostApiClient != null && !lostApiClient.isConnected()) {
|
||||
if (!lostApiClient.isConnected()) {
|
||||
lostApiClient.connect();
|
||||
}
|
||||
}
|
||||
@@ -80,7 +60,7 @@ public class LocationSource extends LocationEngine implements
|
||||
*/
|
||||
@Override
|
||||
public void deactivate() {
|
||||
if (lostApiClient != null && lostApiClient.isConnected()) {
|
||||
if (lostApiClient.isConnected()) {
|
||||
lostApiClient.disconnect();
|
||||
}
|
||||
}
|
||||
@@ -111,7 +91,6 @@ public class LocationSource extends LocationEngine implements
|
||||
*/
|
||||
@Override
|
||||
public void onConnectionSuspended() {
|
||||
Timber.d("Connection suspended.");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,8 +99,9 @@ public class LocationSource extends LocationEngine implements
|
||||
* @return the last known location
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public Location getLastLocation() {
|
||||
if (lostApiClient.isConnected() && PermissionsManager.areLocationPermissionsGranted(context.get())) {
|
||||
if (lostApiClient.isConnected() && PermissionsManager.areLocationPermissionsGranted(context)) {
|
||||
//noinspection MissingPermission
|
||||
return LocationServices.FusedLocationApi.getLastLocation(lostApiClient);
|
||||
}
|
||||
@@ -151,7 +131,7 @@ public class LocationSource extends LocationEngine implements
|
||||
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
|
||||
}
|
||||
|
||||
if (lostApiClient.isConnected() && PermissionsManager.areLocationPermissionsGranted(context.get())) {
|
||||
if (lostApiClient.isConnected() && PermissionsManager.areLocationPermissionsGranted(context)) {
|
||||
//noinspection MissingPermission
|
||||
LocationServices.FusedLocationApi.requestLocationUpdates(lostApiClient, request, this);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,6 @@ import com.mapbox.mapboxsdk.constants.MyLocationTracking;
|
||||
import com.mapbox.mapboxsdk.constants.Style;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
|
||||
import com.mapbox.mapboxsdk.location.LocationSource;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
|
||||
import com.mapbox.mapboxsdk.style.layers.Filter;
|
||||
import com.mapbox.mapboxsdk.style.layers.Layer;
|
||||
@@ -1872,8 +1871,6 @@ public final class MapboxMap {
|
||||
* Replaces the location source of the my-location layer.
|
||||
*
|
||||
* @param locationSource A {@link LocationEngine} location source to use in the my-location layer.
|
||||
* Set to null to use the default {@link LocationSource}
|
||||
* location source.
|
||||
*/
|
||||
@UiThread
|
||||
public void setLocationSource(@Nullable LocationEngine locationSource) {
|
||||
|
||||
@@ -6,11 +6,11 @@ import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import com.mapbox.mapboxsdk.Mapbox;
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
|
||||
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
|
||||
import com.mapbox.mapboxsdk.location.LocationSource;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationView;
|
||||
import com.mapbox.services.android.telemetry.location.LocationEngine;
|
||||
import com.mapbox.services.android.telemetry.location.LocationEngineListener;
|
||||
@@ -30,6 +30,7 @@ public final class TrackingSettings {
|
||||
private LocationEngine locationSource;
|
||||
private LocationEngineListener myLocationListener;
|
||||
private boolean locationChangeAnimationEnabled = true;
|
||||
private boolean isCustomLocationSource;
|
||||
|
||||
private boolean myLocationEnabled;
|
||||
private boolean dismissLocationTrackingOnGesture = true;
|
||||
@@ -47,7 +48,7 @@ public final class TrackingSettings {
|
||||
}
|
||||
|
||||
void initialise(MapboxMapOptions options) {
|
||||
locationSource = LocationSource.getLocationEngine(myLocationView.getContext());
|
||||
locationSource = Mapbox.getLocationSource();
|
||||
setMyLocationEnabled(options.getLocationEnabled());
|
||||
}
|
||||
|
||||
@@ -58,11 +59,15 @@ public final class TrackingSettings {
|
||||
outState.putBoolean(MapboxConstants.STATE_MY_BEARING_TRACKING_DISMISS, isDismissBearingTrackingOnGesture());
|
||||
outState.putBoolean(MapboxConstants.STATE_MY_LOCATION_ENABLED, isMyLocationEnabled());
|
||||
outState.putBoolean(MapboxConstants.STATE_LOCATION_CHANGE_ANIMATION_ENABLED, isLocationChangeAnimationEnabled());
|
||||
outState.putBoolean(MapboxConstants.STATE_USING_CUSTOM_LOCATION_SOURCE, isCustomLocationSource());
|
||||
}
|
||||
|
||||
void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
try {
|
||||
setMyLocationEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_MY_LOCATION_ENABLED));
|
||||
setMyLocationEnabled(
|
||||
savedInstanceState.getBoolean(MapboxConstants.STATE_MY_LOCATION_ENABLED),
|
||||
savedInstanceState.getBoolean(MapboxConstants.STATE_USING_CUSTOM_LOCATION_SOURCE)
|
||||
);
|
||||
} catch (SecurityException ignore) {
|
||||
// User did not accept location permissions
|
||||
}
|
||||
@@ -339,6 +344,10 @@ public final class TrackingSettings {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isCustomLocationSource() {
|
||||
return isCustomLocationSource;
|
||||
}
|
||||
|
||||
void setOnMyLocationTrackingModeChangeListener(MapboxMap.OnMyLocationTrackingModeChangeListener listener) {
|
||||
this.onMyLocationTrackingModeChangeListener = listener;
|
||||
}
|
||||
@@ -357,16 +366,30 @@ public final class TrackingSettings {
|
||||
}
|
||||
|
||||
void setMyLocationEnabled(boolean locationEnabled) {
|
||||
setMyLocationEnabled(locationEnabled, isCustomLocationSource());
|
||||
}
|
||||
|
||||
private void setMyLocationEnabled(boolean locationEnabled, boolean isCustomLocationSource) {
|
||||
if (!PermissionsManager.areLocationPermissionsGranted(myLocationView.getContext())) {
|
||||
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);
|
||||
this.isCustomLocationSource = isCustomLocationSource;
|
||||
myLocationView.setEnabled(locationEnabled, isCustomLocationSource);
|
||||
}
|
||||
|
||||
void setLocationSource(LocationEngine locationSource) {
|
||||
if (this.locationSource != null && this.locationSource.equals(locationSource)) {
|
||||
// this source is already active
|
||||
return;
|
||||
}
|
||||
|
||||
this.isCustomLocationSource = locationSource != null;
|
||||
if (locationSource == null) {
|
||||
locationSource = Mapbox.getLocationSource();
|
||||
}
|
||||
this.locationSource = locationSource;
|
||||
myLocationView.setLocationSource(locationSource);
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.mapbox.mapboxsdk.Mapbox;
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
|
||||
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
|
||||
@@ -40,6 +41,8 @@ import com.mapbox.services.android.telemetry.location.LocationEnginePriority;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
* UI element overlaid on a map to show the user's location.
|
||||
*/
|
||||
@@ -148,6 +151,10 @@ public class MyLocationView extends View {
|
||||
compassListener = new CompassListener(context);
|
||||
}
|
||||
|
||||
public void init(LocationSource locationSource) {
|
||||
this.locationSource = locationSource;
|
||||
}
|
||||
|
||||
public final void setForegroundDrawables(Drawable defaultDrawable, Drawable bearingDrawable) {
|
||||
if (defaultDrawable == null) {
|
||||
return;
|
||||
@@ -391,9 +398,13 @@ public class MyLocationView extends View {
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
setEnabled(enabled, false);
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled, boolean isCustomLocationSource) {
|
||||
super.setEnabled(enabled);
|
||||
setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
|
||||
toggleGps(enabled);
|
||||
toggleGps(enabled, isCustomLocationSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -414,22 +425,23 @@ public class MyLocationView extends View {
|
||||
super.onRestoreInstanceState(state);
|
||||
}
|
||||
|
||||
private void toggleGps(boolean enableGps) {
|
||||
toggleGps(enableGps, mapboxMap != null && mapboxMap.getTrackingSettings().isCustomLocationSource());
|
||||
}
|
||||
|
||||
/**
|
||||
* Enabled / Disable GPS location updates along with updating the UI
|
||||
*
|
||||
* @param enableGps true if GPS is to be enabled, false if GPS is to be disabled
|
||||
*/
|
||||
private void toggleGps(boolean enableGps) {
|
||||
if (locationSource == null) {
|
||||
locationSource = LocationSource.getLocationEngine(this.getContext());
|
||||
}
|
||||
|
||||
private void toggleGps(boolean enableGps, boolean isCustomLocationSource) {
|
||||
if (enableGps) {
|
||||
// Set an initial location if one available
|
||||
Location lastLocation = locationSource.getLastLocation();
|
||||
|
||||
if (lastLocation != null) {
|
||||
setLocation(lastLocation);
|
||||
if (locationSource == null) {
|
||||
if (!isCustomLocationSource) {
|
||||
locationSource = Mapbox.getLocationSource();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (userLocationListener == null) {
|
||||
@@ -437,16 +449,18 @@ public class MyLocationView extends View {
|
||||
}
|
||||
|
||||
locationSource.addLocationEngineListener(userLocationListener);
|
||||
locationSource.setPriority(LocationEnginePriority.HIGH_ACCURACY);
|
||||
locationSource.activate();
|
||||
} else {
|
||||
if (locationSource == null) {
|
||||
return;
|
||||
}
|
||||
// Disable location and user dot
|
||||
location = null;
|
||||
locationSource.removeLocationUpdates();
|
||||
locationSource.removeLocationEngineListener(userLocationListener);
|
||||
locationSource.removeLocationUpdates();
|
||||
locationSource.deactivate();
|
||||
}
|
||||
|
||||
locationSource.setPriority(LocationEnginePriority.HIGH_ACCURACY);
|
||||
}
|
||||
|
||||
public Location getLocation() {
|
||||
@@ -564,7 +578,10 @@ public class MyLocationView extends View {
|
||||
}
|
||||
|
||||
public void setLocationSource(LocationEngine locationSource) {
|
||||
toggleGps(false);
|
||||
this.locationSource = locationSource;
|
||||
this.userLocationListener = null;
|
||||
setEnabled(isEnabled(), locationSource != null);
|
||||
}
|
||||
|
||||
private static class GpsLocationListener implements LocationEngineListener {
|
||||
@@ -580,10 +597,12 @@ public class MyLocationView extends View {
|
||||
@Override
|
||||
public void onConnected() {
|
||||
MyLocationView locationView = userLocationView.get();
|
||||
if (locationView != null) {
|
||||
LocationEngine locationEngine = locationSource.get();
|
||||
Location location = locationEngine.getLastLocation();
|
||||
locationView.setLocation(location);
|
||||
LocationEngine locationEngine = locationSource.get();
|
||||
if (locationView != null && locationEngine != null) {
|
||||
Location lastKnownLocation = locationEngine.getLastLocation();
|
||||
if (lastKnownLocation != null) {
|
||||
locationView.setLocation(lastKnownLocation);
|
||||
}
|
||||
locationEngine.requestLocationUpdates();
|
||||
}
|
||||
}
|
||||
@@ -627,6 +646,9 @@ public class MyLocationView extends View {
|
||||
}
|
||||
|
||||
public boolean isSensorAvailable() {
|
||||
if (rotationVectorSensor == null) {
|
||||
Timber.e("Sensor.TYPE_ROTATION_VECTOR is missing from this device. Unable to use MyBearingTracking.COMPASS.");
|
||||
}
|
||||
return rotationVectorSensor != null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user