Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.text.format.DateUtils; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 20 | |
| 21 | /** @hide */ |
| 22 | public class TwilightCalculator { |
| 23 | |
| 24 | /** Value of {@link #mState} if it is currently day */ |
| 25 | public static final int DAY = 0; |
| 26 | |
| 27 | /** Value of {@link #mState} if it is currently night */ |
| 28 | public static final int NIGHT = 1; |
| 29 | |
| 30 | private static final float DEGREES_TO_RADIANS = (float) (Math.PI / 180.0f); |
| 31 | |
| 32 | // element for calculating solar transit. |
| 33 | private static final float J0 = 0.0009f; |
| 34 | |
| 35 | // correction for civil twilight |
| 36 | private static final float ALTIDUTE_CORRECTION_CIVIL_TWILIGHT = -0.104719755f; |
| 37 | |
| 38 | // coefficients for calculating Equation of Center. |
| 39 | private static final float C1 = 0.0334196f; |
| 40 | private static final float C2 = 0.000349066f; |
| 41 | private static final float C3 = 0.000005236f; |
| 42 | |
| 43 | private static final float OBLIQUITY = 0.40927971f; |
| 44 | |
| 45 | // Java time on Jan 1, 2000 12:00 UTC. |
| 46 | private static final long UTC_2000 = 946728000000L; |
| 47 | |
Bernd Holzhey | 6fd5e0a | 2010-02-18 11:19:56 +0100 | [diff] [blame] | 48 | /** |
| 49 | * Time of sunset (civil twilight) in milliseconds or -1 in the case the day |
| 50 | * or night never ends. |
| 51 | */ |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 52 | public long mSunset; |
| 53 | |
Bernd Holzhey | 6fd5e0a | 2010-02-18 11:19:56 +0100 | [diff] [blame] | 54 | /** |
| 55 | * Time of sunrise (civil twilight) in milliseconds or -1 in the case the |
| 56 | * day or night never ends. |
| 57 | */ |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 58 | public long mSunrise; |
| 59 | |
| 60 | /** Current state */ |
| 61 | public int mState; |
| 62 | |
| 63 | /** |
| 64 | * calculates the civil twilight bases on time and geo-coordinates. |
| 65 | * |
| 66 | * @param time time in milliseconds. |
| 67 | * @param latiude latitude in degrees. |
| 68 | * @param longitude latitude in degrees. |
| 69 | */ |
| 70 | public void calculateTwilight(long time, double latiude, double longitude) { |
| 71 | final float daysSince2000 = (float) (time - UTC_2000) / DateUtils.DAY_IN_MILLIS; |
| 72 | |
| 73 | // mean anomaly |
| 74 | final float meanAnomaly = 6.240059968f + daysSince2000 * 0.01720197f; |
| 75 | |
| 76 | // true anomaly |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 77 | final double trueAnomaly = meanAnomaly + C1 * Math.sin(meanAnomaly) + C2 |
| 78 | * Math.sin(2 * meanAnomaly) + C3 * Math.sin(3 * meanAnomaly); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 79 | |
| 80 | // ecliptic longitude |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 81 | final double solarLng = trueAnomaly + 1.796593063d + Math.PI; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 82 | |
| 83 | // solar transit in days since 2000 |
| 84 | final double arcLongitude = -longitude / 360; |
| 85 | float n = Math.round(daysSince2000 - J0 - arcLongitude); |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 86 | double solarTransitJ2000 = n + J0 + arcLongitude + 0.0053d * Math.sin(meanAnomaly) |
| 87 | + -0.0069d * Math.sin(2 * solarLng); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 88 | |
| 89 | // declination of sun |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 90 | double solarDec = Math.asin(Math.sin(solarLng) * Math.sin(OBLIQUITY)); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 91 | |
| 92 | final double latRad = latiude * DEGREES_TO_RADIANS; |
Bernd Holzhey | 6fd5e0a | 2010-02-18 11:19:56 +0100 | [diff] [blame] | 93 | |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 94 | double cosHourAngle = (Math.sin(ALTIDUTE_CORRECTION_CIVIL_TWILIGHT) - Math.sin(latRad) |
Bernd Holzhey | 6fd5e0a | 2010-02-18 11:19:56 +0100 | [diff] [blame] | 95 | * Math.sin(solarDec)) / (Math.cos(latRad) * Math.cos(solarDec)); |
| 96 | // The day or night never ends for the given date and location, if this value is out of |
| 97 | // range. |
| 98 | if (cosHourAngle >= 1) { |
| 99 | mState = NIGHT; |
| 100 | mSunset = -1; |
| 101 | mSunrise = -1; |
| 102 | return; |
| 103 | } else if (cosHourAngle <= -1) { |
| 104 | mState = DAY; |
| 105 | mSunset = -1; |
| 106 | mSunrise = -1; |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | float hourAngle = (float) (Math.acos(cosHourAngle) / (2 * Math.PI)); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 111 | |
| 112 | mSunset = Math.round((solarTransitJ2000 + hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000; |
| 113 | mSunrise = Math.round((solarTransitJ2000 - hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000; |
| 114 | |
| 115 | if (mSunrise < time && mSunset > time) { |
| 116 | mState = DAY; |
| 117 | } else { |
| 118 | mState = NIGHT; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | } |