Debian Codegraph @

core/java/android/app/time/ExternalTimeSuggestion.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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.app.time;
18
19 import android.annotation.CurrentTimeMillisLong;
20 import android.annotation.ElapsedRealtimeLong;
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.app.timedetector.TimeSuggestionHelper;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 import android.os.ShellCommand;
27 import android.os.TimestampedValue;
28
29 import java.io.PrintWriter;
30 import java.util.List;
31 import java.util.Objects;
32
33 /**
34 * A time signal from an External source.
35 *
36 * <p>External time suggestions are for use in situations where the Android device is part of a
37 * wider network of devices that are required to use a single time source, and where authority for
38 * the time is external to the Android device. For example, for the Android Auto use case where the
39 * Android device is part of a wider in-car network of devices that should display the same time.
40 *
41 * <p>Android allows for a single external source for time. If there are several external sources
42 * then it is left to the caller to prioritize / filter accordingly to ensure consistency.
43 *
44 * <p>External is one of several time "origins" that the Android platform supports. Stock Android
45 * allows for configuration of which origins can be used and the prioritization between them. Until
46 * an external suggestion is made, the Android device may use its own RTC to initialize the system
47 * clock during boot, and then accept suggestions from the configured origins.
48 *
49 * <p>The creator of an external suggestion is expected to be separate Android process, e.g. a
50 * process integrating with the external time source via a HAL or local network. The creator must
51 * capture the elapsed realtime reference clock, e.g. via {@link SystemClock#elapsedRealtime()},
52 * when the Unix epoch time is first obtained (usually under a wakelock). This enables Android to
53 * adjust for latency introduced between suggestion creation and eventual use. Adjustments for other
54 * sources of latency, i.e. those before the external time suggestion is created, must be handled by
55 * the creator.
56 *
57 * <p>{@code elapsedRealtimeMillis} and {@code suggestionMillis} represent the suggested time.
58 * {@code suggestionMillis} is the number of milliseconds elapsed since 1/1/1970 00:00:00 UTC
59 * according to the Unix time scale. {@code elapsedRealtimeMillis} is the value of the elapsed
60 * realtime clock when {@code suggestionMillis} was established. Note that the elapsed realtime
61 * clock is considered accurate but it is volatile, so time suggestions cannot be persisted across
62 * device resets.
63 *
64 * <p>{@code debugInfo} contains debugging metadata associated with the suggestion. This is used to
65 * record why the suggestion exists and how it was entered. This information exists only to aid in
66 * debugging and therefore is used by {@link #toString()}, but it is not for use in detection logic
67 * and is not considered in {@link #hashCode()} or {@link #equals(Object)}.
68 *
69 * @hide
70 */
71 @SystemApi
72 public final class ExternalTimeSuggestion implements Parcelable {
73
74 public static final @NonNull Creator<ExternalTimeSuggestion> CREATOR =
75 new Creator<ExternalTimeSuggestion>() {
76 public ExternalTimeSuggestion createFromParcel(Parcel in) {
77 TimeSuggestionHelper helper = TimeSuggestionHelper.handleCreateFromParcel(
78 ExternalTimeSuggestion.class, in);
79 return new ExternalTimeSuggestion(helper);
80 }
81
82 public ExternalTimeSuggestion[] newArray(int size) {
83 return new ExternalTimeSuggestion[size];
84 }
85 };
86
87 @NonNull private final TimeSuggestionHelper mTimeSuggestionHelper;
88
89 /**
90 * Creates a time suggestion cross-referenced to the elapsed realtime clock. See {@link
91 * ExternalTimeSuggestion} for more details.
92 *
93 * @param elapsedRealtimeMillis the elapsed realtime clock reference for the suggestion
94 * @param suggestionMillis the suggested time in milliseconds since the start of the
95 * Unix epoch
96 */
97 public ExternalTimeSuggestion(@ElapsedRealtimeLong long elapsedRealtimeMillis,
98 @CurrentTimeMillisLong long suggestionMillis) {
99 mTimeSuggestionHelper = new TimeSuggestionHelper(ExternalTimeSuggestion.class,
100 new TimestampedValue<>(elapsedRealtimeMillis, suggestionMillis));
101 }
102
103 private ExternalTimeSuggestion(@NonNull TimeSuggestionHelper helper) {
104 mTimeSuggestionHelper = Objects.requireNonNull(helper);
105 }
106
107 @Override
108 public int describeContents() {
109 return 0;
110 }
111
112 @Override
113 public void writeToParcel(@NonNull Parcel dest, int flags) {
114 mTimeSuggestionHelper.handleWriteToParcel(dest, flags);
115 }
116
117 /**
118 * {@hide}
119 */
120 @NonNull
121 public TimestampedValue<Long> getUnixEpochTime() {
122 return mTimeSuggestionHelper.getUnixEpochTime();
123 }
124
125 /**
126 * Returns information that can be useful for debugging / logging. See {@link #addDebugInfo}.
127 */
128 @NonNull
129 public List<String> getDebugInfo() {
130 return mTimeSuggestionHelper.getDebugInfo();
131 }
132
133 /**
134 * Associates information with the instance that can be useful for debugging / logging. The
135 * information is present in {@link #toString()} but is not considered for {@link
136 * #equals(Object)} and {@link #hashCode()}.
137 */
138 public void addDebugInfo(@NonNull String... debugInfos) {
139 mTimeSuggestionHelper.addDebugInfo(debugInfos);
140 }
141
142 @Override
143 public boolean equals(Object o) {
144 if (this == o) {
145 return true;
146 }
147 if (o == null || getClass() != o.getClass()) {
148 return false;
149 }
150 ExternalTimeSuggestion that = (ExternalTimeSuggestion) o;
151 return mTimeSuggestionHelper.handleEquals(that.mTimeSuggestionHelper);
152 }
153
154 @Override
155 public int hashCode() {
156 return mTimeSuggestionHelper.hashCode();
157 }
158
159 @Override
160 public String toString() {
161 return mTimeSuggestionHelper.handleToString();
162 }
163
164 /** @hide */
165 public static ExternalTimeSuggestion parseCommandLineArg(@NonNull ShellCommand cmd)
166 throws IllegalArgumentException {
167 return new ExternalTimeSuggestion(
168 TimeSuggestionHelper.handleParseCommandLineArg(ExternalTimeSuggestion.class, cmd));
169 }
170
171 /** @hide */
172 public static void printCommandLineOpts(PrintWriter pw) {
173 TimeSuggestionHelper.handlePrintCommandLineOpts(
174 pw, "External", ExternalTimeSuggestion.class);
175 }
176 }