diff options
author | Pablo Guardiola <guardiola31337@gmail.com> | 2017-11-01 18:25:52 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-01 18:25:52 -0700 |
commit | 29a94ae939cf1d81017251ec374e3fff703dc4a6 (patch) | |
tree | 63ef44754744e76e81e5b849ffd7fd931444c10d | |
parent | d0d401b007f460337dd9834ce529f34834ee4a60 (diff) | |
download | qtlocation-mapboxgl-29a94ae939cf1d81017251ec374e3fff703dc4a6.tar.gz |
[android] make location provider optional (#9488) (#10354)
16 files changed, 280 insertions, 117 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/build.gradle b/platform/android/MapboxGLAndroidSDK/build.gradle index 19587d0078..49806780b5 100644 --- a/platform/android/MapboxGLAndroidSDK/build.gradle +++ b/platform/android/MapboxGLAndroidSDK/build.gradle @@ -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 @@ -22,6 +21,8 @@ dependencies { transitive = true exclude group: 'com.android.support' } + + compile(rootProject.ext.dep.mapboxAndroidCore) } android { diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/EmptyLocationSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/EmptyLocationSource.java new file mode 100644 index 0000000000..d24ed5a113 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/EmptyLocationSource.java @@ -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; + } +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java index 7fd9d6172d..e575dc9062 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java @@ -12,9 +12,10 @@ import com.mapbox.mapboxsdk.constants.MapboxConstants; 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; @@ -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. @@ -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 { @@ -66,10 +68,10 @@ public final class Mapbox { 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; } /** @@ -143,7 +145,24 @@ public final class Mapbox { 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; } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationSource.java index 6652142be3..5129db08e8 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationSource.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationSource.java @@ -4,16 +4,17 @@ import android.content.Context; 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) @@ -27,8 +28,11 @@ import com.mapzen.android.lost.api.LostApiClient; * 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; @@ -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() { } /** @@ -50,12 +63,9 @@ public class LocationSource extends LocationEngine implements LocationListener { */ @Override public void activate() { - if (!lostApiClient.isConnected()) { + if (lostApiClient != null && !lostApiClient.isConnected()) { lostApiClient.connect(); } - for (LocationEngineListener listener : locationListeners) { - listener.onConnected(); - } } /** @@ -65,7 +75,7 @@ public class LocationSource extends LocationEngine implements LocationListener { */ @Override public void deactivate() { - if (lostApiClient.isConnected()) { + if (lostApiClient != null && lostApiClient.isConnected()) { lostApiClient.disconnect(); } } @@ -82,6 +92,24 @@ public class LocationSource extends LocationEngine implements LocationListener { } /** + * 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. * * @return the last known location @@ -89,9 +117,9 @@ public class LocationSource extends LocationEngine implements LocationListener { @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; } @@ -101,13 +129,18 @@ public class LocationSource extends LocationEngine implements LocationListener { */ @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) { @@ -118,9 +151,9 @@ public class LocationSource extends LocationEngine implements LocationListener { 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); } } @@ -130,11 +163,21 @@ public class LocationSource extends LocationEngine implements LocationListener { @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. * * @param location the new location @@ -145,4 +188,4 @@ public class LocationSource extends LocationEngine implements LocationListener { listener.onLocationChanged(location); } } -} +}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/package-info.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/package-info.java index 6b0c9a76c0..b27559e95e 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/package-info.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/package-info.java @@ -1,4 +1,4 @@ /** * Contains the Mapbox Maps Android Location API classes. */ -package com.mapbox.mapboxsdk.location; +package com.mapbox.mapboxsdk.location;
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 6bf8342efb..f0461b6307 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -42,7 +42,7 @@ import com.mapbox.mapboxsdk.style.layers.Filter; 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; diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/TrackingSettings.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/TrackingSettings.java index 6881ca067b..2f85c82587 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/TrackingSettings.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/TrackingSettings.java @@ -12,8 +12,8 @@ import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.constants.MyBearingTracking; import com.mapbox.mapboxsdk.constants.MyLocationTracking; import com.mapbox.mapboxsdk.maps.widgets.MyLocationView; -import com.mapbox.services.android.telemetry.location.LocationEngine; -import com.mapbox.services.android.telemetry.location.LocationEngineListener; +import com.mapbox.services.android.core.location.LocationEngine; +import com.mapbox.services.android.core.location.LocationEngineListener; import com.mapbox.services.android.telemetry.permissions.PermissionsManager; import timber.log.Timber; @@ -52,7 +52,7 @@ public final class TrackingSettings { } void initialise(MapboxMapOptions options) { - locationSource = Mapbox.getLocationSource(); + locationSource = Mapbox.getLocationEngine(); setMyLocationEnabled(options.getLocationEnabled()); } @@ -392,7 +392,7 @@ public final class TrackingSettings { this.isCustomLocationSource = locationSource != null; if (locationSource == null) { - locationSource = Mapbox.getLocationSource(); + locationSource = Mapbox.getLocationEngine(); } this.locationSource = locationSource; myLocationView.setLocationSource(locationSource); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java index 8a393ebb1f..cc7602e056 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java @@ -36,9 +36,9 @@ import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.location.LocationSource; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.Projection; -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.core.location.LocationEngine; +import com.mapbox.services.android.core.location.LocationEngineListener; +import com.mapbox.services.android.core.location.LocationEnginePriority; import java.lang.ref.WeakReference; @@ -70,7 +70,7 @@ public class MyLocationView extends View { private LatLng latLng; private Location location; - private LocationEngine locationSource; + private LocationEngine locationEngine; private long locationUpdateTimestamp; private float previousDirection; @@ -161,8 +161,9 @@ public class MyLocationView extends View { compassListener = new CompassListener(context); } + @Deprecated public void init(LocationSource locationSource) { - this.locationSource = locationSource; + this.locationEngine = locationSource; } /** @@ -448,8 +449,8 @@ public class MyLocationView extends View { } if (userLocationListener != null) { - locationSource.removeLocationEngineListener(userLocationListener); - locationSource = null; + locationEngine.removeLocationEngineListener(userLocationListener); + locationEngine = null; userLocationListener = null; } } @@ -485,12 +486,12 @@ public class MyLocationView extends View { * Set the enabled state, for internal use only. * * @param enabled The value to set the state to - * @param isCustomLocationSource Flag handling for handling user provided custom location source + * @param isCustomLocationEngine Flag handling for handling user provided custom location engine */ - public void setEnabled(boolean enabled, boolean isCustomLocationSource) { + public void setEnabled(boolean enabled, boolean isCustomLocationEngine) { super.setEnabled(enabled); setVisibility(enabled ? View.VISIBLE : View.INVISIBLE); - toggleGps(enabled, isCustomLocationSource); + toggleGps(enabled, isCustomLocationEngine); } /** @@ -530,32 +531,33 @@ public class MyLocationView extends View { * * @param enableGps true if GPS is to be enabled, false if GPS is to be disabled */ - private void toggleGps(boolean enableGps, boolean isCustomLocationSource) { + private void toggleGps(boolean enableGps, boolean isCustomLocationEngine) { if (enableGps) { - if (locationSource == null) { - if (!isCustomLocationSource) { - locationSource = Mapbox.getLocationSource(); + if (locationEngine == null) { + if (!isCustomLocationEngine) { + locationEngine = Mapbox.getLocationEngine(); } else { return; } } if (userLocationListener == null) { - userLocationListener = new GpsLocationListener(this, locationSource); + userLocationListener = new GpsLocationListener(this, locationEngine); } - locationSource.addLocationEngineListener(userLocationListener); - locationSource.setPriority(LocationEnginePriority.HIGH_ACCURACY); - locationSource.activate(); + locationEngine.addLocationEngineListener(userLocationListener); + locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY); + locationEngine.activate(); } else { - if (locationSource == null) { + if (locationEngine == null) { return; } // Disable location and user dot location = null; - locationSource.removeLocationEngineListener(userLocationListener); - locationSource.removeLocationUpdates(); - locationSource.deactivate(); + locationEngine.removeLocationEngineListener(userLocationListener); + locationEngine.removeLocationUpdates(); + locationEngine.deactivate(); + restoreLocationEngine(); } } @@ -740,13 +742,13 @@ public class MyLocationView extends View { /** * Set the location source from which location updates are received, for internal use only. * - * @param locationSource The location source to receive updates from + * @param locationEngine The location engine to receive updates from */ - public void setLocationSource(LocationEngine locationSource) { + public void setLocationSource(LocationEngine locationEngine) { toggleGps(false); - this.locationSource = locationSource; + this.locationEngine = locationEngine; this.userLocationListener = null; - setEnabled(isEnabled(), locationSource != null); + setEnabled(isEnabled(), locationEngine != null); } private void applyDrawableTint(Drawable drawable, @ColorInt int color) { @@ -769,6 +771,11 @@ public class MyLocationView extends View { } } + private void restoreLocationEngine() { + locationEngine.setPriority(LocationEnginePriority.LOW_POWER); + locationEngine.activate(); + } + private static class GpsLocationListener implements LocationEngineListener { private WeakReference<MyLocationView> userLocationView; diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java index e05190cd57..09acf3feef 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java @@ -5,7 +5,7 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException; -import com.mapbox.mapboxsdk.location.LocationSource; +import com.mapbox.services.android.core.location.LocationEngine; import org.junit.Before; import org.junit.Test; @@ -25,13 +25,13 @@ public class MapboxTest { private Context context; private Context appContext; - private LocationSource locationSource; + private LocationEngine locationSource; @Before public void before() { context = mock(Context.class); appContext = mock(Context.class); - locationSource = mock(LocationSource.class); + locationSource = mock(LocationEngine.class); when(context.getApplicationContext()).thenReturn(appContext); } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/build.gradle b/platform/android/MapboxGLAndroidSDKTestApp/build.gradle index ad910ddf10..f45ad3dc3a 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/build.gradle +++ b/platform/android/MapboxGLAndroidSDKTestApp/build.gradle @@ -69,6 +69,7 @@ dependencies { compile(rootProject.ext.dep.mapboxJavaServices) { transitive = true } + compile rootProject.ext.dep.lost // Testing dependencies androidTestCompile rootProject.ext.dep.supportAnnotations diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/widgets/MyLocationViewTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/widgets/MyLocationViewTest.java index ec7105c321..cf58ba50a6 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/widgets/MyLocationViewTest.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/widgets/MyLocationViewTest.java @@ -106,7 +106,7 @@ public class MyLocationViewTest extends BaseActivityTest { mapboxMap.moveCamera( CameraUpdateFactory.newCameraPosition( new CameraPosition.Builder() - .target(new LatLng(Mapbox.getLocationSource().getLastLocation())) + .target(new LatLng(Mapbox.getLocationEngine().getLastLocation())) .build() ) ); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/CustomLocationEngineActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/CustomLocationEngineActivity.java index e954b73f8d..626f7c372d 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/CustomLocationEngineActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/CustomLocationEngineActivity.java @@ -68,7 +68,7 @@ public class CustomLocationEngineActivity extends BaseLocationActivity { if (mapboxMap != null) { int itemId = item.getItemId(); if (itemId == R.id.action_id_location_source_lost) { - mapboxMap.setLocationSource(Mapbox.getLocationSource()); + mapboxMap.setLocationSource(Mapbox.getLocationEngine()); return true; } else if (itemId == R.id.action_id_location_source_mock) { mapboxMap.setLocationSource(MockLocationEngine.getInstance()); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MockLocationEngine.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MockLocationEngine.java index da3c78b07a..bfc04dc1e6 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MockLocationEngine.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MockLocationEngine.java @@ -5,8 +5,8 @@ import android.animation.TypeEvaluator; import android.animation.ValueAnimator; import android.location.Location; -import com.mapbox.services.android.telemetry.location.LocationEngine; -import com.mapbox.services.android.telemetry.location.LocationEngineListener; +import com.mapbox.services.android.core.location.LocationEngine; +import com.mapbox.services.android.core.location.LocationEngineListener; import timber.log.Timber; @@ -87,6 +87,11 @@ public class MockLocationEngine extends LocationEngine { } } + @Override + public Type obtainType() { + return Type.MOCK; + } + private static class LocationAnimator extends AnimatorListenerAdapter { private static final long DURATION_ANIMATION = 10000; diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationDrawableActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationDrawableActivity.java index 9f6f2b2fcd..2b681f6983 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationDrawableActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationDrawableActivity.java @@ -16,19 +16,15 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.MapboxMapOptions; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mapbox.mapboxsdk.testapp.R; -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; +import com.mapbox.services.android.core.location.LocationEngineListener; /** * Test activity showcasing how to change the MyLocationView drawable. */ -public class MyLocationDrawableActivity extends BaseLocationActivity implements LocationListener { +public class MyLocationDrawableActivity extends BaseLocationActivity implements LocationEngineListener { private MapView mapView; private MapboxMap mapboxMap; - private LostApiClient lostApiClient; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -66,15 +62,11 @@ public class MyLocationDrawableActivity extends BaseLocationActivity implements @Override protected void enableLocation(boolean enabled) { mapboxMap.setMyLocationEnabled(enabled); - if (lostApiClient == null) { - lostApiClient = new LostApiClient.Builder(this).build(); - lostApiClient.connect(); - LocationRequest request = LocationRequest.create() - .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) - .setInterval(5000) - .setSmallestDisplacement(10); - LocationServices.FusedLocationApi.requestLocationUpdates(request, this); - } + } + + @Override + public void onConnected() { + // Nothing } @Override @@ -104,10 +96,6 @@ public class MyLocationDrawableActivity extends BaseLocationActivity implements protected void onStop() { super.onStop(); mapView.onStop(); - if (lostApiClient.isConnected()) { - LocationServices.FusedLocationApi.removeLocationUpdates(this); - lostApiClient.disconnect(); - } } @Override diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTrackingModeActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTrackingModeActivity.java index 786aeb8733..726d13a516 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTrackingModeActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTrackingModeActivity.java @@ -14,6 +14,7 @@ import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; +import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.constants.MyBearingTracking; import com.mapbox.mapboxsdk.constants.MyLocationTracking; @@ -24,10 +25,7 @@ import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mapbox.mapboxsdk.maps.TrackingSettings; import com.mapbox.mapboxsdk.maps.UiSettings; import com.mapbox.mapboxsdk.testapp.R; -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; +import com.mapbox.services.android.core.location.LocationEngineListener; import timber.log.Timber; @@ -39,10 +37,7 @@ import timber.log.Timber; * </p> */ public class MyLocationTrackingModeActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, - OnMapReadyCallback, LocationListener { - - // Testing for user defined LostApiClient - private LostApiClient lostApiClient; + OnMapReadyCallback, LocationEngineListener { public static final int TRACKING_NONE_INDEX = 0; public static final int TRACKING_FOLLOW_INDEX = 1; @@ -75,18 +70,15 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements @Override public void onMapReady(MapboxMap mapboxMap) { MyLocationTrackingModeActivity.this.mapboxMap = mapboxMap; - lostApiClient = new LostApiClient.Builder(this).build(); - lostApiClient.connect(); - LocationRequest request = LocationRequest.create() - .setPriority(LocationRequest.PRIORITY_LOW_POWER) - .setInterval(5000) - .setSmallestDisplacement(10); - - Location location = LocationServices.FusedLocationApi.getLastLocation(); - if (location != null) { - setInitialLocation(location, 15); - } - LocationServices.FusedLocationApi.requestLocationUpdates(request, this); + + mapboxMap.setMyLocationEnabled(true); + Mapbox.getLocationEngine().addLocationEngineListener(this); + Mapbox.getLocationEngine().requestLocationUpdates(); + } + + @Override + public void onConnected() { + // Nothing } @Override @@ -227,10 +219,8 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements @Override protected void onStop() { super.onStop(); - if (lostApiClient.isConnected()) { - LocationServices.FusedLocationApi.removeLocationUpdates(this); - lostApiClient.disconnect(); - } + Mapbox.getLocationEngine().removeLocationEngineListener(this); + Mapbox.getLocationEngine().removeLocationUpdates(); mapView.onStop(); } diff --git a/platform/android/dependencies.gradle b/platform/android/dependencies.gradle index 2c43eafd3b..47a534fac3 100644 --- a/platform/android/dependencies.gradle +++ b/platform/android/dependencies.gradle @@ -7,7 +7,8 @@ ext { versionCode = 11 versionName = "5.0.0" - mapboxServicesVersion = "2.2.3" + mapboxServicesVersion = "2.2.7" + mapboxAndroidCoreVersion = "0.1.1" supportLibVersion = "25.4.0" espressoVersion = '3.0.1' testRunnerVersion = '1.0.1' @@ -18,9 +19,10 @@ ext { mapboxJavaServices : "com.mapbox.mapboxsdk:mapbox-java-services:${mapboxServicesVersion}@jar", mapboxJavaGeoJSON : "com.mapbox.mapboxsdk:mapbox-java-geojson:${mapboxServicesVersion}@jar", mapboxAndroidTelemetry : "com.mapbox.mapboxsdk:mapbox-android-telemetry:${mapboxServicesVersion}@aar", + mapboxAndroidCore : "com.mapbox.mapboxsdk:mapbox-android-core:${mapboxAndroidCoreVersion}@aar", // mapzen lost - lost : 'com.mapzen.android:lost:1.1.1', + lost : 'com.mapzen.android:lost:3.0.4', // unit test junit : 'junit:junit:4.12', |