| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
public static final @EventType int EVENT_TYPE_PERMANENT_FAILURE = 1; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static final @EventType int EVENT_TYPE_SUGGESTION = 2; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 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 |
|
| 94 |
public static TimeZoneProviderEvent createUncertainEvent( |
| 95 |
@ElapsedRealtimeLong long creationElapsedMillis) { |
| 96 |
return new TimeZoneProviderEvent(EVENT_TYPE_UNCERTAIN, creationElapsedMillis, null, null); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 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 |
|
| 109 |
|
| 110 |
public @EventType int getType() { |
| 111 |
return mType; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
@ElapsedRealtimeLong |
| 116 |
public long getCreationElapsedMillis() { |
| 117 |
return mCreationElapsedMillis; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
@Nullable |
| 124 |
public TimeZoneProviderSuggestion getSuggestion() { |
| 125 |
return mSuggestion; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 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 |
} |