Search in sources :

Example 1 with DayNightMode

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;
}
Also used : SunriseSunset(net.osmand.util.SunriseSunset) DayNightMode(net.osmand.plus.OsmandSettings.DayNightMode)

Example 2 with DayNightMode

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();
    }
}
Also used : SensorManager(android.hardware.SensorManager) DayNightMode(net.osmand.plus.OsmandSettings.DayNightMode) Sensor(android.hardware.Sensor)

Aggregations

DayNightMode (net.osmand.plus.OsmandSettings.DayNightMode)2 Sensor (android.hardware.Sensor)1 SensorManager (android.hardware.SensorManager)1 SunriseSunset (net.osmand.util.SunriseSunset)1