Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6fd5e0ac authored by Bernd Holzhey's avatar Bernd Holzhey
Browse files

Fix calculation of the next twilight update for locations where the day or night never ends.

parent e5a1d4a2
Loading
Loading
Loading
Loading
+27 −18
Original line number Diff line number Diff line
@@ -416,7 +416,7 @@ class DockObserver extends UEventObserver {
                        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                                LOCATION_UPDATE_MS, LOCATION_UPDATE_DISTANCE_METER, mLocationListener);
                        retrieveLocation();
                        if (mLocation != null) {
                        if (mCarModeEnabled && mLocation != null && mNightMode == MODE_NIGHT_AUTO) {
                            try {
                                DockObserver.this.updateTwilight();
                            } catch (RemoteException e) {
@@ -462,8 +462,8 @@ class DockObserver extends UEventObserver {
            if (location == null) {
                Time currentTime = new Time();
                currentTime.set(System.currentTimeMillis());
                double lngOffset = FACTOR_GMT_OFFSET_LONGITUDE * currentTime.gmtoff
                        - (currentTime.isDst > 0 ? 3600 : 0);
                double lngOffset = FACTOR_GMT_OFFSET_LONGITUDE *
                        (currentTime.gmtoff - (currentTime.isDst > 0 ? 3600 : 0));
                location = new Location("fake");
                location.setLongitude(lngOffset);
                location.setLatitude(59.95);
@@ -587,9 +587,14 @@ class DockObserver extends UEventObserver {
            }

            // schedule next update
            long nextUpdate = 0;
            if (tw.mSunrise == -1 || tw.mSunset == -1) {
                // In the case the day or night never ends the update is scheduled 12 hours later.
                nextUpdate = currentTime + 12 * DateUtils.HOUR_IN_MILLIS;
            } else {
                final int mLastTwilightState = tw.mState;
                // add some extra time to be on the save side.
            long nextUpdate = DateUtils.MINUTE_IN_MILLIS;
                nextUpdate += DateUtils.MINUTE_IN_MILLIS;
                if (currentTime > tw.mSunset) {
                    // next update should be on the following day
                    tw.calculateTwilight(currentTime
@@ -602,6 +607,7 @@ class DockObserver extends UEventObserver {
                } else {
                    nextUpdate += tw.mSunset;
                }
            }

            Intent updateIntent = new Intent(ACTION_UPDATE_NIGHT_MODE);
            PendingIntent pendingIntent =
@@ -609,10 +615,13 @@ class DockObserver extends UEventObserver {
            mAlarmManager.cancel(pendingIntent);
            mAlarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdate, pendingIntent);

            // set current mode
            // Make sure that we really set the new mode only if we're in car mode and
            // automatic switching is enables.
            if (mCarModeEnabled && mNightMode == MODE_NIGHT_AUTO) {
                setMode(Configuration.UI_MODE_TYPE_CAR, nightMode << 4);
            }
        }
    }

    /**
     * Check which of two locations is better by comparing the distance a device
+26 −6
Original line number Diff line number Diff line
@@ -46,10 +46,16 @@ public class TwilightCalculator {
    // Java time on Jan 1, 2000 12:00 UTC.
    private static final long UTC_2000 = 946728000000L;

    /** Time of sunset (civil twilight) in milliseconds. */
    /**
     * Time of sunset (civil twilight) in milliseconds or -1 in the case the day
     * or night never ends.
     */
    public long mSunset;

    /** Time of sunrise (civil twilight) in milliseconds. */
    /**
     * Time of sunrise (civil twilight) in milliseconds or -1 in the case the
     * day or night never ends.
     */
    public long mSunrise;

    /** Current state */
@@ -85,10 +91,24 @@ public class TwilightCalculator {
        double solarDec = Math.asin(FloatMath.sin(solarLng) * FloatMath.sin(OBLIQUITY));

        final double latRad = latiude * DEGREES_TO_RADIANS;
        float hourAngle = (float) (Math
                .acos((FloatMath.sin(ALTIDUTE_CORRECTION_CIVIL_TWILIGHT) - Math.sin(latRad)
                        * Math.sin(solarDec))
                        / (Math.cos(latRad) * Math.cos(solarDec))) / (2 * Math.PI));

        double cosHourAngle = (FloatMath.sin(ALTIDUTE_CORRECTION_CIVIL_TWILIGHT) - Math.sin(latRad)
                * Math.sin(solarDec)) / (Math.cos(latRad) * Math.cos(solarDec));
        // The day or night never ends for the given date and location, if this value is out of
        // range.
        if (cosHourAngle >= 1) {
            mState = NIGHT;
            mSunset = -1;
            mSunrise = -1;
            return;
        } else if (cosHourAngle <= -1) {
            mState = DAY;
            mSunset = -1;
            mSunrise = -1;
            return;
        }

        float hourAngle = (float) (Math.acos(cosHourAngle) / (2 * Math.PI));

        mSunset = Math.round((solarTransitJ2000 + hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;
        mSunrise = Math.round((solarTransitJ2000 - hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;