Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1 | /* |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 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.twilight; |
| 18 | |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 19 | import android.text.format.DateFormat; |
| 20 | |
Christine Franks | 2ce862c | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 21 | import java.time.Instant; |
| 22 | import java.time.LocalDateTime; |
| 23 | import java.time.ZoneId; |
| 24 | import java.util.TimeZone; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 27 | * The twilight state, consisting of the sunrise and sunset times (in millis) for the current |
| 28 | * period. |
| 29 | * <p/> |
| 30 | * Note: This object is immutable. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 31 | */ |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 32 | public final class TwilightState { |
Justin Klaassen | 6384878 | 2016-07-12 13:36:45 -0700 | [diff] [blame] | 33 | |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 34 | private final long mSunriseTimeMillis; |
| 35 | private final long mSunsetTimeMillis; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 36 | |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 37 | public TwilightState(long sunriseTimeMillis, long sunsetTimeMillis) { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 38 | mSunriseTimeMillis = sunriseTimeMillis; |
| 39 | mSunsetTimeMillis = sunsetTimeMillis; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 43 | * Returns the time (in UTC milliseconds from epoch) of the upcoming or previous sunrise if |
| 44 | * it's night or day respectively. |
| 45 | */ |
| 46 | public long sunriseTimeMillis() { |
| 47 | return mSunriseTimeMillis; |
| 48 | } |
| 49 | |
| 50 | /** |
Christine Franks | 2ce862c | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 51 | * Returns a new {@link LocalDateTime} instance initialized to {@link #sunriseTimeMillis()}. |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 52 | */ |
Christine Franks | 2ce862c | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 53 | public LocalDateTime sunrise() { |
| 54 | final ZoneId zoneId = TimeZone.getDefault().toZoneId(); |
| 55 | return LocalDateTime.ofInstant(Instant.ofEpochMilli(mSunriseTimeMillis), zoneId); |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns the time (in UTC milliseconds from epoch) of the upcoming or previous sunset if |
| 60 | * it's day or night respectively. |
| 61 | */ |
| 62 | public long sunsetTimeMillis() { |
| 63 | return mSunsetTimeMillis; |
| 64 | } |
| 65 | |
| 66 | /** |
Christine Franks | 2ce862c | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 67 | * Returns a new {@link LocalDateTime} instance initialized to {@link #sunsetTimeMillis()}. |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 68 | */ |
Christine Franks | 2ce862c | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 69 | public LocalDateTime sunset() { |
| 70 | final ZoneId zoneId = TimeZone.getDefault().toZoneId(); |
| 71 | return LocalDateTime.ofInstant(Instant.ofEpochMilli(mSunsetTimeMillis), zoneId); |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns {@code true} if it is currently night time. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 76 | */ |
| 77 | public boolean isNight() { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 78 | final long now = System.currentTimeMillis(); |
| 79 | return now >= mSunsetTimeMillis && now < mSunriseTimeMillis; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean equals(Object o) { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 84 | return o instanceof TwilightState && equals((TwilightState) o); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | public boolean equals(TwilightState other) { |
| 88 | return other != null |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 89 | && mSunriseTimeMillis == other.mSunriseTimeMillis |
| 90 | && mSunsetTimeMillis == other.mSunsetTimeMillis; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public int hashCode() { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 95 | return Long.hashCode(mSunriseTimeMillis) ^ Long.hashCode(mSunsetTimeMillis); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String toString() { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 100 | return "TwilightState {" |
| 101 | + " sunrise=" + DateFormat.format("MM-dd HH:mm", mSunriseTimeMillis) |
| 102 | + " sunset="+ DateFormat.format("MM-dd HH:mm", mSunsetTimeMillis) |
| 103 | + " }"; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 104 | } |
| 105 | } |