Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [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; |
| 23 | import android.util.TimestampedValue; |
| 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 | /** |
Neil Fuller | 568fd89 | 2019-11-20 14:39:06 +0000 | [diff] [blame^] | 32 | * 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 Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 35 | * 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 | */ |
| 41 | public 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 Fuller | 568fd89 | 2019-11-20 14:39:06 +0000 | [diff] [blame^] | 55 | @Nullable private TimestampedValue<Long> mUtcTime; |
| 56 | @Nullable private ArrayList<String> mDebugInfo; |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 57 | |
Neil Fuller | 568fd89 | 2019-11-20 14:39:06 +0000 | [diff] [blame^] | 58 | public PhoneTimeSuggestion(int phoneId) { |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 59 | mPhoneId = phoneId; |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private static PhoneTimeSuggestion createFromParcel(Parcel in) { |
| 63 | int phoneId = in.readInt(); |
Neil Fuller | 568fd89 | 2019-11-20 14:39:06 +0000 | [diff] [blame^] | 64 | PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId); |
| 65 | suggestion.setUtcTime(in.readParcelable(null /* classLoader */)); |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 66 | @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 Fuller | 568fd89 | 2019-11-20 14:39:06 +0000 | [diff] [blame^] | 88 | public void setUtcTime(@Nullable TimestampedValue<Long> utcTime) { |
| 89 | mUtcTime = utcTime; |
| 90 | } |
| 91 | |
| 92 | @Nullable |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 93 | 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 | } |