diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location')
3 files changed, 171 insertions, 135 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationListener.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationListener.java index c385820423..f787085d2f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationListener.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationListener.java @@ -2,12 +2,15 @@ package com.mapbox.mapboxsdk.location; import android.location.Location; +/** + * Callback interface for when a location change occurs. + */ public interface LocationListener { /** - * Callback method for receiving location updates from LocationServices. + * Callback method for receiving location updates from LocationService. * @param location The new Location data */ - public void onLocationChanged(Location location); + void onLocationChanged(Location location); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationService.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationService.java new file mode 100644 index 0000000000..f459b5ad53 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationService.java @@ -0,0 +1,166 @@ +package com.mapbox.mapboxsdk.location; + +import android.Manifest; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.location.Location; +import android.location.LocationManager; +import android.support.annotation.NonNull; +import android.support.v4.content.ContextCompat; +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; +import java.util.List; + +public class LocationService implements com.mapzen.android.lost.api.LocationListener { + + private static final String TAG = "LocationService"; + + private static LocationService instance; + + private Context context; + private LostApiClient locationClient; + private Location lastLocation; + + private List<LocationListener> locationListeners; + + private boolean isGPSEnabled; + + /** + * Private constructor for singleton LocationService + */ + private LocationService(Context context) { + super(); + this.context = context; + // Setup location services + locationClient = new LostApiClient.Builder(context).build(); + locationListeners = new ArrayList<>(); + } + + /** + * Primary (singleton) access method for LocationService + * + * @param context Context + * @return LocationService + */ + public static LocationService getInstance(@NonNull final Context context) { + if (instance == null) { + instance = new LocationService(context.getApplicationContext()); + } + return instance; + } + + /** + * Enabled / Disable GPS focused location tracking + * + * @param enableGPS true if GPS is to be enabled, false if GPS is to be disabled + */ + public void toggleGPS(boolean enableGPS) { + if ((ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) && + (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { + Log.w(TAG, "Location Permissions Not Granted Yet. Try again after requesting."); + return; + } + + // Disconnect + if (locationClient.isConnected()) { + // Disconnect first to ensure that the new requests are GPS + com.mapzen.android.lost.api.LocationServices.FusedLocationApi.removeLocationUpdates(this); + locationClient.disconnect(); + } + + // Setup Fresh + locationClient.connect(); + Location lastLocation = com.mapzen.android.lost.api.LocationServices.FusedLocationApi.getLastLocation(); + if (lastLocation != null) { + this.lastLocation = lastLocation; + } + + LocationRequest locationRequest; + + if (enableGPS) { + // LocationRequest Tuned for GPS + locationRequest = LocationRequest.create() + .setFastestInterval(1000) + .setSmallestDisplacement(3.0f) + .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); + + com.mapzen.android.lost.api.LocationServices.FusedLocationApi.requestLocationUpdates(locationRequest, this); + } else { + // LocationRequest Tuned for PASSIVE + locationRequest = LocationRequest.create() + .setFastestInterval(1000) + .setSmallestDisplacement(3.0f) + .setPriority(LocationRequest.PRIORITY_NO_POWER); + + com.mapzen.android.lost.api.LocationServices.FusedLocationApi.requestLocationUpdates(locationRequest, this); + } + + isGPSEnabled = enableGPS; + } + + /** + * Returns if the GPS sensor is currently enabled + * + * @return active state of the GPS + */ + public boolean isGPSEnabled() { + return isGPSEnabled; + } + + /** + * Called when the location has changed. + * + * @param location The updated location + */ + @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); + } + + /** + * Last known location + * + * @return Last known location data + */ + public Location getLastLocation() { + return lastLocation; + } + + /** + * Registers a LocationListener to receive location updates + * + * @param locationListener LocationListener + */ + public void addLocationListener(@NonNull LocationListener locationListener) { + if (!this.locationListeners.contains(locationListener)) { + this.locationListeners.add(locationListener); + } + } + + /** + * Unregister a LocationListener to stop receiving location updates + * + * @param locationListener LocationListener to remove + * @return True if LocationListener was found and removed, False if it was not + */ + public boolean removeLocationListener(@NonNull LocationListener locationListener) { + return this.locationListeners.remove(locationListener); + } +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationServices.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationServices.java deleted file mode 100644 index ac2ab64076..0000000000 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationServices.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.mapbox.mapboxsdk.location; - -import android.content.Context; -import android.location.Location; -import android.support.annotation.NonNull; -import com.mapzen.android.lost.api.LocationRequest; -import com.mapzen.android.lost.api.LostApiClient; -import java.util.ArrayList; -import java.util.List; - -public class LocationServices implements com.mapzen.android.lost.api.LocationListener { - - private static LocationServices instance = null; - - private LostApiClient mLocationClient; - private LocationRequest mLocationRequest; - - private Location lastLocation = null; - - private List<LocationListener> locationListeners = null; - - private boolean isGPSEnabled = false; - - /** - * Private constructor for singleton LocationServices - */ - private LocationServices(Context context) { - super(); - // Setup location services - mLocationClient = new LostApiClient.Builder(context).build(); - locationListeners = new ArrayList<>(); - } - - /** - * Primary (singleton) access method for LocationServices - * @param context Context - * @return LocationServices - */ - public static LocationServices getLocationServices(@NonNull final Context context) { - if (instance == null) { - if (context == null) { - throw new NullPointerException("Context required for accessing LocationServices"); - } - instance = new LocationServices(context.getApplicationContext()); - } - return instance; - } - - /** - * Enabled / Disable GPS focused location tracking - * - * @param enableGPS true if GPS is to be enabled, false if GPS is to be disabled - */ - public void toggleGPS(boolean enableGPS) { - - 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) - .setSmallestDisplacement(3.0f) - .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); - - com.mapzen.android.lost.api.LocationServices.FusedLocationApi.requestLocationUpdates(mLocationRequest, this); - - } 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(); - } - - } - - isGPSEnabled = enableGPS; - } - - public boolean isGPSEnabled() { - return isGPSEnabled; - } - - @Override - public void onLocationChanged(Location location) { - this.lastLocation = location; - - // Update Listeners - for (LocationListener listener : this.locationListeners) { - listener.onLocationChanged(location); - } - } - - /** - * Last known location - * @return Last known location data - */ - public Location getLastLocation() { - return lastLocation; - } - - /** - * Registers a LocationListener to receive location updates - * @param locationListener LocationListener - */ - public void addLocationListener(@NonNull LocationListener locationListener) { - if(!this.locationListeners.contains(locationListener)){ - this.locationListeners.add(locationListener); - } - } - - /** - * Unregister a LocationListener to stop receiving location updates - * @param locationListener LocationListener to remove - * @return True if LocationListener was found and removed, False if it was not - */ - public boolean removeLocationListener(@NonNull LocationListener locationListener) { - return this.locationListeners.remove(locationListener); - } -} |