1/* 2 * Copyright (C) 2016 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 17package com.android.server.twilight; 18 19import android.text.format.DateFormat; 20 21import java.util.Calendar; 22 23/** 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. 28 */ 29public final class TwilightState { 30 31 private final long mSunriseTimeMillis; 32 private final long mSunsetTimeMillis; 33 34 TwilightState(long sunriseTimeMillis, long sunsetTimeMillis) { 35 mSunriseTimeMillis = sunriseTimeMillis; 36 mSunsetTimeMillis = sunsetTimeMillis; 37 } 38 39 /** 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. 75 */ 76 public boolean isNight() { 77 final long now = System.currentTimeMillis(); 78 return now >= mSunsetTimeMillis && now < mSunriseTimeMillis; 79 } 80 81 @Override 82 public boolean equals(Object o) { 83 return o instanceof TwilightState && equals((TwilightState) o); 84 } 85 86 public boolean equals(TwilightState other) { 87 return other != null 88 && mSunriseTimeMillis == other.mSunriseTimeMillis 89 && mSunsetTimeMillis == other.mSunsetTimeMillis; 90 } 91 92 @Override 93 public int hashCode() { 94 return Long.hashCode(mSunriseTimeMillis) ^ Long.hashCode(mSunsetTimeMillis); 95 } 96 97 @Override 98 public String toString() { 99 return "TwilightState {" 100 + " sunrise=" + DateFormat.format("MM-dd HH:mm", mSunriseTimeMillis) 101 + " sunset="+ DateFormat.format("MM-dd HH:mm", mSunsetTimeMillis) 102 + " }"; 103 } 104} 105