blob: e7d619a2760781f6cec21fa439638d76a3dad87d [file] [log] [blame]
Neil Fuller3aedd492019-11-23 11:33:57 +00001/*
2 * Copyright (C) 2019 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
17package android.app.timedetector;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.util.TimestampedValue;
24
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.Collections;
28import java.util.List;
29import java.util.Objects;
30
31/**
32 * A time signal from a manual (user provided) source. The value consists of the number of
33 * milliseconds elapsed since 1/1/1970 00:00:00 UTC and the time according to the elapsed realtime
34 * clock when that number was established. The elapsed realtime clock is considered accurate but
35 * volatile, so time signals must not be persisted across device resets.
36 *
37 * @hide
38 */
39public final class ManualTimeSuggestion implements Parcelable {
40
41 public static final @NonNull Creator<ManualTimeSuggestion> CREATOR =
42 new Creator<ManualTimeSuggestion>() {
43 public ManualTimeSuggestion createFromParcel(Parcel in) {
44 return ManualTimeSuggestion.createFromParcel(in);
45 }
46
47 public ManualTimeSuggestion[] newArray(int size) {
48 return new ManualTimeSuggestion[size];
49 }
50 };
51
52 @NonNull
53 private final TimestampedValue<Long> mUtcTime;
54 @Nullable
55 private ArrayList<String> mDebugInfo;
56
57 public ManualTimeSuggestion(@NonNull TimestampedValue<Long> utcTime) {
58 mUtcTime = Objects.requireNonNull(utcTime);
59 }
60
61 private static ManualTimeSuggestion createFromParcel(Parcel in) {
62 TimestampedValue<Long> utcTime = in.readParcelable(null /* classLoader */);
63 ManualTimeSuggestion suggestion = new ManualTimeSuggestion(utcTime);
64 @SuppressWarnings("unchecked")
65 ArrayList<String> debugInfo = (ArrayList<String>) in.readArrayList(null /* classLoader */);
66 suggestion.mDebugInfo = debugInfo;
67 return suggestion;
68 }
69
70 @Override
71 public int describeContents() {
72 return 0;
73 }
74
75 @Override
76 public void writeToParcel(@NonNull Parcel dest, int flags) {
77 dest.writeParcelable(mUtcTime, 0);
78 dest.writeList(mDebugInfo);
79 }
80
81 @NonNull
82 public TimestampedValue<Long> getUtcTime() {
83 return mUtcTime;
84 }
85
86 @NonNull
87 public List<String> getDebugInfo() {
88 return Collections.unmodifiableList(mDebugInfo);
89 }
90
91 /**
92 * Associates information with the instance that can be useful for debugging / logging. The
93 * information is present in {@link #toString()} but is not considered for
94 * {@link #equals(Object)} and {@link #hashCode()}.
95 */
96 public void addDebugInfo(String... debugInfos) {
97 if (mDebugInfo == null) {
98 mDebugInfo = new ArrayList<>();
99 }
100 mDebugInfo.addAll(Arrays.asList(debugInfos));
101 }
102
103 @Override
104 public boolean equals(Object o) {
105 if (this == o) {
106 return true;
107 }
108 if (o == null || getClass() != o.getClass()) {
109 return false;
110 }
111 ManualTimeSuggestion that = (ManualTimeSuggestion) o;
112 return Objects.equals(mUtcTime, that.mUtcTime);
113 }
114
115 @Override
116 public int hashCode() {
117 return Objects.hash(mUtcTime);
118 }
119
120 @Override
121 public String toString() {
122 return "ManualTimeSuggestion{"
123 + "mUtcTime=" + mUtcTime
124 + ", mDebugInfo=" + mDebugInfo
125 + '}';
126 }
127}