package org.osmdroid.util;

import org.osmdroid.util.constants.UtilConstants;

import android.location.Location;
import android.location.LocationManager;

public class LocationUtils implements UtilConstants {

	/**
	 * This is a utility class with only static members.
	 */
	private LocationUtils() {
	}

	/**
	 * Get the most recent location from the GPS or Network provider.
	 * @param pLocationManager
	 * @return return the most recent location, or null if there's no known location
	 */
	public static Location getLastKnownLocation(final LocationManager pLocationManager) {
		if (pLocationManager == null) {
			return null;
		}
		final Location gpsLocation =
			pLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		final Location networkLocation =
			pLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		if (gpsLocation == null) {
			return networkLocation;
		} else if (networkLocation == null) {
			return gpsLocation;
		} else {
			// both are non-null - use the most recent
			if (networkLocation.getTime() > gpsLocation.getTime() + GPS_WAIT_TIME) {
				return networkLocation;
			} else {
				return gpsLocation;
			}
		}
	}

}

@@ -394,34 +394,10 @@
 %0A%09 *
- @param pLocationManager
 %0D%0A
+
 %09 * 
@@ -656,38 +656,17 @@
 cation =
-%0D%0A%09%09%09pLocationManager.
+ 
 getLastK
@@ -670,32 +670,50 @@
 stKnownLocation(
+pLocationManager, 
 LocationManager.
@@ -766,30 +766,9 @@
 on =
-%0D%0A%09%09%09pLocationManager.
+ 
 getL
@@ -784,16 +784,34 @@
 ocation(
+pLocationManager, 
 Location
@@ -1170,16 +1170,16 @@
 %0D%0A%09%09%09%7D%0D%0A
-
 %09%09%7D%0D%0A%09%7D%0D
@@ -1181,11 +1181,269 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%09private static Location getLastKnownLocation(final LocationManager pLocationManager, final String pProvider) %7B%0D%0A%09%09if (!pLocationManager.isProviderEnabled(pProvider)) %7B%0D%0A%09%09%09return null;%0D%0A%09%09%7D%0D%0A%09%09return pLocationManager.getLastKnownLocation(pProvider);%0D%0A%09%7D%0D%0A%0D%0A
 %7D%0D%0A

Check for provider enabled before returning last known location. Fixes issue 472.