-
Notifications
You must be signed in to change notification settings - Fork 320
Add FusedLocationEngine to Test App #1373
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2cf9e6
An alternative location engine that provides more stable location upd…
kevinkreiser d1d27bb
remove location engine deactivation from on pause and bump mapbox-nav…
9b114ba
overwrite location timestamps using the system clock. this value is c…
kevinkreiser b75c9c6
Revert "overwrite location timestamps using the system clock. this va…
kevinkreiser 08fe965
overwrite location timestamps using the system clock. this value is c…
kevinkreiser ea9bbfe
use location result get locations method for new location updates and…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...box/services/android/navigation/testapp/activity/location/ForwardingLocationCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.mapbox.services.android.navigation.testapp.activity.location; | ||
|
|
||
| import android.location.Location; | ||
|
|
||
| import com.google.android.gms.location.LocationCallback; | ||
| import com.google.android.gms.location.LocationResult; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| class ForwardingLocationCallback extends LocationCallback { | ||
|
|
||
| private static final int FIRST = 0; | ||
| private final FusedLocationEngine locationEngine; | ||
|
|
||
| ForwardingLocationCallback(FusedLocationEngine locationEngine) { | ||
| this.locationEngine = locationEngine; | ||
| } | ||
|
|
||
| @Override | ||
| public void onLocationResult(LocationResult locationResult) { | ||
| List<Location> locations = locationResult.getLocations(); | ||
| boolean hasLocation = !locations.isEmpty(); | ||
| if (hasLocation) { | ||
| Location newLocation = locations.get(FIRST); | ||
| locationEngine.notifyListenersOnLocationChanged(newLocation); | ||
| } | ||
| } | ||
| } |
137 changes: 137 additions & 0 deletions
137
...com/mapbox/services/android/navigation/testapp/activity/location/FusedLocationEngine.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package com.mapbox.services.android.navigation.testapp.activity.location; | ||
|
|
||
| import android.content.Context; | ||
| import android.location.Location; | ||
| import android.os.Looper; | ||
| import android.support.annotation.NonNull; | ||
|
|
||
| import com.google.android.gms.common.api.ApiException; | ||
| import com.google.android.gms.location.FusedLocationProviderClient; | ||
| import com.google.android.gms.location.LocationRequest; | ||
| import com.google.android.gms.location.LocationServices; | ||
| import com.google.android.gms.location.LocationSettingsRequest; | ||
| import com.google.android.gms.location.LocationSettingsStatusCodes; | ||
| import com.google.android.gms.location.SettingsClient; | ||
| import com.mapbox.android.core.location.LocationEngine; | ||
| import com.mapbox.android.core.location.LocationEngineListener; | ||
|
|
||
| import timber.log.Timber; | ||
|
|
||
| public class FusedLocationEngine extends LocationEngine { | ||
|
|
||
| private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000; | ||
| private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 500; | ||
| private FusedLocationProviderClient fusedLocationClient; | ||
| private SettingsClient settingsClient; | ||
| private LocationRequest locationRequest; | ||
| private LocationSettingsRequest locationSettingsRequest; | ||
| private boolean isConnected = false; | ||
| private boolean receivingUpdates = false; | ||
| private Location lastLocation = null; | ||
| private final ForwardingLocationCallback forwardingCallback = new ForwardingLocationCallback(this); | ||
|
|
||
| public FusedLocationEngine(@NonNull Context context) { | ||
| super(); | ||
| fusedLocationClient = LocationServices.getFusedLocationProviderClient(context); | ||
| settingsClient = LocationServices.getSettingsClient(context); | ||
| } | ||
|
|
||
| @Override | ||
| public void activate() { | ||
| if (!isConnected) { | ||
| startLocationUpdates(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void deactivate() { | ||
| stopLocationUpdates(); | ||
| isConnected = false; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isConnected() { | ||
| return isConnected; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("MissingPermission") | ||
| public Location getLastLocation() { | ||
| fusedLocationClient.getLastLocation().addOnSuccessListener(location -> lastLocation = location); | ||
| return lastLocation; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings( {"MissingPermission"}) | ||
| public void requestLocationUpdates() { | ||
| if (isConnected && !receivingUpdates) { | ||
| fusedLocationClient.requestLocationUpdates(locationRequest, forwardingCallback, Looper.myLooper()) | ||
| .addOnSuccessListener(aVoid -> receivingUpdates = true); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void removeLocationUpdates() { | ||
| stopLocationUpdates(); | ||
| } | ||
|
|
||
| @Override | ||
| public Type obtainType() { | ||
| return Type.GOOGLE_PLAY_SERVICES; | ||
| } | ||
|
|
||
| void notifyListenersOnLocationChanged(Location location) { | ||
| for (LocationEngineListener listener : locationListeners) { | ||
| listener.onLocationChanged(location); | ||
| } | ||
| } | ||
|
|
||
| private void createLocationRequest() { | ||
| locationRequest = new LocationRequest(); | ||
| //TODO: unhardcode these | ||
| locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); | ||
| locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS); | ||
| locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | ||
| } | ||
|
|
||
| private void buildLocationSettingsRequest() { | ||
| LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); | ||
| builder.addLocationRequest(locationRequest); | ||
| locationSettingsRequest = builder.build(); | ||
| } | ||
|
|
||
| private void startLocationUpdates() { | ||
| createLocationRequest(); | ||
| buildLocationSettingsRequest(); | ||
| settingsClient.checkLocationSettings(locationSettingsRequest) | ||
| .addOnSuccessListener(locationSettingsResponse -> { | ||
| isConnected = true; | ||
| notifyListenersOnConnected(); | ||
| }) | ||
| .addOnFailureListener(exception -> { | ||
| isConnected = false; | ||
| int statusCode = ((ApiException) exception).getStatusCode(); | ||
| switch (statusCode) { | ||
| case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: | ||
| Timber.e("Location settings are not satisfied. Upgrade location settings"); | ||
| break; | ||
| case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: | ||
| Timber.e("Location settings are inadequate, and cannot be fixed here. Fix in Settings."); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void notifyListenersOnConnected() { | ||
| for (LocationEngineListener listener : locationListeners) { | ||
| listener.onConnected(); | ||
| } | ||
| } | ||
|
|
||
| private void stopLocationUpdates() { | ||
| fusedLocationClient.removeLocationUpdates(forwardingCallback) | ||
| .addOnCompleteListener(task -> receivingUpdates = false); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setInterval,setFastestIntervalandsetPriorityare hardcoded. We should use setLocationEnginevalues instead.