| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.content.ContentResolver; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.res.Resources; |
| 25 |
import android.net.ConnectivityManager; |
| 26 |
import android.net.Network; |
| 27 |
import android.net.NetworkInfo; |
| 28 |
import android.net.SntpClient; |
| 29 |
import android.os.Build; |
| 30 |
import android.os.SystemClock; |
| 31 |
import android.provider.Settings; |
| 32 |
import android.text.TextUtils; |
| 33 |
|
| 34 |
import com.android.internal.annotations.GuardedBy; |
| 35 |
|
| 36 |
import java.io.PrintWriter; |
| 37 |
import java.time.Duration; |
| 38 |
import java.time.Instant; |
| 39 |
import java.util.Objects; |
| 40 |
import java.util.function.Supplier; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class NtpTrustedTime implements TrustedTime { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static class TimeResult { |
| 58 |
private final long mTimeMillis; |
| 59 |
private final long mElapsedRealtimeMillis; |
| 60 |
private final long mCertaintyMillis; |
| 61 |
|
| 62 |
public TimeResult(long timeMillis, long elapsedRealtimeMillis, long certaintyMillis) { |
| 63 |
mTimeMillis = timeMillis; |
| 64 |
mElapsedRealtimeMillis = elapsedRealtimeMillis; |
| 65 |
mCertaintyMillis = certaintyMillis; |
| 66 |
} |
| 67 |
|
| 68 |
public long getTimeMillis() { |
| 69 |
return mTimeMillis; |
| 70 |
} |
| 71 |
|
| 72 |
public long getElapsedRealtimeMillis() { |
| 73 |
return mElapsedRealtimeMillis; |
| 74 |
} |
| 75 |
|
| 76 |
public long getCertaintyMillis() { |
| 77 |
return mCertaintyMillis; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
public long currentTimeMillis() { |
| 82 |
return mTimeMillis + getAgeMillis(); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
public long getAgeMillis() { |
| 87 |
return getAgeMillis(SystemClock.elapsedRealtime()); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public long getAgeMillis(long currentElapsedRealtimeMillis) { |
| 96 |
return currentElapsedRealtimeMillis - mElapsedRealtimeMillis; |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public String toString() { |
| 101 |
return "TimeResult{" |
| 102 |
+ "mTimeMillis=" + Instant.ofEpochMilli(mTimeMillis) |
| 103 |
+ ", mElapsedRealtimeMillis=" + Duration.ofMillis(mElapsedRealtimeMillis) |
| 104 |
+ ", mCertaintyMillis=" + mCertaintyMillis |
| 105 |
+ '}'; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
private static final String TAG = "NtpTrustedTime"; |
| 110 |
private static final boolean LOGD = false; |
| 111 |
|
| 112 |
private static NtpTrustedTime sSingleton; |
| 113 |
|
| 114 |
@NonNull |
| 115 |
private final Context mContext; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private final Supplier<ConnectivityManager> mConnectivityManagerSupplier = |
| 122 |
new Supplier<ConnectivityManager>() { |
| 123 |
private ConnectivityManager mConnectivityManager; |
| 124 |
|
| 125 |
@Nullable |
| 126 |
@Override |
| 127 |
public synchronized ConnectivityManager get() { |
| 128 |
|
| 129 |
|
| 130 |
if (mConnectivityManager == null) { |
| 131 |
mConnectivityManager = mContext.getSystemService(ConnectivityManager.class); |
| 132 |
} |
| 133 |
return mConnectivityManager; |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
@Nullable |
| 139 |
private String mHostnameForTests; |
| 140 |
|
| 141 |
|
| 142 |
@Nullable |
| 143 |
private Integer mPortForTests; |
| 144 |
|
| 145 |
|
| 146 |
@Nullable |
| 147 |
private Duration mTimeoutForTests; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
private volatile TimeResult mTimeResult; |
| 152 |
|
| 153 |
private NtpTrustedTime(Context context) { |
| 154 |
mContext = Objects.requireNonNull(context); |
| 155 |
} |
| 156 |
|
| 157 |
@UnsupportedAppUsage |
| 158 |
public static synchronized NtpTrustedTime getInstance(Context context) { |
| 159 |
if (sSingleton == null) { |
| 160 |
Context appContext = context.getApplicationContext(); |
| 161 |
sSingleton = new NtpTrustedTime(appContext); |
| 162 |
} |
| 163 |
return sSingleton; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public void setServerConfigForTests( |
| 171 |
@Nullable String hostname, @Nullable Integer port, @Nullable Duration timeout) { |
| 172 |
synchronized (this) { |
| 173 |
mHostnameForTests = hostname; |
| 174 |
mPortForTests = port; |
| 175 |
mTimeoutForTests = timeout; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 180 |
public boolean forceRefresh() { |
| 181 |
synchronized (this) { |
| 182 |
NtpConnectionInfo connectionInfo = getNtpConnectionInfo(); |
| 183 |
if (connectionInfo == null) { |
| 184 |
|
| 185 |
if (LOGD) Log.d(TAG, "forceRefresh: invalid server config"); |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
ConnectivityManager connectivityManager = mConnectivityManagerSupplier.get(); |
| 190 |
if (connectivityManager == null) { |
| 191 |
if (LOGD) Log.d(TAG, "forceRefresh: no ConnectivityManager"); |
| 192 |
return false; |
| 193 |
} |
| 194 |
final Network network = connectivityManager.getActiveNetwork(); |
| 195 |
final NetworkInfo ni = connectivityManager.getNetworkInfo(network); |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
if (ni == null || !ni.isConnected()) { |
| 207 |
if (LOGD) Log.d(TAG, "forceRefresh: no connectivity"); |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
if (LOGD) Log.d(TAG, "forceRefresh() from cache miss"); |
| 212 |
final SntpClient client = new SntpClient(); |
| 213 |
final String serverName = connectionInfo.getServer(); |
| 214 |
final int port = connectionInfo.getPort(); |
| 215 |
final int timeoutMillis = connectionInfo.getTimeoutMillis(); |
| 216 |
if (client.requestTime(serverName, port, timeoutMillis, network)) { |
| 217 |
long ntpCertainty = client.getRoundTripTime() / 2; |
| 218 |
mTimeResult = new TimeResult( |
| 219 |
client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty); |
| 220 |
return true; |
| 221 |
} else { |
| 222 |
return false; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
@Deprecated |
| 233 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 234 |
public boolean hasCache() { |
| 235 |
return mTimeResult != null; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
@Deprecated |
| 244 |
@Override |
| 245 |
public long getCacheAge() { |
| 246 |
TimeResult timeResult = mTimeResult; |
| 247 |
if (timeResult != null) { |
| 248 |
return SystemClock.elapsedRealtime() - timeResult.getElapsedRealtimeMillis(); |
| 249 |
} else { |
| 250 |
return Long.MAX_VALUE; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
@Deprecated |
| 260 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 261 |
public long currentTimeMillis() { |
| 262 |
TimeResult timeResult = mTimeResult; |
| 263 |
if (timeResult == null) { |
| 264 |
throw new IllegalStateException("Missing authoritative time source"); |
| 265 |
} |
| 266 |
if (LOGD) Log.d(TAG, "currentTimeMillis() cache hit"); |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
return timeResult.currentTimeMillis(); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
@Deprecated |
| 279 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 280 |
public long getCachedNtpTime() { |
| 281 |
if (LOGD) Log.d(TAG, "getCachedNtpTime() cache hit"); |
| 282 |
TimeResult timeResult = mTimeResult; |
| 283 |
return timeResult == null ? 0 : timeResult.getTimeMillis(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
@Deprecated |
| 292 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 293 |
public long getCachedNtpTimeReference() { |
| 294 |
TimeResult timeResult = mTimeResult; |
| 295 |
return timeResult == null ? 0 : timeResult.getElapsedRealtimeMillis(); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
@Nullable |
| 303 |
public TimeResult getCachedTimeResult() { |
| 304 |
return mTimeResult; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
public void clearCachedTimeResult() { |
| 309 |
synchronized (this) { |
| 310 |
mTimeResult = null; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
private static class NtpConnectionInfo { |
| 315 |
|
| 316 |
@NonNull private final String mServer; |
| 317 |
private final int mPort; |
| 318 |
private final int mTimeoutMillis; |
| 319 |
|
| 320 |
NtpConnectionInfo(@NonNull String server, int port, int timeoutMillis) { |
| 321 |
mServer = Objects.requireNonNull(server); |
| 322 |
mPort = port; |
| 323 |
mTimeoutMillis = timeoutMillis; |
| 324 |
} |
| 325 |
|
| 326 |
@NonNull |
| 327 |
public String getServer() { |
| 328 |
return mServer; |
| 329 |
} |
| 330 |
|
| 331 |
@NonNull |
| 332 |
public int getPort() { |
| 333 |
return mPort; |
| 334 |
} |
| 335 |
|
| 336 |
int getTimeoutMillis() { |
| 337 |
return mTimeoutMillis; |
| 338 |
} |
| 339 |
|
| 340 |
@Override |
| 341 |
public String toString() { |
| 342 |
return "NtpConnectionInfo{" |
| 343 |
+ "mServer='" + mServer + '\'' |
| 344 |
+ ", mPort='" + mPort + '\'' |
| 345 |
+ ", mTimeoutMillis=" + mTimeoutMillis |
| 346 |
+ '}'; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
@GuardedBy("this") |
| 351 |
private NtpConnectionInfo getNtpConnectionInfo() { |
| 352 |
final ContentResolver resolver = mContext.getContentResolver(); |
| 353 |
|
| 354 |
final Resources res = mContext.getResources(); |
| 355 |
|
| 356 |
final String hostname; |
| 357 |
if (mHostnameForTests != null) { |
| 358 |
hostname = mHostnameForTests; |
| 359 |
} else { |
| 360 |
String serverGlobalSetting = |
| 361 |
Settings.Global.getString(resolver, Settings.Global.NTP_SERVER); |
| 362 |
if (serverGlobalSetting != null) { |
| 363 |
hostname = serverGlobalSetting; |
| 364 |
} else { |
| 365 |
hostname = res.getString(com.android.internal.R.string.config_ntpServer); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
final Integer port; |
| 370 |
if (mPortForTests != null) { |
| 371 |
port = mPortForTests; |
| 372 |
} else { |
| 373 |
port = SntpClient.STANDARD_NTP_PORT; |
| 374 |
} |
| 375 |
|
| 376 |
final int timeoutMillis; |
| 377 |
if (mTimeoutForTests != null) { |
| 378 |
timeoutMillis = (int) mTimeoutForTests.toMillis(); |
| 379 |
} else { |
| 380 |
int defaultTimeoutMillis = |
| 381 |
res.getInteger(com.android.internal.R.integer.config_ntpTimeout); |
| 382 |
timeoutMillis = Settings.Global.getInt( |
| 383 |
resolver, Settings.Global.NTP_TIMEOUT, defaultTimeoutMillis); |
| 384 |
} |
| 385 |
return TextUtils.isEmpty(hostname) ? null : |
| 386 |
new NtpConnectionInfo(hostname, port, timeoutMillis); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
public void dump(PrintWriter pw) { |
| 391 |
synchronized (this) { |
| 392 |
pw.println("getNtpConnectionInfo()=" + getNtpConnectionInfo()); |
| 393 |
pw.println("mTimeResult=" + mTimeResult); |
| 394 |
if (mTimeResult != null) { |
| 395 |
pw.println("mTimeResult.getAgeMillis()=" |
| 396 |
+ Duration.ofMillis(mTimeResult.getAgeMillis())); |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
} |