/*** Location Monitor *** *** monitors location and provides updates * * Copyright (c) 2014 * Randroid Solutions (http://www.randroidsolutions.com/) * All Rights Reserved. * * The right to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of this software, and/or to permit persons to whom the * software is furnished to do so is NOT PERMITTED without the written consent * of Randroid Solutions [dev@randroidsolutions.com]. * */ package com.randroid.library.gps.objects; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import com.randroid.library.gps.GlobalPosition; import com.randroid.library.gps.R; public abstract class LocationMonitor { protected boolean gps_enabled = false; protected boolean net_enabled = false; protected LocationResult mResult; private Context mContext; private LocationManager mManager; private Location gpsLocation = null; private Location netLocation = null; public interface LocationResult { public void update(final Location location); public void failure(final String error); public void unavailable(); } public LocationMonitor(final Context context, final LocationResult result) { mContext = context; mResult = result; if (mManager == null) { mManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); } //determine which location services are available try { gps_enabled = mManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (final Exception e) {} try { net_enabled = mManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (final Exception e) {} if (!gps_enabled && !net_enabled){ mResult.unavailable(); } else { //request updates from the enabled location services if (gps_enabled) { mManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, gpsListener); } if (net_enabled) { mManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, netListener); } } } public void cancelUpdates() { mManager.removeUpdates(gpsListener); mManager.removeUpdates(netListener); } protected abstract void locationChanged(final Location location); protected LocationListener gpsListener = new LocationListener() { //handle gps location updates public void onLocationChanged(final Location location) { locationChanged(location); gpsLocation = location; } public void onProviderDisabled(final String provider) { gps_enabled = false; if (!net_enabled) { cancelUpdates(); mResult.unavailable(); } } public void onProviderEnabled(final String provider) {} public void onStatusChanged(final String provider, int status, final Bundle extras) {} }; protected LocationListener netListener = new LocationListener() { //handle network location updates public void onLocationChanged(final Location location) { locationChanged(location); netLocation = location; } public void onProviderDisabled(final String provider) { net_enabled = false; if (!gps_enabled) { cancelUpdates(); mResult.unavailable(); } } public void onProviderEnabled(final String provider) {} public void onStatusChanged(final String provider, int status, final Bundle extras) {} }; protected Location getBestLocation() throws LocationException { Location result = null; //allow a location monitor that halts prematurely, such as a timeout, //to return the most accurate or last known location if ((gpsLocation != null) && (netLocation != null)) { if (gpsLocation.getAccuracy() < netLocation.getAccuracy()) { result = gpsLocation; } else { result = netLocation; } } else if (gpsLocation != null) { result = gpsLocation; } else if (netLocation != null) { result = netLocation; } if (result == null) { result = GlobalPosition.getLastLocation(mContext); } if (result == null) { throw new LocationException(mContext.getString(R.string.error_unavailable_location)); } else { return result; } } public class LocationException extends Exception { private static final long serialVersionUID = -6617766248520865577L; public LocationException(final String message) { super(message); } } }