Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 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 com.android.server.twilight; |
| 18 | |
| 19 | import java.text.DateFormat; |
| 20 | import java.util.Date; |
| 21 | |
| 22 | /** |
| 23 | * Describes whether it is day or night. |
| 24 | * This object is immutable. |
| 25 | */ |
| 26 | public class TwilightState { |
| 27 | private final boolean mIsNight; |
| 28 | private final long mYesterdaySunset; |
| 29 | private final long mTodaySunrise; |
| 30 | private final long mTodaySunset; |
| 31 | private final long mTomorrowSunrise; |
| 32 | |
| 33 | TwilightState(boolean isNight, |
| 34 | long yesterdaySunset, |
| 35 | long todaySunrise, long todaySunset, |
| 36 | long tomorrowSunrise) { |
| 37 | mIsNight = isNight; |
| 38 | mYesterdaySunset = yesterdaySunset; |
| 39 | mTodaySunrise = todaySunrise; |
| 40 | mTodaySunset = todaySunset; |
| 41 | mTomorrowSunrise = tomorrowSunrise; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns true if it is currently night time. |
| 46 | */ |
| 47 | public boolean isNight() { |
| 48 | return mIsNight; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the time of yesterday's sunset in the System.currentTimeMillis() timebase, |
| 53 | * or -1 if the sun never sets. |
| 54 | */ |
| 55 | public long getYesterdaySunset() { |
| 56 | return mYesterdaySunset; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the time of today's sunrise in the System.currentTimeMillis() timebase, |
| 61 | * or -1 if the sun never rises. |
| 62 | */ |
| 63 | public long getTodaySunrise() { |
| 64 | return mTodaySunrise; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the time of today's sunset in the System.currentTimeMillis() timebase, |
| 69 | * or -1 if the sun never sets. |
| 70 | */ |
| 71 | public long getTodaySunset() { |
| 72 | return mTodaySunset; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the time of tomorrow's sunrise in the System.currentTimeMillis() timebase, |
| 77 | * or -1 if the sun never rises. |
| 78 | */ |
| 79 | public long getTomorrowSunrise() { |
| 80 | return mTomorrowSunrise; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean equals(Object o) { |
| 85 | return o instanceof TwilightState && equals((TwilightState)o); |
| 86 | } |
| 87 | |
| 88 | public boolean equals(TwilightState other) { |
| 89 | return other != null |
| 90 | && mIsNight == other.mIsNight |
| 91 | && mYesterdaySunset == other.mYesterdaySunset |
| 92 | && mTodaySunrise == other.mTodaySunrise |
| 93 | && mTodaySunset == other.mTodaySunset |
| 94 | && mTomorrowSunrise == other.mTomorrowSunrise; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public int hashCode() { |
| 99 | return 0; // don't care |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | DateFormat f = DateFormat.getDateTimeInstance(); |
| 105 | return "{TwilightState: isNight=" + mIsNight |
| 106 | + ", mYesterdaySunset=" + f.format(new Date(mYesterdaySunset)) |
| 107 | + ", mTodaySunrise=" + f.format(new Date(mTodaySunrise)) |
| 108 | + ", mTodaySunset=" + f.format(new Date(mTodaySunset)) |
| 109 | + ", mTomorrowSunrise=" + f.format(new Date(mTomorrowSunrise)) |
| 110 | + "}"; |
| 111 | } |
| 112 | } |