1 /*
2  * Copyright 2021 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 androidx.wear.watchface.complications.data
18 
19 import android.support.wearable.complications.ComplicationData as WireComplicationData
20 import java.time.Instant
21 
22 /** A range of time, that may be unbounded on either side. */
23 public class TimeRange
24 internal constructor(
25     public val startDateTimeMillis: Instant,
26     public val endDateTimeMillis: Instant
27 ) {
28     /** Returns whether the [TimeRange] contains a given point in time. */
containsnull29     public operator fun contains(dateTimeMillis: Instant): Boolean =
30         (dateTimeMillis >= startDateTimeMillis) and (dateTimeMillis <= endDateTimeMillis)
31 
32     override fun equals(other: Any?): Boolean {
33         if (this === other) return true
34         if (javaClass != other?.javaClass) return false
35 
36         other as TimeRange
37 
38         if (startDateTimeMillis != other.startDateTimeMillis) return false
39         if (endDateTimeMillis != other.endDateTimeMillis) return false
40 
41         return true
42     }
43 
hashCodenull44     override fun hashCode(): Int {
45         var result = startDateTimeMillis.hashCode()
46         result = 31 * result + endDateTimeMillis.hashCode()
47         return result
48     }
49 
toStringnull50     override fun toString(): String {
51         if (WireComplicationData.shouldRedact()) {
52             return "TimeRange(REDACTED)"
53         }
54         return "TimeRange(startDateTimeMillis=$startDateTimeMillis, " +
55             "endDateTimeMillis=$endDateTimeMillis)"
56     }
57 
58     public companion object {
59         /** The [TimeRange] that includes every point in time. */
60         @JvmField public val ALWAYS: TimeRange = TimeRange(Instant.MIN, Instant.MAX)
61 
62         /** Constructs a time range after a given point in time. */
63         @JvmStatic
afternull64         public fun after(startInstant: Instant): TimeRange = TimeRange(startInstant, Instant.MAX)
65 
66         /** Constructs a time range until a given point in time. */
67         @JvmStatic
68         public fun before(endInstant: Instant): TimeRange = TimeRange(Instant.MIN, endInstant)
69 
70         /**
71          * Constructs a time range between two points in time, inclusive of the points themselves.
72          */
73         @JvmStatic
74         public fun between(startInstant: Instant, endInstant: Instant): TimeRange =
75             TimeRange(startInstant, endInstant)
76     }
77 }
78 
79 /** Defines a point in time the complication is counting down until. */
80 public class CountDownTimeReference(public val instant: Instant)
81 
82 /** Defines a point in time the complication is counting up from. */
83 public class CountUpTimeReference(public val instant: Instant)
84