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 com.android.server.timezonedetector.location; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.service.timezone.TimeZoneProviderService; 23 import android.service.timezone.TimeZoneProviderSuggestion; 24 25 import java.util.Objects; 26 27 /** 28 * An event from a {@link TimeZoneProviderService}. 29 */ 30 final class TimeZoneProviderEvent { 31 32 @IntDef(prefix = "EVENT_TYPE_", 33 value = { EVENT_TYPE_PERMANENT_FAILURE, EVENT_TYPE_SUGGESTION, EVENT_TYPE_UNCERTAIN }) 34 public @interface EventType {} 35 36 /** 37 * The provider failed permanently. See {@link 38 * TimeZoneProviderService#reportPermanentFailure(Throwable)} 39 */ 40 public static final int EVENT_TYPE_PERMANENT_FAILURE = 1; 41 42 /** 43 * The provider made a suggestion. See {@link 44 * TimeZoneProviderService#reportSuggestion(TimeZoneProviderSuggestion)} 45 */ 46 public static final int EVENT_TYPE_SUGGESTION = 2; 47 48 /** 49 * The provider was uncertain about the time zone. See {@link 50 * TimeZoneProviderService#reportUncertain()} 51 */ 52 public static final int EVENT_TYPE_UNCERTAIN = 3; 53 54 private static final TimeZoneProviderEvent UNCERTAIN_EVENT = 55 new TimeZoneProviderEvent(EVENT_TYPE_UNCERTAIN, null, null); 56 57 @EventType 58 private final int mType; 59 60 @Nullable 61 private final TimeZoneProviderSuggestion mSuggestion; 62 63 @Nullable 64 private final String mFailureCause; 65 TimeZoneProviderEvent(@ventType int type, @Nullable TimeZoneProviderSuggestion suggestion, @Nullable String failureCause)66 private TimeZoneProviderEvent(@EventType int type, 67 @Nullable TimeZoneProviderSuggestion suggestion, 68 @Nullable String failureCause) { 69 mType = type; 70 mSuggestion = suggestion; 71 mFailureCause = failureCause; 72 } 73 74 /** Returns a event of type {@link #EVENT_TYPE_SUGGESTION}. */ createSuggestionEvent( @onNull TimeZoneProviderSuggestion suggestion)75 public static TimeZoneProviderEvent createSuggestionEvent( 76 @NonNull TimeZoneProviderSuggestion suggestion) { 77 return new TimeZoneProviderEvent(EVENT_TYPE_SUGGESTION, 78 Objects.requireNonNull(suggestion), null); 79 } 80 81 /** Returns a event of type {@link #EVENT_TYPE_UNCERTAIN}. */ createUncertainEvent()82 public static TimeZoneProviderEvent createUncertainEvent() { 83 return UNCERTAIN_EVENT; 84 } 85 86 /** Returns a event of type {@link #EVENT_TYPE_PERMANENT_FAILURE}. */ createPermanentFailureEvent(@onNull String cause)87 public static TimeZoneProviderEvent createPermanentFailureEvent(@NonNull String cause) { 88 return new TimeZoneProviderEvent(EVENT_TYPE_PERMANENT_FAILURE, null, 89 Objects.requireNonNull(cause)); 90 } 91 92 /** 93 * Returns the event type. 94 */ getType()95 public @EventType int getType() { 96 return mType; 97 } 98 99 /** 100 * Returns the suggestion. Populated when {@link #getType()} is {@link #EVENT_TYPE_SUGGESTION}. 101 */ 102 @Nullable getSuggestion()103 public TimeZoneProviderSuggestion getSuggestion() { 104 return mSuggestion; 105 } 106 107 /** 108 * Returns the failure cauese. Populated when {@link #getType()} is {@link 109 * #EVENT_TYPE_PERMANENT_FAILURE}. 110 */ 111 @Nullable getFailureCause()112 public String getFailureCause() { 113 return mFailureCause; 114 } 115 116 @Override toString()117 public String toString() { 118 return "TimeZoneProviderEvent{" 119 + "mType=" + mType 120 + ", mSuggestion=" + mSuggestion 121 + ", mFailureCause=" + mFailureCause 122 + '}'; 123 } 124 125 @Override equals(Object o)126 public boolean equals(Object o) { 127 if (this == o) { 128 return true; 129 } 130 if (o == null || getClass() != o.getClass()) { 131 return false; 132 } 133 TimeZoneProviderEvent that = (TimeZoneProviderEvent) o; 134 return mType == that.mType 135 && Objects.equals(mSuggestion, that.mSuggestion) 136 && Objects.equals(mFailureCause, that.mFailureCause); 137 } 138 139 @Override hashCode()140 public int hashCode() { 141 return Objects.hash(mType, mSuggestion, mFailureCause); 142 } 143 } 144