• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 com.android.internal.telephony;
18 
19 import android.annotation.DurationMillisLong;
20 import android.annotation.ElapsedRealtimeLong;
21 import android.annotation.NonNull;
22 import android.os.TimestampedValue;
23 
24 import java.time.Duration;
25 import java.util.Objects;
26 
27 /** NITZ information and associated metadata. */
28 public final class NitzSignal {
29 
30     @ElapsedRealtimeLong private final long mReceiptElapsedMillis;
31     @NonNull private final NitzData mNitzData;
32     @DurationMillisLong private final long mAgeMillis;
33 
34     /**
35      * @param receiptElapsedMillis the time according to {@link
36      *     android.os.SystemClock#elapsedRealtime()} when the NITZ signal was first received by
37      *     the platform code
38      * @param nitzData the NITZ data
39      * @param ageMillis the age of the NITZ when it was passed to the platform, e.g. if it was
40      *     cached by the modem for a period of time. Must not be negative.
41      */
NitzSignal( @lapsedRealtimeLong long receiptElapsedMillis, @NonNull NitzData nitzData, long ageMillis)42     public NitzSignal(
43             @ElapsedRealtimeLong long receiptElapsedMillis,
44             @NonNull NitzData nitzData,
45             long ageMillis) {
46         mReceiptElapsedMillis = receiptElapsedMillis;
47         mNitzData = Objects.requireNonNull(nitzData);
48         if (ageMillis < 0) {
49             throw new IllegalArgumentException("ageMillis < 0");
50         }
51         mAgeMillis = ageMillis;
52     }
53 
54     /**
55      * Returns the time according to {@link android.os.SystemClock#elapsedRealtime()} when the NITZ
56      * signal was first received by the platform code.
57      */
58     @ElapsedRealtimeLong
getReceiptElapsedRealtimeMillis()59     public long getReceiptElapsedRealtimeMillis() {
60         return mReceiptElapsedMillis;
61     }
62 
63     /**
64      * Returns the NITZ data.
65      */
66     @NonNull
getNitzData()67     public NitzData getNitzData() {
68         return mNitzData;
69     }
70 
71     /**
72      * Returns the age of the NITZ when it was passed to the platform, e.g. if it was cached by the
73      * modem for a period of time. Must not be negative.
74      */
75     @DurationMillisLong
getAgeMillis()76     public long getAgeMillis() {
77         return mAgeMillis;
78     }
79 
80     /**
81      * Returns a derived property of {@code receiptElapsedMillis - ageMillis}, i.e. the time
82      * according to the elapsed realtime clock when the NITZ signal was actually received by this
83      * device taking into time it was cached by layers before the RIL.
84      */
85     @ElapsedRealtimeLong
getAgeAdjustedElapsedRealtimeMillis()86     public long getAgeAdjustedElapsedRealtimeMillis() {
87         return mReceiptElapsedMillis - mAgeMillis;
88     }
89 
90     /**
91      * Creates a {@link android.os.TimestampedValue} containing the UTC time as the number of
92      * milliseconds since the start of the Unix epoch. The reference time is the time according to
93      * the elapsed realtime clock when that would have been the time, accounting for receipt time
94      * and age.
95      */
createTimeSignal()96     public TimestampedValue<Long> createTimeSignal() {
97         return new TimestampedValue<>(
98                 getAgeAdjustedElapsedRealtimeMillis(),
99                 getNitzData().getCurrentTimeInMillis());
100     }
101 
102     @Override
equals(Object o)103     public boolean equals(Object o) {
104         if (this == o) {
105             return true;
106         }
107         if (o == null || getClass() != o.getClass()) {
108             return false;
109         }
110         NitzSignal that = (NitzSignal) o;
111         return mReceiptElapsedMillis == that.mReceiptElapsedMillis
112                 && mAgeMillis == that.mAgeMillis
113                 && mNitzData.equals(that.mNitzData);
114     }
115 
116     @Override
hashCode()117     public int hashCode() {
118         return Objects.hash(mReceiptElapsedMillis, mNitzData, mAgeMillis);
119     }
120 
121     @Override
toString()122     public String toString() {
123         return "NitzSignal{"
124                 + "mReceiptElapsedMillis=" + Duration.ofMillis(mReceiptElapsedMillis)
125                 + ", mNitzData=" + mNitzData
126                 + ", mAgeMillis=" + mAgeMillis
127                 + '}';
128     }
129 }
130