1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.text.format.DateUtils;
20import android.util.FloatMath;
21
22/** @hide */
23public class TwilightCalculator {
24
25    /** Value of {@link #mState} if it is currently day */
26    public static final int DAY = 0;
27
28    /** Value of {@link #mState} if it is currently night */
29    public static final int NIGHT = 1;
30
31    private static final float DEGREES_TO_RADIANS = (float) (Math.PI / 180.0f);
32
33    // element for calculating solar transit.
34    private static final float J0 = 0.0009f;
35
36    // correction for civil twilight
37    private static final float ALTIDUTE_CORRECTION_CIVIL_TWILIGHT = -0.104719755f;
38
39    // coefficients for calculating Equation of Center.
40    private static final float C1 = 0.0334196f;
41    private static final float C2 = 0.000349066f;
42    private static final float C3 = 0.000005236f;
43
44    private static final float OBLIQUITY = 0.40927971f;
45
46    // Java time on Jan 1, 2000 12:00 UTC.
47    private static final long UTC_2000 = 946728000000L;
48
49    /**
50     * Time of sunset (civil twilight) in milliseconds or -1 in the case the day
51     * or night never ends.
52     */
53    public long mSunset;
54
55    /**
56     * Time of sunrise (civil twilight) in milliseconds or -1 in the case the
57     * day or night never ends.
58     */
59    public long mSunrise;
60
61    /** Current state */
62    public int mState;
63
64    /**
65     * calculates the civil twilight bases on time and geo-coordinates.
66     *
67     * @param time time in milliseconds.
68     * @param latiude latitude in degrees.
69     * @param longitude latitude in degrees.
70     */
71    public void calculateTwilight(long time, double latiude, double longitude) {
72        final float daysSince2000 = (float) (time - UTC_2000) / DateUtils.DAY_IN_MILLIS;
73
74        // mean anomaly
75        final float meanAnomaly = 6.240059968f + daysSince2000 * 0.01720197f;
76
77        // true anomaly
78        final float trueAnomaly = meanAnomaly + C1 * FloatMath.sin(meanAnomaly) + C2
79                * FloatMath.sin(2 * meanAnomaly) + C3 * FloatMath.sin(3 * meanAnomaly);
80
81        // ecliptic longitude
82        final float solarLng = trueAnomaly + 1.796593063f + (float) Math.PI;
83
84        // solar transit in days since 2000
85        final double arcLongitude = -longitude / 360;
86        float n = Math.round(daysSince2000 - J0 - arcLongitude);
87        double solarTransitJ2000 = n + J0 + arcLongitude + 0.0053f * FloatMath.sin(meanAnomaly)
88                + -0.0069f * FloatMath.sin(2 * solarLng);
89
90        // declination of sun
91        double solarDec = Math.asin(FloatMath.sin(solarLng) * FloatMath.sin(OBLIQUITY));
92
93        final double latRad = latiude * DEGREES_TO_RADIANS;
94
95        double cosHourAngle = (FloatMath.sin(ALTIDUTE_CORRECTION_CIVIL_TWILIGHT) - Math.sin(latRad)
96                * Math.sin(solarDec)) / (Math.cos(latRad) * Math.cos(solarDec));
97        // The day or night never ends for the given date and location, if this value is out of
98        // range.
99        if (cosHourAngle >= 1) {
100            mState = NIGHT;
101            mSunset = -1;
102            mSunrise = -1;
103            return;
104        } else if (cosHourAngle <= -1) {
105            mState = DAY;
106            mSunset = -1;
107            mSunrise = -1;
108            return;
109        }
110
111        float hourAngle = (float) (Math.acos(cosHourAngle) / (2 * Math.PI));
112
113        mSunset = Math.round((solarTransitJ2000 + hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;
114        mSunrise = Math.round((solarTransitJ2000 - hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;
115
116        if (mSunrise < time && mSunset > time) {
117            mState = DAY;
118        } else {
119            mState = NIGHT;
120        }
121    }
122
123}
124