From 18c04e398b6c654162bb689918fe398c98d3df5d Mon Sep 17 00:00:00 2001 From: Pavol Zibrita Date: Thu, 7 Apr 2011 13:48:13 +0200 Subject: [PATCH] Fixed issue 307 to work also on android 1.5 and above without problems. --- .../net/osmand/activities/DayNightHelper.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/activities/DayNightHelper.java b/OsmAnd/src/net/osmand/activities/DayNightHelper.java index b38831384b..1e0c1f2dbf 100644 --- a/OsmAnd/src/net/osmand/activities/DayNightHelper.java +++ b/OsmAnd/src/net/osmand/activities/DayNightHelper.java @@ -1,5 +1,6 @@ package net.osmand.activities; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.TimeZone; @@ -81,9 +82,7 @@ public class DayNightHelper implements SensorEventListener { if (currentTime - lastAutoCall > 5000) { lastAutoCall = System.currentTimeMillis(); try { - LocationManager locationProvider = (LocationManager) osmandApplication.getSystemService(Context.LOCATION_SERVICE); - // Or use LocationManager.GPS_PROVIDER - Location lastKnownLocation = locationProvider.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); + Location lastKnownLocation = getLocation(); if(lastKnownLocation == null){ return null; } @@ -111,6 +110,26 @@ public class DayNightHelper implements SensorEventListener { return null; } + private Location getLocation() { + Location lastKnownLocation = null; + LocationManager locationProvider = (LocationManager) osmandApplication.getSystemService(Context.LOCATION_SERVICE); + List providers = new ArrayList(locationProvider.getProviders(true)); + //note, passive provider is from API_LEVEL 8 but it is a constant, we can check for it. + int passiveFirst = providers.indexOf(LocationManager.PASSIVE_PROVIDER); + //put passive provider to first place + if (passiveFirst > -1) { + providers.add(0,providers.remove(passiveFirst)); + } + //find location + for (String provider : providers) { + lastKnownLocation = locationProvider.getLastKnownLocation(provider); + if (lastKnownLocation == null) { + break; + } + } + return lastKnownLocation; + } + public void onMapPause() { unregisterServiceListener(); }