blob: 71304a7a4701a1b2c0ca7949d7ac19c98be2aa3c [file] [log] [blame]
Adam Lesinski182f73f2013-12-05 16:48:06 -08001/*
Justin Klaassen908b86c2016-08-08 09:18:42 -07002 * Copyright (C) 2016 The Android Open Source Project
Adam Lesinski182f73f2013-12-05 16:48:06 -08003 *
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.twilight;
18
Justin Klaassen908b86c2016-08-08 09:18:42 -070019import android.text.format.DateFormat;
20
Christine Franks03213462017-08-25 13:57:26 -070021import java.time.Instant;
22import java.time.LocalDateTime;
23import java.time.ZoneId;
24import java.util.TimeZone;
Adam Lesinski182f73f2013-12-05 16:48:06 -080025
26/**
Justin Klaassen908b86c2016-08-08 09:18:42 -070027 * 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 Lesinski182f73f2013-12-05 16:48:06 -080031 */
Justin Klaassen908b86c2016-08-08 09:18:42 -070032public final class TwilightState {
Justin Klaassen63848782016-07-12 13:36:45 -070033
Justin Klaassen908b86c2016-08-08 09:18:42 -070034 private final long mSunriseTimeMillis;
35 private final long mSunsetTimeMillis;
Adam Lesinski182f73f2013-12-05 16:48:06 -080036
Christine Frankse5bb03e2017-02-10 17:36:10 -080037 public TwilightState(long sunriseTimeMillis, long sunsetTimeMillis) {
Justin Klaassen908b86c2016-08-08 09:18:42 -070038 mSunriseTimeMillis = sunriseTimeMillis;
39 mSunsetTimeMillis = sunsetTimeMillis;
Adam Lesinski182f73f2013-12-05 16:48:06 -080040 }
41
42 /**
Justin Klaassen908b86c2016-08-08 09:18:42 -070043 * 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 Franks03213462017-08-25 13:57:26 -070051 * Returns a new {@link LocalDateTime} instance initialized to {@link #sunriseTimeMillis()}.
Justin Klaassen908b86c2016-08-08 09:18:42 -070052 */
Christine Franks03213462017-08-25 13:57:26 -070053 public LocalDateTime sunrise() {
54 final ZoneId zoneId = TimeZone.getDefault().toZoneId();
55 return LocalDateTime.ofInstant(Instant.ofEpochMilli(mSunriseTimeMillis), zoneId);
Justin Klaassen908b86c2016-08-08 09:18:42 -070056 }
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 Franks03213462017-08-25 13:57:26 -070067 * Returns a new {@link LocalDateTime} instance initialized to {@link #sunsetTimeMillis()}.
Justin Klaassen908b86c2016-08-08 09:18:42 -070068 */
Christine Franks03213462017-08-25 13:57:26 -070069 public LocalDateTime sunset() {
70 final ZoneId zoneId = TimeZone.getDefault().toZoneId();
71 return LocalDateTime.ofInstant(Instant.ofEpochMilli(mSunsetTimeMillis), zoneId);
Justin Klaassen908b86c2016-08-08 09:18:42 -070072 }
73
74 /**
75 * Returns {@code true} if it is currently night time.
Adam Lesinski182f73f2013-12-05 16:48:06 -080076 */
77 public boolean isNight() {
Justin Klaassen908b86c2016-08-08 09:18:42 -070078 final long now = System.currentTimeMillis();
79 return now >= mSunsetTimeMillis && now < mSunriseTimeMillis;
Adam Lesinski182f73f2013-12-05 16:48:06 -080080 }
81
82 @Override
83 public boolean equals(Object o) {
Justin Klaassen908b86c2016-08-08 09:18:42 -070084 return o instanceof TwilightState && equals((TwilightState) o);
Adam Lesinski182f73f2013-12-05 16:48:06 -080085 }
86
87 public boolean equals(TwilightState other) {
88 return other != null
Justin Klaassen908b86c2016-08-08 09:18:42 -070089 && mSunriseTimeMillis == other.mSunriseTimeMillis
90 && mSunsetTimeMillis == other.mSunsetTimeMillis;
Adam Lesinski182f73f2013-12-05 16:48:06 -080091 }
92
93 @Override
94 public int hashCode() {
Justin Klaassen908b86c2016-08-08 09:18:42 -070095 return Long.hashCode(mSunriseTimeMillis) ^ Long.hashCode(mSunsetTimeMillis);
Adam Lesinski182f73f2013-12-05 16:48:06 -080096 }
97
98 @Override
99 public String toString() {
Justin Klaassen908b86c2016-08-08 09:18:42 -0700100 return "TwilightState {"
101 + " sunrise=" + DateFormat.format("MM-dd HH:mm", mSunriseTimeMillis)
102 + " sunset="+ DateFormat.format("MM-dd HH:mm", mSunsetTimeMillis)
103 + " }";
Adam Lesinski182f73f2013-12-05 16:48:06 -0800104 }
105}