Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 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.Parcelable; |
Neil Fuller | 35cc296 | 2020-01-08 16:31:44 +0000 | [diff] [blame^] | 23 | import android.os.TimestampedValue; |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Arrays; |
| 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | /** |
| 32 | * A time signal from a network time source like NTP. 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 | */ |
| 39 | public final class NetworkTimeSuggestion implements Parcelable { |
| 40 | |
| 41 | public static final @NonNull Creator<NetworkTimeSuggestion> CREATOR = |
| 42 | new Creator<NetworkTimeSuggestion>() { |
| 43 | public NetworkTimeSuggestion createFromParcel(Parcel in) { |
| 44 | return NetworkTimeSuggestion.createFromParcel(in); |
| 45 | } |
| 46 | |
| 47 | public NetworkTimeSuggestion[] newArray(int size) { |
| 48 | return new NetworkTimeSuggestion[size]; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | @NonNull |
| 53 | private final TimestampedValue<Long> mUtcTime; |
| 54 | @Nullable |
| 55 | private ArrayList<String> mDebugInfo; |
| 56 | |
| 57 | public NetworkTimeSuggestion(@NonNull TimestampedValue<Long> utcTime) { |
| 58 | mUtcTime = Objects.requireNonNull(utcTime); |
| 59 | Objects.requireNonNull(utcTime.getValue()); |
| 60 | } |
| 61 | |
| 62 | private static NetworkTimeSuggestion createFromParcel(Parcel in) { |
| 63 | TimestampedValue<Long> utcTime = in.readParcelable(null /* classLoader */); |
| 64 | NetworkTimeSuggestion suggestion = new NetworkTimeSuggestion(utcTime); |
| 65 | @SuppressWarnings("unchecked") |
| 66 | ArrayList<String> debugInfo = (ArrayList<String>) in.readArrayList(null /* classLoader */); |
| 67 | suggestion.mDebugInfo = debugInfo; |
| 68 | return suggestion; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int describeContents() { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 78 | dest.writeParcelable(mUtcTime, 0); |
| 79 | dest.writeList(mDebugInfo); |
| 80 | } |
| 81 | |
| 82 | @NonNull |
| 83 | public TimestampedValue<Long> getUtcTime() { |
| 84 | return mUtcTime; |
| 85 | } |
| 86 | |
| 87 | @NonNull |
| 88 | public List<String> getDebugInfo() { |
| 89 | return mDebugInfo == null |
| 90 | ? Collections.emptyList() : Collections.unmodifiableList(mDebugInfo); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Associates information with the instance that can be useful for debugging / logging. The |
| 95 | * information is present in {@link #toString()} but is not considered for |
| 96 | * {@link #equals(Object)} and {@link #hashCode()}. |
| 97 | */ |
| 98 | public void addDebugInfo(String... debugInfos) { |
| 99 | if (mDebugInfo == null) { |
| 100 | mDebugInfo = new ArrayList<>(); |
| 101 | } |
| 102 | mDebugInfo.addAll(Arrays.asList(debugInfos)); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean equals(Object o) { |
| 107 | if (this == o) { |
| 108 | return true; |
| 109 | } |
| 110 | if (o == null || getClass() != o.getClass()) { |
| 111 | return false; |
| 112 | } |
| 113 | NetworkTimeSuggestion that = (NetworkTimeSuggestion) o; |
| 114 | return Objects.equals(mUtcTime, that.mUtcTime); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public int hashCode() { |
| 119 | return Objects.hash(mUtcTime); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public String toString() { |
| 124 | return "NetworkTimeSuggestion{" |
| 125 | + "mUtcTime=" + mUtcTime |
| 126 | + ", mDebugInfo=" + mDebugInfo |
| 127 | + '}'; |
| 128 | } |
| 129 | } |