From a3735f4c86bcdfc9a2944f3c3f74d751771b8742 Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Tue, 7 May 2013 15:19:58 -0700 Subject: [PATCH] Bug 828563 - Return the most recent location, not the most accurate provider's most recent location. r=blassey --- mobile/android/base/GeckoAppShell.java | 52 ++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/mobile/android/base/GeckoAppShell.java b/mobile/android/base/GeckoAppShell.java index 1f95926996d..66672c107ff 100644 --- a/mobile/android/base/GeckoAppShell.java +++ b/mobile/android/base/GeckoAppShell.java @@ -149,7 +149,7 @@ public class GeckoAppShell private static Sensor gProximitySensor = null; private static Sensor gLightSensor = null; - private static boolean mLocationHighAccuracy = false; + private static volatile boolean mLocationHighAccuracy; public static ActivityHandlerHelper sActivityHelper = new ActivityHandlerHelper(); static NotificationClient sNotificationClient; @@ -393,14 +393,52 @@ public class GeckoAppShell } } + private static float getLocationAccuracy(Location location) { + float radius = location.getAccuracy(); + return (location.hasAccuracy() && radius > 0) ? radius : 1001; + } + + private static Location getLastKnownLocation() { + Location lastKnownLocation = null; + LocationManager lm = getLocationManager(); + List providers = lm.getAllProviders(); + + for (String provider : providers) { + Location location = lm.getLastKnownLocation(provider); + if (location == null) { + continue; + } + + if (lastKnownLocation == null) { + lastKnownLocation = location; + continue; + } + + long timeDiff = location.getElapsedRealtimeNanos() + - lastKnownLocation.getElapsedRealtimeNanos(); + + if (timeDiff > 0 || + (timeDiff == 0 && + getLocationAccuracy(location) < getLocationAccuracy(lastKnownLocation))) { + lastKnownLocation = location; + } + } + + return lastKnownLocation; + } + public static void enableLocation(final boolean enable) { ThreadUtils.postToUiThread(new Runnable() { @Override public void run() { - LocationManager lm = (LocationManager) - GeckoApp.mAppContext.getSystemService(Context.LOCATION_SERVICE); + LocationManager lm = getLocationManager(); if (enable) { + Location lastKnownLocation = getLastKnownLocation(); + if (lastKnownLocation != null) { + GeckoApp.mAppContext.onLocationChanged(lastKnownLocation); + } + Criteria criteria = new Criteria(); criteria.setSpeedRequired(false); criteria.setBearingRequired(false); @@ -420,10 +458,6 @@ public class GeckoAppShell return; Looper l = Looper.getMainLooper(); - Location loc = lm.getLastKnownLocation(provider); - if (loc != null) { - GeckoApp.mAppContext.onLocationChanged(loc); - } lm.requestLocationUpdates(provider, 100, (float).5, GeckoApp.mAppContext, l); } else { lm.removeUpdates(GeckoApp.mAppContext); @@ -432,6 +466,10 @@ public class GeckoAppShell }); } + private static LocationManager getLocationManager() { + return (LocationManager) GeckoApp.mAppContext.getSystemService(Context.LOCATION_SERVICE); + } + public static void enableLocationHighAccuracy(final boolean enable) { mLocationHighAccuracy = enable; }