1 /* 2 * Copyright (C) 2015 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 androidx.appcompat.app; 18 19 import android.text.format.DateUtils; 20 21 /** 22 * Imported from frameworks/base/services/core/java/com/android/server/TwilightCalculator.java 23 * 24 * <p>Calculates the sunrise and sunsets times for a given location.</p> 25 */ 26 class TwilightCalculator { 27 28 private static TwilightCalculator sInstance; 29 30 static TwilightCalculator getInstance() { 31 if (sInstance == null) { 32 sInstance = new TwilightCalculator(); 33 } 34 return sInstance; 35 } 36 37 /** Value of {@link #state} if it is currently day */ 38 public static final int DAY = 0; 39 40 /** Value of {@link #state} if it is currently night */ 41 public static final int NIGHT = 1; 42 43 private static final float DEGREES_TO_RADIANS = (float) (Math.PI / 180.0f); 44 45 // element for calculating solar transit. 46 private static final float J0 = 0.0009f; 47 48 // correction for civil twilight 49 @SuppressWarnings("FloatingPointLiteralPrecision") 50 private static final float ALTIDUTE_CORRECTION_CIVIL_TWILIGHT = -0.104719755f; 51 52 // coefficients for calculating Equation of Center. 53 private static final float C1 = 0.0334196f; 54 private static final float C2 = 0.000349066f; 55 private static final float C3 = 0.000005236f; 56 57 @SuppressWarnings("FloatingPointLiteralPrecision") 58 private static final float OBLIQUITY = 0.40927971f; 59 60 // Java time on Jan 1, 2000 12:00 UTC. 61 private static final long UTC_2000 = 946728000000L; 62 63 /** 64 * Time of sunset (civil twilight) in milliseconds or -1 in the case the day 65 * or night never ends. 66 */ 67 public long sunset; 68 69 /** 70 * Time of sunrise (civil twilight) in milliseconds or -1 in the case the 71 * day or night never ends. 72 */ 73 public long sunrise; 74 75 /** 76 * Current state 77 */ 78 public int state; 79 80 /** 81 * calculates the civil twilight bases on time and geo-coordinates. 82 * 83 * @param time time in milliseconds. 84 * @param latitude latitude in degrees. 85 * @param longitude latitude in degrees. 86 */ 87 @SuppressWarnings("FloatingPointLiteralPrecision") 88 public void calculateTwilight(long time, double latitude, double longitude) { 89 final float daysSince2000 = (float) (time - UTC_2000) / DateUtils.DAY_IN_MILLIS; 90 91 // mean anomaly 92 final float meanAnomaly = 6.240059968f + daysSince2000 * 0.01720197f; 93 94 // true anomaly 95 final double trueAnomaly = meanAnomaly + C1 * Math.sin(meanAnomaly) + C2 96 * Math.sin(2 * meanAnomaly) + C3 * Math.sin(3 * meanAnomaly); 97 98 // ecliptic longitude 99 final double solarLng = trueAnomaly + 1.796593063d + Math.PI; 100 101 // solar transit in days since 2000 102 final double arcLongitude = -longitude / 360; 103 float n = Math.round(daysSince2000 - J0 - arcLongitude); 104 double solarTransitJ2000 = n + J0 + arcLongitude + 0.0053d * Math.sin(meanAnomaly) 105 + -0.0069d * Math.sin(2 * solarLng); 106 107 // declination of sun 108 double solarDec = Math.asin(Math.sin(solarLng) * Math.sin(OBLIQUITY)); 109 110 final double latRad = latitude * DEGREES_TO_RADIANS; 111 112 double cosHourAngle = (Math.sin(ALTIDUTE_CORRECTION_CIVIL_TWILIGHT) - Math.sin(latRad) 113 * Math.sin(solarDec)) / (Math.cos(latRad) * Math.cos(solarDec)); 114 // The day or night never ends for the given date and location, if this value is out of 115 // range. 116 if (cosHourAngle >= 1) { 117 state = NIGHT; 118 sunset = -1; 119 sunrise = -1; 120 return; 121 } else if (cosHourAngle <= -1) { 122 state = DAY; 123 sunset = -1; 124 sunrise = -1; 125 return; 126 } 127 128 float hourAngle = (float) (Math.acos(cosHourAngle) / (2 * Math.PI)); 129 130 sunset = Math.round((solarTransitJ2000 + hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000; 131 sunrise = Math.round((solarTransitJ2000 - hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000; 132 133 if (sunrise < time && sunset > time) { 134 state = DAY; 135 } else { 136 state = NIGHT; 137 } 138 } 139 140 }