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.timezone.location.provider.core; 18 19 import androidx.annotation.IntDef; 20 import androidx.annotation.NonNull; 21 import androidx.annotation.Nullable; 22 23 import android.service.timezone.TimeZoneProviderService; 24 import android.service.timezone.TimeZoneProviderSuggestion; 25 26 import java.util.Objects; 27 28 /** 29 * A result of a period of time zone detection. 30 */ 31 public final class TimeZoneProviderResult { 32 33 @IntDef({ RESULT_TYPE_PERMANENT_FAILURE, RESULT_TYPE_SUGGESTION, RESULT_TYPE_UNCERTAIN }) 34 public @interface ResultType {} 35 36 /** 37 * The provider failed permanently. See {@link 38 * TimeZoneProviderService#reportPermanentFailure(Throwable)} 39 */ 40 public static final int RESULT_TYPE_PERMANENT_FAILURE = 1; 41 42 /** 43 * The provider made a suggestion. See {@link 44 * TimeZoneProviderService#reportSuggestion(TimeZoneProviderSuggestion)} 45 */ 46 public static final int RESULT_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 RESULT_TYPE_UNCERTAIN = 3; 53 54 private static final TimeZoneProviderResult UNCERTAIN_RESULT = 55 new TimeZoneProviderResult(RESULT_TYPE_UNCERTAIN, null, null); 56 57 @ResultType 58 private final int mType; 59 60 @Nullable 61 private final TimeZoneProviderSuggestion mSuggestion; 62 63 @Nullable 64 private final Throwable mFailureCause; 65 TimeZoneProviderResult(@esultType int type, @Nullable TimeZoneProviderSuggestion suggestion, @Nullable Throwable failureCause)66 private TimeZoneProviderResult(@ResultType int type, 67 @Nullable TimeZoneProviderSuggestion suggestion, 68 @Nullable Throwable failureCause) { 69 mType = type; 70 mSuggestion = suggestion; 71 mFailureCause = failureCause; 72 } 73 74 /** Returns a result of type {@link #RESULT_TYPE_SUGGESTION}. */ createSuggestion( @onNull TimeZoneProviderSuggestion suggestion)75 public static TimeZoneProviderResult createSuggestion( 76 @NonNull TimeZoneProviderSuggestion suggestion) { 77 return new TimeZoneProviderResult(RESULT_TYPE_SUGGESTION, 78 Objects.requireNonNull(suggestion), null); 79 } 80 81 /** Returns a result of type {@link #RESULT_TYPE_UNCERTAIN}. */ createUncertain()82 public static TimeZoneProviderResult createUncertain() { 83 return UNCERTAIN_RESULT; 84 } 85 86 /** Returns a result of type {@link #RESULT_TYPE_PERMANENT_FAILURE}. */ createPermanentFailure(@onNull Throwable cause)87 public static TimeZoneProviderResult createPermanentFailure(@NonNull Throwable cause) { 88 return new TimeZoneProviderResult(RESULT_TYPE_PERMANENT_FAILURE, null, 89 Objects.requireNonNull(cause)); 90 } 91 92 /** 93 * Returns the result type. 94 */ getType()95 public @ResultType int getType() { 96 return mType; 97 } 98 99 /** 100 * Returns the suggestion. Populated for {@link #RESULT_TYPE_SUGGESTION}. 101 */ 102 @NonNull getSuggestion()103 public TimeZoneProviderSuggestion getSuggestion() { 104 return mSuggestion; 105 } 106 107 /** 108 * Returns the failure cause. Populated for {@link #RESULT_TYPE_PERMANENT_FAILURE}. 109 */ 110 @Nullable getFailureCause()111 public Throwable getFailureCause() { 112 return mFailureCause; 113 } 114 115 @Override toString()116 public String toString() { 117 return "TimeZoneProviderResult{" 118 + "mResultType=" + mType 119 + ", mSuggestion=" + mSuggestion 120 + '}'; 121 } 122 123 @Override equals(Object o)124 public boolean equals(Object o) { 125 if (this == o) { 126 return true; 127 } 128 if (o == null || getClass() != o.getClass()) { 129 return false; 130 } 131 TimeZoneProviderResult that = (TimeZoneProviderResult) o; 132 return mType == that.mType 133 && Objects.equals(mSuggestion, that.mSuggestion); 134 } 135 136 @Override hashCode()137 public int hashCode() { 138 return Objects.hash(mType, mSuggestion); 139 } 140 } 141