[android] #2421 - Refactoring LocationServices to be in PASSIVE mode when GPS is not active. Integrating TelemetryService with Core SDK for startup

This commit is contained in:
Brad Leege
2016-02-01 11:21:42 -06:00
parent 22209b75cd
commit 9436e4ff99
5 changed files with 49 additions and 24 deletions

View File

@@ -1,8 +1,12 @@
package com.mapbox.mapboxsdk.location;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.util.Log;
import com.mapbox.mapboxsdk.telemetry.TelemetryLocationReceiver;
import com.mapzen.android.lost.api.LocationRequest;
import com.mapzen.android.lost.api.LostApiClient;
import java.util.ArrayList;
@@ -10,8 +14,11 @@ import java.util.List;
public class LocationServices implements com.mapzen.android.lost.api.LocationListener {
private static final String TAG = "LocationServices";
private static LocationServices instance = null;
private Context context;
private LostApiClient mLocationClient;
private LocationRequest mLocationRequest;
@@ -26,6 +33,7 @@ public class LocationServices implements com.mapzen.android.lost.api.LocationLis
*/
private LocationServices(Context context) {
super();
this.context = context;
// Setup location services
mLocationClient = new LostApiClient.Builder(context).build();
locationListeners = new ArrayList<>();
@@ -53,21 +61,22 @@ public class LocationServices implements com.mapzen.android.lost.api.LocationLis
*/
public void toggleGPS(boolean enableGPS) {
// Disconnect
if (mLocationClient.isConnected()) {
// Disconnect first to ensure that the new requests are GPS
com.mapzen.android.lost.api.LocationServices.FusedLocationApi.removeLocationUpdates(this);
mLocationClient.disconnect();
}
// Setup Fresh
mLocationClient.connect();
Location lastLocation = com.mapzen.android.lost.api.LocationServices.FusedLocationApi.getLastLocation();
if (lastLocation != null) {
this.lastLocation = lastLocation;
}
if (enableGPS) {
if (mLocationClient.isConnected()) {
// Disconnect first to ensure that the new requests are GPS
com.mapzen.android.lost.api.LocationServices.FusedLocationApi.removeLocationUpdates(this);
mLocationClient.disconnect();
}
// Setup Fresh
mLocationClient.connect();
Location lastLocation = com.mapzen.android.lost.api.LocationServices.FusedLocationApi.getLastLocation();
if (lastLocation != null) {
this.lastLocation = lastLocation;
}
// LocationRequest Tuned for GPS
mLocationRequest = LocationRequest.create()
.setFastestInterval(1000)
@@ -78,12 +87,13 @@ public class LocationServices implements com.mapzen.android.lost.api.LocationLis
} else {
// Disconnect
if (mLocationClient.isConnected()) {
// Disconnect first to ensure that the new requests are GPS
com.mapzen.android.lost.api.LocationServices.FusedLocationApi.removeLocationUpdates(this);
mLocationClient.disconnect();
}
// LocationRequest Tuned for PASSIVE
mLocationRequest = LocationRequest.create()
.setFastestInterval(1000)
.setSmallestDisplacement(3.0f)
.setPriority(LocationRequest.PRIORITY_NO_POWER);
com.mapzen.android.lost.api.LocationServices.FusedLocationApi.requestLocationUpdates(mLocationRequest, this);
}
@@ -96,12 +106,18 @@ public class LocationServices implements com.mapzen.android.lost.api.LocationLis
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged()..." + location);
this.lastLocation = location;
// Update Listeners
for (LocationListener listener : this.locationListeners) {
listener.onLocationChanged(location);
}
// Update the Telemetry Receiver
Intent locIntent = new Intent(TelemetryLocationReceiver.INTENT_STRING);
locIntent.putExtra(LocationManager.KEY_LOCATION_CHANGED, location);
context.sendBroadcast(locIntent);
}
/**

View File

@@ -79,6 +79,7 @@ import com.mapbox.mapboxsdk.exceptions.TelemetryServiceNotConfiguredException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.layers.CustomLayer;
import com.mapbox.mapboxsdk.telemetry.TelemetryService;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import java.lang.annotation.Retention;
@@ -364,14 +365,17 @@ public class MapView extends FrameLayout {
mMapboxMap.setMyLocationTrackingMode(savedInstanceState.getInt(MapboxConstants.STATE_MY_LOCATION_TRACKING_MODE, MyLocationTracking.TRACKING_NONE));
//noinspection ResourceType
mMapboxMap.setMyBearingTrackingMode(savedInstanceState.getInt(MapboxConstants.STATE_MY_BEARING_TRACKING_MODE, MyBearingTracking.NONE));
} else {
// Force a check for Telemetry
validateTelemetryServiceConfigured();
Intent telemetryService = new Intent(getContext(), TelemetryService.class);
getContext().startService(telemetryService);
}
// Force a check for an access token
validateAccessToken(getAccessToken());
// Force a check for Telemetry
validateTelemetryServiceConfigured();
// Initialize EGL
mNativeMapView.initializeDisplay();
mNativeMapView.initializeContext();

View File

@@ -14,6 +14,8 @@ public class TelemetryLocationReceiver extends BroadcastReceiver {
private static final String TAG = "TelemLocationReceiver";
public static final String INTENT_STRING = "com.mapbox.mapboxsdk.telemetry.TelemetryLocationReceiver";
/**
* Default Constructor
*/

View File

@@ -55,7 +55,7 @@ public class TelemetryService extends Service {
super.onCreate();
// Enable Location Listening for lifecycle of app
IntentFilter filter = new IntentFilter("com.mapbox.mapboxsdk.telemetry.TelemetryLocationReceiver");
IntentFilter filter = new IntentFilter(TelemetryLocationReceiver.INTENT_STRING);
telemetryLocationReceiver = new TelemetryLocationReceiver();
registerReceiver(telemetryLocationReceiver, filter);
}
@@ -137,8 +137,10 @@ public class TelemetryService extends Service {
// Start Ambient Location Listening
// TODO
Intent locIntent = new Intent("com.mapbox.hermes.LocationReceiver");
/*
Intent locIntent = new Intent(TelemetryLocationReceiver.INTENT_STRING);
locationPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 14872, locIntent, PendingIntent.FLAG_CANCEL_CURRENT);
*/
try {
// Request Location Updates to be delivered to PendingIntent
// Currently Not supported in LOST

View File

@@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:name=".MapboxApplication"