Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Custom location source fix (#9142)
Browse files Browse the repository at this point in the history
*  [android] - custom location engine fixes (#9139)

* Update to latest LOST dependency, fixup internal location source integration
  • Loading branch information
tobrun authored Jun 9, 2017
1 parent 233117f commit 9f88e37
Show file tree
Hide file tree
Showing 20 changed files with 444 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -55,9 +56,10 @@ public static synchronized Mapbox getInstance(@NonNull Context context, @NonNull
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;
}

/**
Expand Down Expand Up @@ -128,4 +130,8 @@ public static synchronized Boolean isConnected() {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return (activeNetwork != null && activeNetwork.isConnected());
}

public static LocationSource getLocationSource() {
return INSTANCE.locationSource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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;
Expand All @@ -13,10 +13,6 @@
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>
Expand All @@ -35,40 +31,24 @@
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();
}
}
Expand All @@ -80,7 +60,7 @@ public void activate() {
*/
@Override
public void deactivate() {
if (lostApiClient != null && lostApiClient.isConnected()) {
if (lostApiClient.isConnected()) {
lostApiClient.disconnect();
}
}
Expand Down Expand Up @@ -111,7 +91,6 @@ public void onConnected() {
*/
@Override
public void onConnectionSuspended() {
Timber.d("Connection suspended.");
}

/**
Expand All @@ -120,8 +99,9 @@ public void onConnectionSuspended() {
* @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);
}
Expand Down Expand Up @@ -151,7 +131,7 @@ public void requestLocationUpdates() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
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;
Expand Down Expand Up @@ -1872,8 +1871,6 @@ public void setOnMyLocationChangeListener(@Nullable MapboxMap.OnMyLocationChange
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
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;
Expand All @@ -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;
Expand All @@ -47,7 +48,7 @@ public final class TrackingSettings {
}

void initialise(MapboxMapOptions options) {
locationSource = LocationSource.getLocationEngine(myLocationView.getContext());
locationSource = Mapbox.getLocationSource();
setMyLocationEnabled(options.getLocationEnabled());
}

Expand All @@ -58,11 +59,15 @@ void onSaveInstanceState(Bundle outState) {
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
}
Expand Down Expand Up @@ -339,6 +344,10 @@ public void onLocationChanged(Location location) {
}
}

public boolean isCustomLocationSource() {
return isCustomLocationSource;
}

void setOnMyLocationTrackingModeChangeListener(MapboxMap.OnMyLocationTrackingModeChangeListener listener) {
this.onMyLocationTrackingModeChangeListener = listener;
}
Expand All @@ -357,16 +366,30 @@ boolean isMyLocationEnabled() {
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -40,6 +41,8 @@

import java.lang.ref.WeakReference;

import timber.log.Timber;

/**
* UI element overlaid on a map to show the user's location.
*/
Expand Down Expand Up @@ -148,6 +151,10 @@ private void init(Context context) {
compassListener = new CompassListener(context);
}

public void init(LocationSource locationSource) {
this.locationSource = locationSource;
}

public final void setForegroundDrawables(Drawable defaultDrawable, Drawable bearingDrawable) {
if (defaultDrawable == null) {
return;
Expand Down Expand Up @@ -391,9 +398,13 @@ public void setMapboxMap(MapboxMap mapboxMap) {

@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
Expand All @@ -414,39 +425,42 @@ public void onRestoreInstanceState(Parcelable state) {
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) {
userLocationListener = new GpsLocationListener(this, locationSource);
}

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() {
Expand Down Expand Up @@ -564,7 +578,10 @@ public void setContentPadding(int[] padding) {
}

public void setLocationSource(LocationEngine locationSource) {
toggleGps(false);
this.locationSource = locationSource;
this.userLocationListener = null;
setEnabled(isEnabled(), locationSource != null);
}

private static class GpsLocationListener implements LocationEngineListener {
Expand All @@ -580,10 +597,12 @@ private static class GpsLocationListener implements LocationEngineListener {
@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();
}
}
Expand Down Expand Up @@ -627,6 +646,9 @@ public void onPause() {
}

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;
}

Expand Down
Loading

0 comments on commit 9f88e37

Please sign in to comment.