blob: 233dbbc42f504c39287adf1ef9f1a39fe8f34027 [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/**
Neil Fuller568fd892019-11-20 14:39:06 +000032 * A time signal from a telephony source. The value can be {@code null} to indicate that the
33 * telephony source has entered an "un-opinionated" state and any previously sent suggestions are
34 * being withdrawn. When not {@code null}, the value consists of the number of milliseconds elapsed
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010035 * since 1/1/1970 00:00:00 UTC and the time according to the elapsed realtime clock when that number
36 * was established. The elapsed realtime clock is considered accurate but volatile, so time signals
37 * must not be persisted across device resets.
38 *
39 * @hide
40 */
41public final class PhoneTimeSuggestion implements Parcelable {
42
43 public static final @NonNull Parcelable.Creator<PhoneTimeSuggestion> CREATOR =
44 new Parcelable.Creator<PhoneTimeSuggestion>() {
45 public PhoneTimeSuggestion createFromParcel(Parcel in) {
46 return PhoneTimeSuggestion.createFromParcel(in);
47 }
48
49 public PhoneTimeSuggestion[] newArray(int size) {
50 return new PhoneTimeSuggestion[size];
51 }
52 };
53
54 private final int mPhoneId;
Neil Fuller568fd892019-11-20 14:39:06 +000055 @Nullable private TimestampedValue<Long> mUtcTime;
56 @Nullable private ArrayList<String> mDebugInfo;
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010057
Neil Fuller568fd892019-11-20 14:39:06 +000058 public PhoneTimeSuggestion(int phoneId) {
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010059 mPhoneId = phoneId;
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010060 }
61
62 private static PhoneTimeSuggestion createFromParcel(Parcel in) {
63 int phoneId = in.readInt();
Neil Fuller568fd892019-11-20 14:39:06 +000064 PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId);
65 suggestion.setUtcTime(in.readParcelable(null /* classLoader */));
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010066 @SuppressWarnings("unchecked")
67 ArrayList<String> debugInfo = (ArrayList<String>) in.readArrayList(null /* classLoader */);
68 suggestion.mDebugInfo = debugInfo;
69 return suggestion;
70 }
71
72 @Override
73 public int describeContents() {
74 return 0;
75 }
76
77 @Override
78 public void writeToParcel(@NonNull Parcel dest, int flags) {
79 dest.writeInt(mPhoneId);
80 dest.writeParcelable(mUtcTime, 0);
81 dest.writeList(mDebugInfo);
82 }
83
84 public int getPhoneId() {
85 return mPhoneId;
86 }
87
Neil Fuller568fd892019-11-20 14:39:06 +000088 public void setUtcTime(@Nullable TimestampedValue<Long> utcTime) {
89 mUtcTime = utcTime;
90 }
91
92 @Nullable
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010093 public TimestampedValue<Long> getUtcTime() {
94 return mUtcTime;
95 }
96
97 @NonNull
98 public List<String> getDebugInfo() {
99 return Collections.unmodifiableList(mDebugInfo);
100 }
101
102 /**
103 * Associates information with the instance that can be useful for debugging / logging. The
104 * information is present in {@link #toString()} but is not considered for
105 * {@link #equals(Object)} and {@link #hashCode()}.
106 */
107 public void addDebugInfo(String... debugInfos) {
108 if (mDebugInfo == null) {
109 mDebugInfo = new ArrayList<>();
110 }
111 mDebugInfo.addAll(Arrays.asList(debugInfos));
112 }
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 PhoneTimeSuggestion that = (PhoneTimeSuggestion) o;
123 return mPhoneId == that.mPhoneId
124 && Objects.equals(mUtcTime, that.mUtcTime);
125 }
126
127 @Override
128 public int hashCode() {
129 return Objects.hash(mPhoneId, mUtcTime);
130 }
131
132 @Override
133 public String toString() {
134 return "PhoneTimeSuggestion{"
135 + "mPhoneId='" + mPhoneId + '\''
136 + ", mUtcTime=" + mUtcTime
137 + ", mDebugInfo=" + mDebugInfo
138 + '}';
139 }
140}