Debian Codegraph @

core/java/android/service/timezone/TimeZoneProviderEvent.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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 android.service.timezone;
18
19 import android.annotation.ElapsedRealtimeLong;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25
26 import java.lang.annotation.ElementType;
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 import java.lang.annotation.Target;
30 import java.time.Duration;
31 import java.util.Objects;
32
33 /**
34 * Encapsulates a reported event from a {@link TimeZoneProviderService}.
35 *
36 * @hide
37 */
38 public final class TimeZoneProviderEvent implements Parcelable {
39
40 @IntDef(prefix = "EVENT_TYPE_",
41 value = { EVENT_TYPE_PERMANENT_FAILURE, EVENT_TYPE_SUGGESTION, EVENT_TYPE_UNCERTAIN })
42 @Retention(RetentionPolicy.SOURCE)
43 @Target({ ElementType.TYPE_USE, ElementType.TYPE_PARAMETER })
44 public @interface EventType {}
45
46 /**
47 * The provider failed permanently. See {@link
48 * TimeZoneProviderService#reportPermanentFailure(Throwable)}
49 */
50 public static final @EventType int EVENT_TYPE_PERMANENT_FAILURE = 1;
51
52 /**
53 * The provider made a suggestion. See {@link
54 * TimeZoneProviderService#reportSuggestion(TimeZoneProviderSuggestion)}
55 */
56 public static final @EventType int EVENT_TYPE_SUGGESTION = 2;
57
58 /**
59 * The provider was uncertain about the time zone. See {@link
60 * TimeZoneProviderService#reportUncertain()}
61 */
62 public static final @EventType int EVENT_TYPE_UNCERTAIN = 3;
63
64 private final @EventType int mType;
65
66 @ElapsedRealtimeLong
67 private final long mCreationElapsedMillis;
68
69 @Nullable
70 private final TimeZoneProviderSuggestion mSuggestion;
71
72 @Nullable
73 private final String mFailureCause;
74
75 private TimeZoneProviderEvent(@EventType int type,
76 @ElapsedRealtimeLong long creationElapsedMillis,
77 @Nullable TimeZoneProviderSuggestion suggestion,
78 @Nullable String failureCause) {
79 mType = type;
80 mCreationElapsedMillis = creationElapsedMillis;
81 mSuggestion = suggestion;
82 mFailureCause = failureCause;
83 }
84
85 /** Returns a event of type {@link #EVENT_TYPE_SUGGESTION}. */
86 public static TimeZoneProviderEvent createSuggestionEvent(
87 @ElapsedRealtimeLong long creationElapsedMillis,
88 @NonNull TimeZoneProviderSuggestion suggestion) {
89 return new TimeZoneProviderEvent(EVENT_TYPE_SUGGESTION, creationElapsedMillis,
90 Objects.requireNonNull(suggestion), null);
91 }
92
93 /** Returns a event of type {@link #EVENT_TYPE_UNCERTAIN}. */
94 public static TimeZoneProviderEvent createUncertainEvent(
95 @ElapsedRealtimeLong long creationElapsedMillis) {
96 return new TimeZoneProviderEvent(EVENT_TYPE_UNCERTAIN, creationElapsedMillis, null, null);
97 }
98
99 /** Returns a event of type {@link #EVENT_TYPE_PERMANENT_FAILURE}. */
100 public static TimeZoneProviderEvent createPermanentFailureEvent(
101 @ElapsedRealtimeLong long creationElapsedMillis,
102 @NonNull String cause) {
103 return new TimeZoneProviderEvent(EVENT_TYPE_PERMANENT_FAILURE, creationElapsedMillis, null,
104 Objects.requireNonNull(cause));
105 }
106
107 /**
108 * Returns the event type.
109 */
110 public @EventType int getType() {
111 return mType;
112 }
113
114 /** Returns the time according to the elapsed realtime clock when the event was created. */
115 @ElapsedRealtimeLong
116 public long getCreationElapsedMillis() {
117 return mCreationElapsedMillis;
118 }
119
120 /**
121 * Returns the suggestion. Populated when {@link #getType()} is {@link #EVENT_TYPE_SUGGESTION}.
122 */
123 @Nullable
124 public TimeZoneProviderSuggestion getSuggestion() {
125 return mSuggestion;
126 }
127
128 /**
129 * Returns the failure cauese. Populated when {@link #getType()} is {@link
130 * #EVENT_TYPE_PERMANENT_FAILURE}.
131 */
132 @Nullable
133 public String getFailureCause() {
134 return mFailureCause;
135 }
136
137 public static final @NonNull Creator<TimeZoneProviderEvent> CREATOR =
138 new Creator<TimeZoneProviderEvent>() {
139 @Override
140 public TimeZoneProviderEvent createFromParcel(Parcel in) {
141 int type = in.readInt();
142 long creationElapsedMillis = in.readLong();
143 TimeZoneProviderSuggestion suggestion =
144 in.readParcelable(getClass().getClassLoader(), android.service.timezone.TimeZoneProviderSuggestion.class);
145 String failureCause = in.readString8();
146 return new TimeZoneProviderEvent(
147 type, creationElapsedMillis, suggestion, failureCause);
148 }
149
150 @Override
151 public TimeZoneProviderEvent[] newArray(int size) {
152 return new TimeZoneProviderEvent[size];
153 }
154 };
155
156 @Override
157 public int describeContents() {
158 return 0;
159 }
160
161 @Override
162 public void writeToParcel(@NonNull Parcel parcel, int flags) {
163 parcel.writeInt(mType);
164 parcel.writeLong(mCreationElapsedMillis);
165 parcel.writeParcelable(mSuggestion, 0);
166 parcel.writeString8(mFailureCause);
167 }
168
169 @Override
170 public String toString() {
171 return "TimeZoneProviderEvent{"
172 + "mType=" + mType
173 + ", mCreationElapsedMillis=" + Duration.ofMillis(mCreationElapsedMillis).toString()
174 + ", mSuggestion=" + mSuggestion
175 + ", mFailureCause=" + mFailureCause
176 + '}';
177 }
178
179 /**
180 * Similar to {@link #equals} except this methods checks for equivalence, not equality.
181 * i.e. two {@link #EVENT_TYPE_UNCERTAIN} and {@link #EVENT_TYPE_PERMANENT_FAILURE} events are
182 * always equivalent, two {@link #EVENT_TYPE_SUGGESTION} events are equivalent if they suggest
183 * the same time zones.
184 */
185 @SuppressWarnings("ReferenceEquality")
186 public boolean isEquivalentTo(@Nullable TimeZoneProviderEvent other) {
187 if (this == other) {
188 return true;
189 }
190 if (other == null || mType != other.mType) {
191 return false;
192 }
193 if (mType == EVENT_TYPE_SUGGESTION) {
194 return mSuggestion.isEquivalentTo(other.getSuggestion());
195 }
196 return true;
197 }
198
199 @Override
200 public boolean equals(Object o) {
201 if (this == o) {
202 return true;
203 }
204 if (o == null || getClass() != o.getClass()) {
205 return false;
206 }
207 TimeZoneProviderEvent that = (TimeZoneProviderEvent) o;
208 return mType == that.mType
209 && mCreationElapsedMillis == that.mCreationElapsedMillis
210 && Objects.equals(mSuggestion, that.mSuggestion)
211 && Objects.equals(mFailureCause, that.mFailureCause);
212 }
213
214 @Override
215 public int hashCode() {
216 return Objects.hash(mType, mCreationElapsedMillis, mSuggestion, mFailureCause);
217 }
218 }