Skip to content

Instantly share code, notes, and snippets.

@BGR360
Created January 22, 2016 17:56
Show Gist options
  • Save BGR360/ecfa17a42b64cf34d7da to your computer and use it in GitHub Desktop.
Save BGR360/ecfa17a42b64cf34d7da to your computer and use it in GitHub Desktop.
Excerpt from DeliveryDispatcher
/**
* Begins the search to find the device's location. When it finishes or times out,
* it will invoke onLocationFound(Location) on all OnLocationFoundListeners
* in mLocationFoundListeners. If a listener cancels its request for a Location,
* but there are still others waiting, the search will continue.
*
* @param timeoutMillis The amount of time, in milliseconds,
* that we should search for a location
* @return True if we were able to include listener in a new or existing search, or false
* if we were unable to begin searching or if listener was already listening
*/
public static boolean beginFindLocation(long timeoutMillis, OnLocationFoundListener listener)
{
mLock.lock();
boolean listenerSatisfied = false;
Log.d(LOG_TAG, "Somebody wants to know our location...");
// If we are already in the middle of a search, simply add listener to our list.
// It will later be notified once our ongoing search is complete.
if(mIsSearching)
{
if(addListener(listener))
{
Log.d(LOG_TAG, "Added listener to the list.");
listenerSatisfied = true;
}
else
{
Log.d(LOG_TAG, "Listener was already listening!");
}
}
else
{
// We were not searching, so start a new search.
Log.d(LOG_TAG, "Beginning find location...");
if(isLocationServicesAvailable())
{
// As long as Location Services are available, we can check
// to see which location providers are available for use.
boolean networkIsAvailable = mLocationManager.isProviderEnabled(
LocationManager.NETWORK_PROVIDER);
boolean passiveIsAvailable = mLocationManager.isProviderEnabled(
LocationManager.PASSIVE_PROVIDER);
boolean gpsIsAvailable = mLocationManager.isProviderEnabled(
LocationManager.GPS_PROVIDER);
String available = networkIsAvailable ? "available" : "not available";
Log.d(LOG_TAG, "Network provider is " + available + ".");
available = passiveIsAvailable ? "available" : "not available";
Log.d(LOG_TAG, "Passive provider is " + available + ".");
available = gpsIsAvailable ? "available" : "not available";
Log.d(LOG_TAG, "GPS provider is " + available + ".");
// We can begin the search if any provider is available
if(networkIsAvailable || passiveIsAvailable || gpsIsAvailable)
{
// Add listener as the first listener in our list
addListener(listener);
// Begin the search by subscribing to location updates
// LocationManager.requestLocationUpdates() takes three parameters:
// 1: the provider
// 2: how often you want to receive updates (millis)
// 3: minimum required change in distance to require an update (meters)
// 4: a LocationListener
Log.d(LOG_TAG, "Attaching to location updates...");
// Attach to location updates from the best provider
// Prefer network over passive and passive over gps
if(networkIsAvailable)
{
Log.d(LOG_TAG, "Using Network provider.");
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, mLocationChangedListener);
}
else if (passiveIsAvailable)
{
Log.d(LOG_TAG, "Using Passive provider.");
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, mLocationChangedListener);
}
else if (gpsIsAvailable)
{
Log.d(LOG_TAG, "Using GPS provider.");
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, mLocationChangedListener);
}
// Start the timeout timer
mStopTimer = new Timer();
mStopTimer.schedule(new TimerTask()
{
@Override
public void run()
{
// Stop searching after timeoutMillis, regardless of
// whether or not we have found the location
Log.d(LOG_TAG, "Search timed out!");
mLock.requestEndFindLocation(null);
}
}, timeoutMillis);
mIsSearching = true;
listenerSatisfied = true;
}
else
{
// No location provider available
Log.w(LOG_TAG, "Unable to begin finding location: " +
"No location provider available.");
}
}
else
{
// Location Services unavailable
Log.w(LOG_TAG, "Unable to begin finding location: " +
"Location Services are unavailable.");
}
}
mLock.unlock();
return listenerSatisfied;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment