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 | |
| 21 | import java.util.Calendar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 24 | * 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 Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 28 | */ |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 29 | public final class TwilightState { |
Justin Klaassen | 6384878 | 2016-07-12 13:36:45 -0700 | [diff] [blame] | 30 | |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 31 | private final long mSunriseTimeMillis; |
| 32 | private final long mSunsetTimeMillis; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 33 | |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 34 | public TwilightState(long sunriseTimeMillis, long sunsetTimeMillis) { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 35 | mSunriseTimeMillis = sunriseTimeMillis; |
| 36 | mSunsetTimeMillis = sunsetTimeMillis; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /** |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 40 | * 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 Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 75 | */ |
| 76 | public boolean isNight() { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 77 | final long now = System.currentTimeMillis(); |
| 78 | return now >= mSunsetTimeMillis && now < mSunriseTimeMillis; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean equals(Object o) { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 83 | return o instanceof TwilightState && equals((TwilightState) o); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public boolean equals(TwilightState other) { |
| 87 | return other != null |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 88 | && mSunriseTimeMillis == other.mSunriseTimeMillis |
| 89 | && mSunsetTimeMillis == other.mSunsetTimeMillis; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int hashCode() { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 94 | return Long.hashCode(mSunriseTimeMillis) ^ Long.hashCode(mSunsetTimeMillis); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 99 | return "TwilightState {" |
| 100 | + " sunrise=" + DateFormat.format("MM-dd HH:mm", mSunriseTimeMillis) |
| 101 | + " sunset="+ DateFormat.format("MM-dd HH:mm", mSunsetTimeMillis) |
| 102 | + " }"; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 103 | } |
| 104 | } |