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 17 package com.android.server.twilight; 18 19 import android.text.format.DateFormat; 20 21 import java.time.Instant; 22 import java.time.LocalDateTime; 23 import java.time.ZoneId; 24 import java.util.TimeZone; 25 26 /** 27 * 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. 31 */ 32 public final class TwilightState { 33 34 private final long mSunriseTimeMillis; 35 private final long mSunsetTimeMillis; 36 TwilightState(long sunriseTimeMillis, long sunsetTimeMillis)37 public TwilightState(long sunriseTimeMillis, long sunsetTimeMillis) { 38 mSunriseTimeMillis = sunriseTimeMillis; 39 mSunsetTimeMillis = sunsetTimeMillis; 40 } 41 42 /** 43 * Returns the time (in UTC milliseconds from epoch) of the upcoming or previous sunrise if 44 * it's night or day respectively. 45 */ sunriseTimeMillis()46 public long sunriseTimeMillis() { 47 return mSunriseTimeMillis; 48 } 49 50 /** 51 * Returns a new {@link LocalDateTime} instance initialized to {@link #sunriseTimeMillis()}. 52 */ sunrise()53 public LocalDateTime sunrise() { 54 final ZoneId zoneId = TimeZone.getDefault().toZoneId(); 55 return LocalDateTime.ofInstant(Instant.ofEpochMilli(mSunriseTimeMillis), zoneId); 56 } 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 */ sunsetTimeMillis()62 public long sunsetTimeMillis() { 63 return mSunsetTimeMillis; 64 } 65 66 /** 67 * Returns a new {@link LocalDateTime} instance initialized to {@link #sunsetTimeMillis()}. 68 */ sunset()69 public LocalDateTime sunset() { 70 final ZoneId zoneId = TimeZone.getDefault().toZoneId(); 71 return LocalDateTime.ofInstant(Instant.ofEpochMilli(mSunsetTimeMillis), zoneId); 72 } 73 74 /** 75 * Returns {@code true} if it is currently night time. 76 */ isNight()77 public boolean isNight() { 78 final long now = System.currentTimeMillis(); 79 return now >= mSunsetTimeMillis && now < mSunriseTimeMillis; 80 } 81 82 @Override equals(Object o)83 public boolean equals(Object o) { 84 return o instanceof TwilightState && equals((TwilightState) o); 85 } 86 equals(TwilightState other)87 public boolean equals(TwilightState other) { 88 return other != null 89 && mSunriseTimeMillis == other.mSunriseTimeMillis 90 && mSunsetTimeMillis == other.mSunsetTimeMillis; 91 } 92 93 @Override hashCode()94 public int hashCode() { 95 return Long.hashCode(mSunriseTimeMillis) ^ Long.hashCode(mSunsetTimeMillis); 96 } 97 98 @Override toString()99 public String toString() { 100 return "TwilightState {" 101 + " sunrise=" + DateFormat.format("MM-dd HH:mm", mSunriseTimeMillis) 102 + " sunset="+ DateFormat.format("MM-dd HH:mm", mSunsetTimeMillis) 103 + " }"; 104 } 105 } 106