use of net.osmand.plus.OsmandSettings.DayNightMode in project Osmand by osmandapp.
the class DayNightHelper method isNightMode.
/**
* @return null if could not be determined (in case of error)
* @return true if day is supposed to be
*/
public boolean isNightMode() {
DayNightMode dayNightMode = pref.get();
if (dayNightMode.isDay()) {
return false;
} else if (dayNightMode.isNight()) {
return true;
} else // We are in auto mode!
if (dayNightMode.isAuto()) {
long currentTime = System.currentTimeMillis();
// allow recalculation each 60 seconds
if (currentTime - lastTime > 60000) {
lastTime = System.currentTimeMillis();
try {
SunriseSunset daynightSwitch = getSunriseSunset();
if (daynightSwitch != null) {
boolean daytime = daynightSwitch.isDaytime();
// $NON-NLS-1$
log.debug("Sunrise/sunset setting to day: " + daytime);
lastNightMode = !daytime;
}
} catch (IllegalArgumentException e) {
// $NON-NLS-1$
log.warn("Network location provider not available");
} catch (SecurityException e) {
// $NON-NLS-1$
log.warn("Missing permissions to get actual location!");
}
}
return lastNightMode;
} else if (dayNightMode.isSensor()) {
return lastNightMode;
}
return false;
}
use of net.osmand.plus.OsmandSettings.DayNightMode in project Osmand by osmandapp.
the class DayNightHelper method startSensorIfNeeded.
public void startSensorIfNeeded(StateChangedListener<Boolean> sensorStateListener) {
this.sensorStateListener = sensorStateListener;
DayNightMode dayNightMode = pref.get();
if (listener == null && dayNightMode.isSensor()) {
SensorManager mSensorManager = (SensorManager) osmandApplication.getSystemService(Context.SENSOR_SERVICE);
Sensor mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
List<Sensor> list = mSensorManager.getSensorList(Sensor.TYPE_LIGHT);
// $NON-NLS-1$
log.info("Light sensors:" + list.size());
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
listener = this;
} else if (listener != null && !dayNightMode.isSensor()) {
stopSensorIfNeeded();
}
}
Aggregations