From e561035e4f34fe16884332b16159d084f7ddc25f Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sat, 30 Mar 2013 20:42:04 +0100 Subject: [PATCH] Fix sensor listener --- .../plus/activities/DayNightHelper.java | 64 ++++++++++--------- .../osmand/plus/activities/MapActivity.java | 10 ++- .../plus/render/MapRenderRepositories.java | 6 +- .../net/osmand/plus/views/OsmandMapLayer.java | 11 ++-- .../osmand/plus/views/OsmandMapTileView.java | 26 +------- 5 files changed, 51 insertions(+), 66 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/DayNightHelper.java b/OsmAnd/src/net/osmand/plus/activities/DayNightHelper.java index caba39b031..731b3f164a 100644 --- a/OsmAnd/src/net/osmand/plus/activities/DayNightHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/DayNightHelper.java @@ -7,6 +7,7 @@ import java.util.TimeZone; import net.osmand.Location; import net.osmand.PlatformUtil; +import net.osmand.StateChangedListener; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings.CommonPreference; import net.osmand.plus.OsmandSettings.DayNightMode; @@ -51,73 +52,64 @@ public class DayNightHelper implements SensorEventListener { } private DayNightHelper listener; - private float lux = SensorManager.LIGHT_SUNLIGHT; - private long lastAutoCall = 0; - private Boolean lastAutoValue = null; + private long lastTime = 0; + private boolean lastNightMode = false; + private StateChangedListener sensorStateListener; /** * @return null if could not be determined (in case of error) * @return true if day is supposed to be */ - public Boolean getDayNightRenderer() { + public boolean isNightMode() { DayNightMode dayNightMode = pref.get(); if (dayNightMode.isDay()) { - return Boolean.TRUE; + return false; } else if (dayNightMode.isNight()) { - return Boolean.FALSE; + return true; } else // We are in auto mode! if (dayNightMode.isAuto()) { long currentTime = System.currentTimeMillis(); // allow recalculation each 60 seconds - if (currentTime - lastAutoCall > 60000) { - lastAutoCall = System.currentTimeMillis(); + if (currentTime - lastTime > 60000) { + lastTime = System.currentTimeMillis(); try { - SunriseSunset daynightSwitch = getSunriseSunset(); + SunriseSunset daynightSwitch = getSunriseSunset(); if (daynightSwitch != null) { boolean daytime = daynightSwitch.isDaytime(); log.debug("Sunrise/sunset setting to day: " + daytime); //$NON-NLS-1$ - lastAutoValue = Boolean.valueOf(daytime); + lastNightMode = !daytime; } - return lastAutoValue; } catch (IllegalArgumentException e) { log.warn("Network location provider not available"); //$NON-NLS-1$ } catch (SecurityException e) { log.warn("Missing permissions to get actual location!"); //$NON-NLS-1$ } - return null; - } else { - return lastAutoValue; } + return lastNightMode; } else if (dayNightMode.isSensor()) { - log.debug("lux value:" + lux + " setting to day: " + (lux > SensorManager.LIGHT_CLOUDY)); //$NON-NLS-1$ //$NON-NLS-2$ - return lux > SensorManager.LIGHT_CLOUDY ? Boolean.TRUE : Boolean.FALSE; + return lastNightMode; } - return null; + return false; } - public SunriseSunset getSunriseSunset(){ - Location lastKnownLocation = getLocation(); + public SunriseSunset getSunriseSunset() { + Location lastKnownLocation = osmandApplication.getLocationProvider().getLastKnownLocation(); + if(lastKnownLocation == null) { + lastKnownLocation = osmandApplication.getLocationProvider().getFirstTimeRunDefaultLocation(); + } if (lastKnownLocation == null) { return null; } double longitude = lastKnownLocation.getLongitude(); Date actualTime = new Date(); SunriseSunset daynightSwitch = new SunriseSunset(lastKnownLocation.getLatitude(), - longitude < 0 ? 360 + longitude : longitude, actualTime, - TimeZone.getDefault()); + longitude < 0 ? 360 + longitude : longitude, + actualTime, TimeZone.getDefault()); return daynightSwitch; } - private Location getLocation() { - Location lastKnownLocation = osmandApplication.getLocationProvider().getLastKnownLocation(); - if(lastKnownLocation == null) { - lastKnownLocation = osmandApplication.getLocationProvider().getFirstTimeRunDefaultLocation(); - } - return lastKnownLocation; - } - public void stopSensorIfNeeded() { if (listener != null) { SensorManager mSensorManager = (SensorManager) osmandApplication @@ -128,7 +120,8 @@ public class DayNightHelper implements SensorEventListener { } } - public void startSensorIfNeeded() { + public void startSensorIfNeeded(StateChangedListener sensorStateListener) { + this.sensorStateListener = sensorStateListener; DayNightMode dayNightMode = pref.get(); if (listener == null && dayNightMode.isSensor()) { SensorManager mSensorManager = (SensorManager) osmandApplication.getSystemService(Context.SENSOR_SERVICE); @@ -150,7 +143,16 @@ public class DayNightHelper implements SensorEventListener { @Override public void onSensorChanged(SensorEvent event) { if (event.values.length > 0) { - lux = event.values[0]; + float lux = event.values[0]; +// log.debug("lux value:" + lux + " setting to day: " + (lux > SensorManager.LIGHT_CLOUDY)); //$NON-NLS-1$ //$NON-NLS-2$ + boolean nightMode = lux > SensorManager.LIGHT_CLOUDY? false : true; + if(nightMode != lastNightMode) { + if(System.currentTimeMillis() - lastTime > 10000) { + lastTime = System.currentTimeMillis(); + lastNightMode = nightMode; + sensorStateListener.stateChanged(nightMode); + } + } } } } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index dd073a4d16..fd93fe1ea1 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -315,7 +315,6 @@ public class MapActivity extends AccessibleActivity { } OsmandPlugin.onMapActivityResume(this); - app.getDaynightHelper().startSensorIfNeeded(); mapView.refreshMap(true); } @@ -537,7 +536,14 @@ public class MapActivity extends AccessibleActivity { mapLayers.getMapInfoLayer().recreateControls(); } mapLayers.updateLayers(mapView); - app.getDaynightHelper().startSensorIfNeeded(); + app.getDaynightHelper().startSensorIfNeeded(new StateChangedListener() { + + @Override + public void stateChanged(Boolean change) { + getMapView().refreshMap(true); + } + }); + getMapView().refreshMap(true); } diff --git a/OsmAnd/src/net/osmand/plus/render/MapRenderRepositories.java b/OsmAnd/src/net/osmand/plus/render/MapRenderRepositories.java index 600793db0b..72f8265be1 100644 --- a/OsmAnd/src/net/osmand/plus/render/MapRenderRepositories.java +++ b/OsmAnd/src/net/osmand/plus/render/MapRenderRepositories.java @@ -176,7 +176,7 @@ public class MapRenderRepositories { if (requestedBox == null) { return true; } - if (drawSettings.isForce()) { + if (drawSettings.isUpdateVectorRendering()) { return true; } if (requestedBox.getZoom() != box.getZoom()) { @@ -449,9 +449,7 @@ public class MapRenderRepositories { try { // find selected rendering type OsmandApplication app = ((OsmandApplication) context.getApplicationContext()); - Boolean renderDay = app.getDaynightHelper().getDayNightRenderer(); - boolean nightMode = renderDay != null && !renderDay.booleanValue(); - + boolean nightMode = app.getDaynightHelper().isNightMode(); // boolean moreDetail = prefs.SHOW_MORE_MAP_DETAIL.get(); RenderingRulesStorage storage = app.getRendererRegistry().getCurrentSelectedRenderer(); RenderingRuleSearchRequest renderingReq = new RenderingRuleSearchRequest(storage); diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java index 853e3fbe9e..90e6b4605d 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java @@ -41,21 +41,20 @@ public abstract class OsmandMapLayer { public static class DrawSettings { private final boolean nightMode; - private final boolean force; + private final boolean updateVectorRendering; public DrawSettings(boolean nightMode) { this(nightMode,false); } - public DrawSettings(boolean nightMode, boolean force) { + public DrawSettings(boolean nightMode, boolean updateVectorRendering) { this.nightMode = nightMode; - this.force = force; + this.updateVectorRendering = updateVectorRendering; } - public boolean isForce() { - return force; + public boolean isUpdateVectorRendering() { + return updateVectorRendering; } - public boolean isNightMode() { return nightMode; } diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java index aa61948592..3178e1f405 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java @@ -487,7 +487,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall return settings; } - private void refreshMapInternal(boolean force) { + private void refreshMapInternal(boolean updateVectorRendering) { handler.removeMessages(1); // long time = System.currentTimeMillis(); @@ -509,13 +509,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall float h = getCenterPointY(); Canvas canvas = holder.lockCanvas(); if (canvas != null) { - boolean nightMode = false; - if (application != null) { - Boolean dayNightRenderer = application.getDaynightHelper().getDayNightRenderer(); - if (dayNightRenderer != null) { - nightMode = !dayNightRenderer.booleanValue(); - } - } + boolean nightMode = application.getDaynightHelper().isNightMode(); try { boundsRect.set(0, 0, getWidth(), getHeight()); calculateTileRectangle(boundsRect, w, h, tileX, tileY, tilesRect); @@ -528,21 +522,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall } else { canvas.drawARGB(255, 225, 225, 225); } - // TODO map -// float ftileSize = getTileSize(); -// int left = (int) FloatMath.floor(tilesRect.left); -// int top = (int) FloatMath.floor(tilesRect.top); -// int width = (int) FloatMath.ceil(tilesRect.right - left); -// int height = (int) FloatMath.ceil(tilesRect.bottom - top); -// for (int i = 0; i < width; i++) { -// for (int j = 0; j < height; j++) { -// float x1 = (i + left - tileX) * ftileSize + w; -// float y1 = (j + top - tileY) * ftileSize + h; -// drawEmptyTile(canvas, x1, y1, ftileSize, nightMode); -// } -// } - drawOverMap(canvas, latlonRect, tilesRect, new DrawSettings(nightMode,force)); - + drawOverMap(canvas, latlonRect, tilesRect, new DrawSettings(nightMode, updateVectorRendering)); // log.info("Draw with layers " + (System.currentTimeMillis() - time)); } finally { holder.unlockCanvasAndPost(canvas);