@@ -96,6 +96,12 @@ public interface InitializationCallback {
96
96
@ GuardedBy ("valueLock" )
97
97
private static int timeoutMs = DEFAULT_TIMEOUT_MS ;
98
98
99
+ @ GuardedBy ("valueLock" )
100
+ private static long maxElapsedTimeUntilUpdateMs = C .TIME_UNSET ;
101
+
102
+ @ GuardedBy ("valueLock" )
103
+ private static long lastUpdateElapsedRealtime = C .TIME_UNSET ;
104
+
99
105
private SntpClient () {}
100
106
101
107
/** Returns the NTP host address used to retrieve {@link #getElapsedRealtimeOffsetMs()}. */
@@ -148,6 +154,24 @@ public static void setTimeoutMs(int timeoutMs) {
148
154
}
149
155
}
150
156
157
+ /**
158
+ * Sets the maximum time to elapse until the client is re-initialized, in milliseconds.
159
+ *
160
+ * The default is {@link C#TIME_UNSET} to never re-initialize.
161
+ */
162
+ public static void setMaxElapsedTimeUntilUpdateMs (long maxElapsedTimeUntilUpdateMs ) {
163
+ synchronized (valueLock ) {
164
+ SntpClient .maxElapsedTimeUntilUpdateMs = maxElapsedTimeUntilUpdateMs ;
165
+ }
166
+ }
167
+
168
+ /** Returns the maximum time to elapse until the client is re-initialized, in milliseconds. */
169
+ public static long getMaxElapsedTimeUntilUpdateMs () {
170
+ synchronized (valueLock ) {
171
+ return maxElapsedTimeUntilUpdateMs ;
172
+ }
173
+ }
174
+
151
175
/**
152
176
* Returns whether the device time offset has already been loaded.
153
177
*
@@ -156,6 +180,11 @@ public static void setTimeoutMs(int timeoutMs) {
156
180
*/
157
181
public static boolean isInitialized () {
158
182
synchronized (valueLock ) {
183
+ if (lastUpdateElapsedRealtime != C .TIME_UNSET
184
+ && maxElapsedTimeUntilUpdateMs != C .TIME_UNSET ) {
185
+ long deltaLastUpdate = SystemClock .elapsedRealtime () - lastUpdateElapsedRealtime ;
186
+ isInitialized = isInitialized && deltaLastUpdate < maxElapsedTimeUntilUpdateMs ;
187
+ }
159
188
return isInitialized ;
160
189
}
161
190
}
@@ -353,6 +382,7 @@ public void load() throws IOException {
353
382
}
354
383
long offsetMs = loadNtpTimeOffsetMs ();
355
384
synchronized (valueLock ) {
385
+ lastUpdateElapsedRealtime = SystemClock .elapsedRealtime ();
356
386
elapsedRealtimeOffsetMs = offsetMs ;
357
387
isInitialized = true ;
358
388
}
0 commit comments