blob: 30a8cccb6ad55363c5f9948f6b6d2cdf07c8eac9 [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
21import java.util.Calendar;
Adam Lesinski182f73f2013-12-05 16:48:06 -080022
23/**
Justin Klaassen908b86c2016-08-08 09:18:42 -070024 * The twilight state, consisting of the sunrise and sunset times (in millis) for the current
25 * period.
26 * <p/>
27 * Note: This object is immutable.
Adam Lesinski182f73f2013-12-05 16:48:06 -080028 */
Justin Klaassen908b86c2016-08-08 09:18:42 -070029public final class TwilightState {
Justin Klaassen63848782016-07-12 13:36:45 -070030
Justin Klaassen908b86c2016-08-08 09:18:42 -070031 private final long mSunriseTimeMillis;
32 private final long mSunsetTimeMillis;
Adam Lesinski182f73f2013-12-05 16:48:06 -080033
Christine Frankse5bb03e2017-02-10 17:36:10 -080034 public TwilightState(long sunriseTimeMillis, long sunsetTimeMillis) {
Justin Klaassen908b86c2016-08-08 09:18:42 -070035 mSunriseTimeMillis = sunriseTimeMillis;
36 mSunsetTimeMillis = sunsetTimeMillis;
Adam Lesinski182f73f2013-12-05 16:48:06 -080037 }
38
39 /**
Justin Klaassen908b86c2016-08-08 09:18:42 -070040 * Returns the time (in UTC milliseconds from epoch) of the upcoming or previous sunrise if
41 * it's night or day respectively.
42 */
43 public long sunriseTimeMillis() {
44 return mSunriseTimeMillis;
45 }
46
47 /**
48 * Returns a new {@link Calendar} instance initialized to {@link #sunriseTimeMillis()}.
49 */
50 public Calendar sunrise() {
51 final Calendar sunrise = Calendar.getInstance();
52 sunrise.setTimeInMillis(mSunriseTimeMillis);
53 return sunrise;
54 }
55
56 /**
57 * Returns the time (in UTC milliseconds from epoch) of the upcoming or previous sunset if
58 * it's day or night respectively.
59 */
60 public long sunsetTimeMillis() {
61 return mSunsetTimeMillis;
62 }
63
64 /**
65 * Returns a new {@link Calendar} instance initialized to {@link #sunsetTimeMillis()}.
66 */
67 public Calendar sunset() {
68 final Calendar sunset = Calendar.getInstance();
69 sunset.setTimeInMillis(mSunsetTimeMillis);
70 return sunset;
71 }
72
73 /**
74 * Returns {@code true} if it is currently night time.
Adam Lesinski182f73f2013-12-05 16:48:06 -080075 */
76 public boolean isNight() {
Justin Klaassen908b86c2016-08-08 09:18:42 -070077 final long now = System.currentTimeMillis();
78 return now >= mSunsetTimeMillis && now < mSunriseTimeMillis;
Adam Lesinski182f73f2013-12-05 16:48:06 -080079 }
80
81 @Override
82 public boolean equals(Object o) {
Justin Klaassen908b86c2016-08-08 09:18:42 -070083 return o instanceof TwilightState && equals((TwilightState) o);
Adam Lesinski182f73f2013-12-05 16:48:06 -080084 }
85
86 public boolean equals(TwilightState other) {
87 return other != null
Justin Klaassen908b86c2016-08-08 09:18:42 -070088 && mSunriseTimeMillis == other.mSunriseTimeMillis
89 && mSunsetTimeMillis == other.mSunsetTimeMillis;
Adam Lesinski182f73f2013-12-05 16:48:06 -080090 }
91
92 @Override
93 public int hashCode() {
Justin Klaassen908b86c2016-08-08 09:18:42 -070094 return Long.hashCode(mSunriseTimeMillis) ^ Long.hashCode(mSunsetTimeMillis);
Adam Lesinski182f73f2013-12-05 16:48:06 -080095 }
96
97 @Override
98 public String toString() {
Justin Klaassen908b86c2016-08-08 09:18:42 -070099 return "TwilightState {"
100 + " sunrise=" + DateFormat.format("MM-dd HH:mm", mSunriseTimeMillis)
101 + " sunset="+ DateFormat.format("MM-dd HH:mm", mSunsetTimeMillis)
102 + " }";
Adam Lesinski182f73f2013-12-05 16:48:06 -0800103 }
104}