Debian Codegraph @

core/java/android/app/timedetector/TimeSuggestionHelper.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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.timedetector;
18
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.ShellCommand;
23 import android.os.TimestampedValue;
24
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Objects;
31
32 /**
33 * A delegate class to support time suggestion classes that could diverge in the future. This class
34 * exists purely for code re-use and provides support methods. It avoids class inheritance
35 * deliberately to allow each suggestion to evolve in different directions later without affecting
36 * SDK APIs.
37 *
38 * <p>{@code unixEpochTime} is the suggested time. The {@code unixEpochTime.value} is the number of
39 * milliseconds elapsed since 1/1/1970 00:00:00 UTC according to the Unix time system. The {@code
40 * unixEpochTime.referenceTimeMillis} is the value of the elapsed realtime clock when the {@code
41 * unixEpochTime.value} was established. Note that the elapsed realtime clock is considered accurate
42 * but it is volatile, so time suggestions cannot be persisted across device resets.
43 *
44 * <p>{@code debugInfo} contains debugging metadata associated with the suggestion. This is used to
45 * record why the suggestion exists and how it was entered. This information exists only to aid in
46 * debugging and therefore is used by {@link #toString()}, but it is not for use in detection
47 * logic and is not considered in {@link #hashCode()} or {@link #equals(Object)}.
48 *
49 * @hide
50 */
51 public final class TimeSuggestionHelper {
52
53 @NonNull private final Class<?> mHelpedClass;
54 @NonNull private final TimestampedValue<Long> mUnixEpochTime;
55 @Nullable private ArrayList<String> mDebugInfo;
56
57 /** Creates a helper for the specified class, containing the supplied properties. */
58 public TimeSuggestionHelper(@NonNull Class<?> helpedClass,
59 @NonNull TimestampedValue<Long> unixEpochTime) {
60 mHelpedClass = Objects.requireNonNull(helpedClass);
61 mUnixEpochTime = Objects.requireNonNull(unixEpochTime);
62 Objects.requireNonNull(unixEpochTime.getValue());
63 }
64
65 /** See {@link TimeSuggestionHelper} for property details. */
66 @NonNull
67 public TimestampedValue<Long> getUnixEpochTime() {
68 return mUnixEpochTime;
69 }
70
71 /** See {@link TimeSuggestionHelper} for information about {@code debugInfo}. */
72 @NonNull
73 public List<String> getDebugInfo() {
74 return mDebugInfo == null
75 ? Collections.emptyList() : Collections.unmodifiableList(mDebugInfo);
76 }
77
78 /**
79 * Associates information with the instance that can be useful for debugging / logging.
80 *
81 * <p>See {@link TimeSuggestionHelper} for more information about {@code debugInfo}.
82 */
83 public void addDebugInfo(@NonNull String debugInfo) {
84 if (mDebugInfo == null) {
85 mDebugInfo = new ArrayList<>();
86 }
87 mDebugInfo.add(debugInfo);
88 }
89
90 /**
91 * Associates information with the instance that can be useful for debugging / logging. The
92 * information is present in {@link #toString()} but is not considered for
93 * {@link #equals(Object)} and {@link #hashCode()}.
94 */
95 public void addDebugInfo(String... debugInfos) {
96 addDebugInfo(Arrays.asList(debugInfos));
97 }
98
99 /**
100 * Associates information with the instance that can be useful for debugging / logging.
101 *
102 * <p>See {@link TimeSuggestionHelper} for more information about {@code debugInfo}.
103 */
104 public void addDebugInfo(@NonNull List<String> debugInfo) {
105 if (mDebugInfo == null) {
106 mDebugInfo = new ArrayList<>(debugInfo.size());
107 }
108 mDebugInfo.addAll(debugInfo);
109 }
110
111 /**
112 * Implemented in case users call this insteam of {@link #handleEquals(TimeSuggestionHelper)}.
113 */
114 @Override
115 public boolean equals(Object o) {
116 if (this == o) {
117 return true;
118 }
119 if (o == null || getClass() != o.getClass()) {
120 return false;
121 }
122 TimeSuggestionHelper that = (TimeSuggestionHelper) o;
123 return handleEquals(that);
124 }
125
126 /** Used to implement {@link Object#equals(Object)}. */
127 public boolean handleEquals(TimeSuggestionHelper o) {
128 return Objects.equals(mHelpedClass, o.mHelpedClass)
129 && Objects.equals(mUnixEpochTime, o.mUnixEpochTime);
130 }
131
132 @Override
133 public int hashCode() {
134 return Objects.hash(mUnixEpochTime);
135 }
136
137 /** Used to implement {@link Object#toString()}. */
138 public String handleToString() {
139 return mHelpedClass.getSimpleName() + "{"
140 + "mUnixEpochTime=" + mUnixEpochTime
141 + ", mDebugInfo=" + mDebugInfo
142 + '}';
143 }
144
145 /** Constructs a helper with suggestion state from a Parcel. */
146 public static TimeSuggestionHelper handleCreateFromParcel(@NonNull Class<?> helpedClass,
147 @NonNull Parcel in) {
148 @SuppressWarnings("unchecked")
149 TimestampedValue<Long> unixEpochTime = in.readParcelable(
150 null /* classLoader */, TimestampedValue.class);
151 TimeSuggestionHelper suggestionHelper =
152 new TimeSuggestionHelper(helpedClass, unixEpochTime);
153 suggestionHelper.mDebugInfo = in.readArrayList(null /* classLoader */, String.class);
154 return suggestionHelper;
155 }
156
157 /** Writes the helper suggestion state to a Parcel. */
158 public void handleWriteToParcel(@NonNull Parcel dest, int flags) {
159 dest.writeParcelable(mUnixEpochTime, 0);
160 dest.writeList(mDebugInfo);
161 }
162
163 /** Parses command line args to create a {@link TimeSuggestionHelper}. */
164 public static TimeSuggestionHelper handleParseCommandLineArg(
165 @NonNull Class<?> helpedClass, @NonNull ShellCommand cmd)
166 throws IllegalArgumentException {
167 Long referenceTimeMillis = null;
168 Long unixEpochTimeMillis = null;
169 String opt;
170 while ((opt = cmd.getNextArg()) != null) {
171 switch (opt) {
172 case "--reference_time": {
173 referenceTimeMillis = Long.parseLong(cmd.getNextArgRequired());
174 break;
175 }
176 case "--unix_epoch_time": {
177 unixEpochTimeMillis = Long.parseLong(cmd.getNextArgRequired());
178 break;
179 }
180 default: {
181 throw new IllegalArgumentException("Unknown option: " + opt);
182 }
183 }
184 }
185
186 if (referenceTimeMillis == null) {
187 throw new IllegalArgumentException("No referenceTimeMillis specified.");
188 }
189 if (unixEpochTimeMillis == null) {
190 throw new IllegalArgumentException("No unixEpochTimeMillis specified.");
191 }
192
193 TimestampedValue<Long> timeSignal =
194 new TimestampedValue<>(referenceTimeMillis, unixEpochTimeMillis);
195 TimeSuggestionHelper suggestionHelper = new TimeSuggestionHelper(helpedClass, timeSignal);
196 suggestionHelper.addDebugInfo("Command line injection");
197 return suggestionHelper;
198 }
199
200 /** Prints the command line args needed to create a {@link TimeSuggestionHelper}. */
201 public static void handlePrintCommandLineOpts(
202 @NonNull PrintWriter pw, @NonNull String typeName, @NonNull Class<?> clazz) {
203 pw.printf("%s suggestion options:\n", typeName);
204 pw.println(" --reference_time <elapsed realtime millis>");
205 pw.println(" --unix_epoch_time <Unix epoch time millis>");
206 pw.println();
207 pw.println("See " + clazz.getName() + " for more information");
208 }
209 }