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.NonNull;
20 import android.annotation.Nullable;
21 
22 import java.time.Duration;
23 import java.util.Objects;
24 
25 /**
26  * This class encapsulates a request to a provider.
27  */
28 final class TimeZoneProviderRequest {
29 
30     @NonNull
31     private static final TimeZoneProviderRequest STOP_UPDATES =
32             new TimeZoneProviderRequest(
33                     false /* sendUpdates */,
34                     null /* initializationTimeout */);
35 
36     private final boolean mSendUpdates;
37 
38     @Nullable
39     private final Duration mInitializationTimeout;
40 
TimeZoneProviderRequest( boolean sendUpdates, @Nullable Duration initializationTimeout)41     private TimeZoneProviderRequest(
42             boolean sendUpdates, @Nullable Duration initializationTimeout) {
43         mSendUpdates = sendUpdates;
44         mInitializationTimeout = initializationTimeout;
45     }
46 
47     /** Creates a request to start updates with the specified timeout. */
createStartUpdatesRequest( @onNull Duration initializationTimeout)48     public static TimeZoneProviderRequest createStartUpdatesRequest(
49             @NonNull Duration initializationTimeout) {
50         return new TimeZoneProviderRequest(true, Objects.requireNonNull(initializationTimeout));
51     }
52 
53     /** Creates a request to stop updates. */
createStopUpdatesRequest()54     public static TimeZoneProviderRequest createStopUpdatesRequest() {
55         return STOP_UPDATES;
56     }
57 
58     /**
59      * Returns {@code true} if the provider should send updates related to the device's current
60      * time zone, {@code false} otherwise.
61      */
sendUpdates()62     public boolean sendUpdates() {
63         return mSendUpdates;
64     }
65 
66     /**
67      * Returns the maximum time that the provider is allowed to initialize before it is expected to
68      * send an event of any sort. Only valid when {@link #sendUpdates()} is {@code true}. Failure to
69      * send an event in this time (with some fuzz) may be interpreted as if the provider is
70      * uncertain of the time zone, and/or it could lead to the provider being stopped.
71      */
72     @Nullable
getInitializationTimeout()73     public Duration getInitializationTimeout() {
74         return mInitializationTimeout;
75     }
76 
77     @Override
equals(Object o)78     public boolean equals(Object o) {
79         if (this == o) {
80             return true;
81         }
82         if (o == null || getClass() != o.getClass()) {
83             return false;
84         }
85         TimeZoneProviderRequest
86                 that = (TimeZoneProviderRequest) o;
87         return mSendUpdates == that.mSendUpdates
88                 && mInitializationTimeout == that.mInitializationTimeout;
89     }
90 
91     @Override
hashCode()92     public int hashCode() {
93         return Objects.hash(mSendUpdates, mInitializationTimeout);
94     }
95 
96     @Override
toString()97     public String toString() {
98         return "TimeZoneProviderRequest{"
99                 + "mSendUpdates=" + mSendUpdates
100                 + ", mInitializationTimeout=" + mInitializationTimeout
101                 + "}";
102     }
103 }
104