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

Commit

Permalink
[android] make location provider optional (#9488) (#10354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guardiola31337 committed Nov 2, 2017
1 parent d0d401b commit 29a94ae
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 117 deletions.
5 changes: 3 additions & 2 deletions platform/android/MapboxGLAndroidSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ dependencies {
compile rootProject.ext.dep.supportFragmentV4
compile rootProject.ext.dep.timber
compile rootProject.ext.dep.okhttp3
compile(rootProject.ext.dep.lost) {
exclude group: 'com.google.guava'
provided(rootProject.ext.dep.lost) {
exclude group: 'com.android.support'
}
testCompile rootProject.ext.dep.junit
Expand All @@ -22,6 +21,8 @@ dependencies {
transitive = true
exclude group: 'com.android.support'
}

compile(rootProject.ext.dep.mapboxAndroidCore)
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.mapbox.mapboxsdk;


import android.location.Location;
import android.support.annotation.Nullable;

import com.mapbox.mapboxsdk.location.LocationSource;
import com.mapbox.services.android.core.location.LocationEngine;
import com.mapbox.services.android.core.location.LocationEngineListener;

class EmptyLocationSource extends LocationSource {

/**
* 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() {
// Intentionally left empty
}

/**
* Disconnect the location engine which is useful when you no longer need location updates or requesting the users
* {@link LocationEngine#getLastLocation()}. Before deactivating, you'll need to stop request user location updates
* using {@link LocationEngine#removeLocationUpdates()}.
*/
@Override
public void deactivate() {
// Intentionally left empty
}

/**
* Check if your location provider has been activated/connected. This is mainly used internally but is also useful in
* the rare case when you'd like to know if your location engine is connected or not.
*
* @return boolean true if the location engine has been activated/connected, else false.
*/
@Override
public boolean isConnected() {
return false;
}

/**
* Returns the Last known location is the location provider is connected and location permissions are granted.
*
* @return the last known location
*/
@Override
@Nullable
public Location getLastLocation() {
return null;
}

/**
* Request location updates to the location provider.
*/
@Override
public void requestLocationUpdates() {
// Intentionally left empty
}

/**
* Dismiss ongoing location update to the location provider.
*/
@Override
public void removeLocationUpdates() {
// Intentionally left empty
}

/**
* Invoked when the Location has changed.
*
* @param location the new location
*/
@Override
public void onLocationChanged(Location location) {
// Intentionally left empty
}

/**
* Useful when you'd like to add a location listener to handle location connections and update events. It is important
* to note, that the callback will continue getting called even when your application isn't in the foreground.
* Therefore, it is a good idea to use {@link LocationEngine#removeLocationEngineListener(LocationEngineListener)}
* inside your activities {@code onStop()} method.
*
* @param listener A {@link LocationEngineListener} which you'd like to add to your location engine.
* @since 2.0.0
*/
@Override
public void addLocationEngineListener(LocationEngineListener listener) {
// Intentionally left empty
}

/**
* If you no longer need your {@link LocationEngineListener} to be invoked with every location update, use this
* method to remove it. It's also important to remove your listeners before the activity is destroyed to prevent any
* potential memory leaks.
*
* @param listener the {@link LocationEngineListener} you'd like to remove from this {@link LocationEngine}.
* @return true.
* @since 2.0.0
*/
@Override
public boolean removeLocationEngineListener(LocationEngineListener listener) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;
import com.mapbox.mapboxsdk.location.LocationSource;
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;
import com.mapbox.services.android.core.location.LocationEngine;
import com.mapbox.services.android.core.location.LocationEnginePriority;
import com.mapbox.services.android.core.location.LocationEngineProvider;
import com.mapbox.services.android.telemetry.MapboxTelemetry;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEnginePriority;

import timber.log.Timber;

Expand All @@ -34,7 +35,7 @@ public final class Mapbox {
private Context context;
private String accessToken;
private Boolean connected;
private LocationSource locationSource;
private LocationEngine locationEngine;

/**
* Get an instance of Mapbox.
Expand All @@ -50,8 +51,9 @@ 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, new LocationSource(appContext));
LocationEngine locationEngine = new LocationSource(appContext);
LocationEngineProvider locationEngineProvider = new LocationEngineProvider(context);
LocationEngine locationEngine = locationEngineProvider.obtainBestLocationEngineAvailable();
INSTANCE = new Mapbox(appContext, accessToken, locationEngine);
locationEngine.setPriority(LocationEnginePriority.NO_POWER);

try {
Expand All @@ -66,10 +68,10 @@ public static synchronized Mapbox getInstance(@NonNull Context context, @NonNull
return INSTANCE;
}

Mapbox(@NonNull Context context, @NonNull String accessToken, LocationSource locationSource) {
Mapbox(@NonNull Context context, @NonNull String accessToken, LocationEngine locationEngine) {
this.context = context;
this.accessToken = accessToken;
this.locationSource = locationSource;
this.locationEngine = locationEngine;
}

/**
Expand Down Expand Up @@ -143,7 +145,24 @@ public static synchronized Boolean isConnected() {
return (activeNetwork != null && activeNetwork.isConnected());
}

/**
* Returns a location source instance with empty methods.
*
* @return an empty location source implementation
* @deprecated Replaced by {@link Mapbox#getLocationEngine()}
*/
@Deprecated
public static LocationSource getLocationSource() {
return INSTANCE.locationSource;
return new EmptyLocationSource();
}


/**
* Returns the location engine used by the SDK.
*
* @return the location engine configured
*/
public static LocationEngine getLocationEngine() {
return INSTANCE.locationEngine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
import android.location.Location;
import android.support.annotation.Nullable;

import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEngineListener;
import com.mapbox.services.android.telemetry.location.LocationEnginePriority;
import com.mapbox.services.android.telemetry.permissions.PermissionsManager;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.services.android.core.location.LocationEngine;
import com.mapbox.services.android.core.location.LocationEngineListener;
import com.mapbox.services.android.core.location.LocationEnginePriority;
import com.mapzen.android.lost.api.LocationListener;
import com.mapzen.android.lost.api.LocationRequest;
import com.mapzen.android.lost.api.LocationServices;
import com.mapzen.android.lost.api.LostApiClient;

/**
* LocationEngine using the Open Source Lost library
* Manages locational updates. Contains methods to register and unregister location listeners.
* <ul>
* <li>You can register a LocationEngineListener with LocationSource#addLocationEngineListener(LocationEngineListener)
Expand All @@ -27,8 +28,11 @@
* overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back
* in the history stack.
* </p>
*
* @deprecated Use a {@link Mapbox#getLocationEngine()} instead.
*/
public class LocationSource extends LocationEngine implements LocationListener {
@Deprecated
public class LocationSource extends LocationEngine implements LostApiClient.ConnectionCallbacks, LocationListener {

private Context context;
private LostApiClient lostApiClient;
Expand All @@ -41,7 +45,16 @@ public class LocationSource extends LocationEngine implements LocationListener {
public LocationSource(Context context) {
super();
this.context = context.getApplicationContext();
lostApiClient = new LostApiClient.Builder(this.context).build();
lostApiClient = new LostApiClient.Builder(this.context)
.addConnectionCallbacks(this)
.build();
}

/**
* Constructs a location source instance.
* Needed to create empty location source implementations.
*/
public LocationSource() {
}

/**
Expand All @@ -50,12 +63,9 @@ public LocationSource(Context context) {
*/
@Override
public void activate() {
if (!lostApiClient.isConnected()) {
if (lostApiClient != null && !lostApiClient.isConnected()) {
lostApiClient.connect();
}
for (LocationEngineListener listener : locationListeners) {
listener.onConnected();
}
}

/**
Expand All @@ -65,7 +75,7 @@ public void activate() {
*/
@Override
public void deactivate() {
if (lostApiClient.isConnected()) {
if (lostApiClient != null && lostApiClient.isConnected()) {
lostApiClient.disconnect();
}
}
Expand All @@ -81,6 +91,24 @@ public boolean isConnected() {
return lostApiClient.isConnected();
}

/**
* Invoked when the location provider has connected.
*/
@Override
public void onConnected() {
for (LocationEngineListener listener : locationListeners) {
listener.onConnected();
}
}

/**
* Invoked when the location provider connection has been suspended.
*/
@Override
public void onConnectionSuspended() {
// Intentionally left empty
}

/**
* Returns the Last known location is the location provider is connected and location permissions are granted.
*
Expand All @@ -89,9 +117,9 @@ public boolean isConnected() {
@Override
@Nullable
public Location getLastLocation() {
if (lostApiClient.isConnected() && PermissionsManager.areLocationPermissionsGranted(context)) {
if (lostApiClient.isConnected()) {
//noinspection MissingPermission
return LocationServices.FusedLocationApi.getLastLocation();
return LocationServices.FusedLocationApi.getLastLocation(lostApiClient);
}
return null;
}
Expand All @@ -101,13 +129,18 @@ public Location getLastLocation() {
*/
@Override
public void requestLocationUpdates() {
// Common params
LocationRequest request = LocationRequest.create()
.setInterval(1000)
.setFastestInterval(1000)
.setSmallestDisplacement(3.0f);
LocationRequest request = LocationRequest.create();

if (interval != null) {
request.setInterval(interval);
}
if (fastestInterval != null) {
request.setFastestInterval(fastestInterval);
}
if (smallestDisplacement != null) {
request.setSmallestDisplacement(smallestDisplacement);
}

// Priority matching is straightforward
if (priority == LocationEnginePriority.NO_POWER) {
request.setPriority(LocationRequest.PRIORITY_NO_POWER);
} else if (priority == LocationEnginePriority.LOW_POWER) {
Expand All @@ -118,9 +151,9 @@ public void requestLocationUpdates() {
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

if (lostApiClient.isConnected() && PermissionsManager.areLocationPermissionsGranted(context)) {
if (lostApiClient.isConnected()) {
//noinspection MissingPermission
LocationServices.FusedLocationApi.requestLocationUpdates(request, this);
LocationServices.FusedLocationApi.requestLocationUpdates(lostApiClient, request, this);
}
}

Expand All @@ -130,10 +163,20 @@ public void requestLocationUpdates() {
@Override
public void removeLocationUpdates() {
if (lostApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(this);
LocationServices.FusedLocationApi.removeLocationUpdates(lostApiClient, this);
}
}

/**
* Returns the location engine type.
*
* @return Lost type
*/
@Override
public Type obtainType() {
return Type.LOST;
}

/**
* Invoked when the Location has changed.
*
Expand All @@ -145,4 +188,4 @@ public void onLocationChanged(Location location) {
listener.onLocationChanged(location);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Contains the Mapbox Maps Android Location API classes.
*/
package com.mapbox.mapboxsdk.location;
package com.mapbox.mapboxsdk.location;
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.light.Light;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.core.location.LocationEngine;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.Geometry;

Expand Down
Loading

0 comments on commit 29a94ae

Please sign in to comment.