Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract the location engine from a specific provider #269

Merged
merged 16 commits into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Mapbox welcomes participation and contributions from everyone.

### v2.0.0

* `libjava` and `libandroid` now live under the same (Android Studio) project. These projects are now split up into: `libjava-core`, `libjava-geojson`, `libjava-services`, `libjava-services-rx`, `libandroid-telemetry`, `libandroid-services`, `libandroid-ui`, independently published to Maven to reduce method count on Android apps.
* New `LocationEngine` that can be implemented by different location providers (open source or proprietary), decoupled from other components so that it can be used for telemetry, user tracking (map), navigation, or mocking use-cases. Sample implementations for Lost (`LostLocationEngine`), Google Play Services (`GoogleLocationEngine`) and Mock provider (`MockLocationEngine`).
* New `PermissionsManager` and `PermissionsListener` that replaces the old `PermissionsUtils` to be more flexible checking for any kind of permission, not just location, at runtime.

### v1.3.2

* Geocoding: added poi.landmark type
Expand Down
7 changes: 4 additions & 3 deletions mapbox/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ dependencies {
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'

// LOST
compile ('com.mapzen.android:lost:1.1.1') {
exclude group: 'com.google.guava'
}
compile 'com.mapzen.android:lost:2.1.2'

// Google Play Services
compile 'com.google.android.gms:play-services-location:10.0.1'

// Picasso (Static Image)
compile 'com.squareup.picasso:picasso:2.5.2'
Expand Down
7 changes: 7 additions & 0 deletions mapbox/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".location.LocationEngineActivity"
android:label="@string/title_location">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
<activity
android:name=".directions.DirectionsV5Activity"
android:label="@string/title_directions_v5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.mapbox.services.android.BuildConfig;
import com.mapbox.services.android.telemetry.permissions.PermissionsListener;
import com.mapbox.services.android.telemetry.permissions.PermissionsManager;
import com.mapbox.services.android.testapp.directions.DirectionsV5Activity;
import com.mapbox.services.android.testapp.directions.RouteUtilsV5Activity;
import com.mapbox.services.android.testapp.distance.DistanceActivity;
Expand All @@ -21,6 +24,7 @@
import com.mapbox.services.android.testapp.geocoding.GeocodingWidgetActivity;
import com.mapbox.services.android.testapp.icons.DirectionsIconsActivity;
import com.mapbox.services.android.testapp.icons.MakiIconsActivity;
import com.mapbox.services.android.testapp.location.LocationEngineActivity;
import com.mapbox.services.android.testapp.nav.OffRouteDetectionActivity;
import com.mapbox.services.android.testapp.staticimage.StaticImageActivity;
import com.mapbox.services.android.testapp.turf.TurfBearingActivity;
Expand All @@ -31,28 +35,32 @@
import com.mapbox.services.android.testapp.turf.TurfMidpointActivity;
import com.mapbox.services.android.testapp.utils.MapMatchingActivity;
import com.mapbox.services.android.testapp.utils.SimplifyPolylineActivity;
import com.mapbox.services.android.utils.PermissionsUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* This activity shows how to use PermissionsUtils to request location permissions
* This activity shows how to use PermissionsManager to request location permissions
* from the user. It loads all the sample activities using a RecyclerView.
*/
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements PermissionsListener {

private static final String LOG_TAG = "MainActivity";

private RecyclerView recyclerView;
private PermissionsManager permissionsManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final List<SampleItem> samples = new ArrayList<>(Arrays.asList(
new SampleItem(
getString(R.string.title_location),
getString(R.string.description_location),
LocationEngineActivity.class),
new SampleItem(
getString(R.string.title_distance),
getString(R.string.description_distance),
Expand Down Expand Up @@ -162,18 +170,31 @@ protected void onCreate(Bundle savedInstanceState) {
recyclerView.setAdapter(adapter);

// Check for location permission
if (!PermissionsUtils.isLocationGranted(this)) {
permissionsManager = new PermissionsManager(this);
if (!permissionsManager.areLocationPermissionsGranted(this)) {
recyclerView.setEnabled(false);
PermissionsUtils.startPermissionFlow(this);
permissionsManager.requestLocationPermissions(this);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (PermissionsUtils.isRequestSuccessful(requestCode, permissions, grantResults)) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
Toast.makeText(this, "This app needs location permissions in order to show its functionality.",
Toast.LENGTH_LONG).show();
}

@Override
public void onPermissionResult(boolean granted) {
if (granted) {
recyclerView.setEnabled(true);
} else {
PermissionsUtils.explainFallback(this);
Toast.makeText(this, "You didn't grant location permissions.",
Toast.LENGTH_LONG).show();
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ protected void onStart() {
private void getLastLocation() {
// Gets the best and most recent location currently available, which may be null
// in rare cases when a location is not available.
lastLocation = LocationServices.FusedLocationApi.getLastLocation();
//noinspection MissingPermission
lastLocation = LocationServices.FusedLocationApi.getLastLocation(lostApiClient);
if (lastLocation != null) {
// Determine whether a Geocoder is available.
if (!AndroidGeocoder.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;

public class GeocodingWidgetActivity extends AppCompatActivity {

Expand Down Expand Up @@ -65,13 +66,27 @@ public void onMapReady(MapboxMap mapboxMapReady) {
LocationRequest request = LocationRequest.create()
.setInterval(5000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(request, new LocationListener() {
LostApiClient lostApiClient = new LostApiClient.Builder(this).build();

//noinspection MissingPermission
LocationServices.FusedLocationApi.requestLocationUpdates(lostApiClient, request, new LocationListener() {

@Override
public void onLocationChanged(Location location) {
Log.d(LOG_TAG, "New LOST location: " + location.toString());
autocomplete.setProximity(Position.fromCoordinates(
location.getLongitude(), location.getLatitude()));
}

@Override
public void onProviderDisabled(String provider) {
Log.d(LOG_TAG, "onProviderDisabled: " + provider);
}

@Override
public void onProviderEnabled(String provider) {
Log.d(LOG_TAG, "onProviderEnabled: " + provider);
}
});
}

Expand Down
Loading