blob: 475a4aafd929f7eb9c8b971b8a1c0da3f29dfc8d [file] [log] [blame]
Neil Fulleraf3eeaf2019-10-15 14:37:37 +01001/*
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 telephony source. The value consists of the number of milliseconds elapsed
33 * since 1/1/1970 00:00:00 UTC and the time according to the elapsed realtime clock when that number
34 * was established. The elapsed realtime clock is considered accurate but volatile, so time signals
35 * must not be persisted across device resets.
36 *
37 * @hide
38 */
39public final class PhoneTimeSuggestion implements Parcelable {
40
41 public static final @NonNull Parcelable.Creator<PhoneTimeSuggestion> CREATOR =
42 new Parcelable.Creator<PhoneTimeSuggestion>() {
43 public PhoneTimeSuggestion createFromParcel(Parcel in) {
44 return PhoneTimeSuggestion.createFromParcel(in);
45 }
46
47 public PhoneTimeSuggestion[] newArray(int size) {
48 return new PhoneTimeSuggestion[size];
49 }
50 };
51
52 private final int mPhoneId;
53 @NonNull
54 private final TimestampedValue<Long> mUtcTime;
55 @Nullable
56 private ArrayList<String> mDebugInfo;
57
58 public PhoneTimeSuggestion(int phoneId, @NonNull TimestampedValue<Long> utcTime) {
59 mPhoneId = phoneId;
60 mUtcTime = Objects.requireNonNull(utcTime);
61 }
62
63 private static PhoneTimeSuggestion createFromParcel(Parcel in) {
64 int phoneId = in.readInt();
65 TimestampedValue<Long> utcTime = in.readParcelable(null /* classLoader */);
66 PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId, utcTime);
67 @SuppressWarnings("unchecked")
68 ArrayList<String> debugInfo = (ArrayList<String>) in.readArrayList(null /* classLoader */);
69 suggestion.mDebugInfo = debugInfo;
70 return suggestion;
71 }
72
73 @Override
74 public int describeContents() {
75 return 0;
76 }
77
78 @Override
79 public void writeToParcel(@NonNull Parcel dest, int flags) {
80 dest.writeInt(mPhoneId);
81 dest.writeParcelable(mUtcTime, 0);
82 dest.writeList(mDebugInfo);
83 }
84
85 public int getPhoneId() {
86 return mPhoneId;
87 }
88
89 @NonNull
90 public TimestampedValue<Long> getUtcTime() {
91 return mUtcTime;
92 }
93
94 @NonNull
95 public List<String> getDebugInfo() {
96 return Collections.unmodifiableList(mDebugInfo);
97 }
98
99 /**
100 * Associates information with the instance that can be useful for debugging / logging. The
101 * information is present in {@link #toString()} but is not considered for
102 * {@link #equals(Object)} and {@link #hashCode()}.
103 */
104 public void addDebugInfo(String... debugInfos) {
105 if (mDebugInfo == null) {
106 mDebugInfo = new ArrayList<>();
107 }
108 mDebugInfo.addAll(Arrays.asList(debugInfos));
109 }
110
111 @Override
112 public boolean equals(Object o) {
113 if (this == o) {
114 return true;
115 }
116 if (o == null || getClass() != o.getClass()) {
117 return false;
118 }
119 PhoneTimeSuggestion that = (PhoneTimeSuggestion) o;
120 return mPhoneId == that.mPhoneId
121 && Objects.equals(mUtcTime, that.mUtcTime);
122 }
123
124 @Override
125 public int hashCode() {
126 return Objects.hash(mPhoneId, mUtcTime);
127 }
128
129 @Override
130 public String toString() {
131 return "PhoneTimeSuggestion{"
132 + "mPhoneId='" + mPhoneId + '\''
133 + ", mUtcTime=" + mUtcTime
134 + ", mDebugInfo=" + mDebugInfo
135 + '}';
136 }
137}