diff options
| author | 2024-08-28 19:47:13 +0000 | |
|---|---|---|
| committer | 2024-08-28 19:47:13 +0000 | |
| commit | 160d3e44d7f7b5ef67f5443456504330d64b95fb (patch) | |
| tree | d17425fa510e49ee2e833cb79fe06bbbcb600489 | |
| parent | 29f70f3af1ae2821f906cd1b49d53decdd8b84e0 (diff) | |
| parent | fc26d5e940a0fe05df4a2fdac7af19340a1684af (diff) | |
Merge "Cache ITimeDetectorService in network time Clock" into main
| -rw-r--r-- | core/java/android/os/SystemClock.java | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/core/java/android/os/SystemClock.java b/core/java/android/os/SystemClock.java index 23bd30a21c4c..0ed1ab6c8d72 100644 --- a/core/java/android/os/SystemClock.java +++ b/core/java/android/os/SystemClock.java @@ -314,6 +314,15 @@ public final class SystemClock { } /** + * @see #currentNetworkTimeMillis(ITimeDetectorService) + * @hide + */ + public static long currentNetworkTimeMillis() { + return currentNetworkTimeMillis(ITimeDetectorService.Stub + .asInterface(ServiceManager.getService(Context.TIME_DETECTOR_SERVICE))); + } + + /** * Returns milliseconds since January 1, 1970 00:00:00.0 UTC, synchronized * using a remote network source outside the device. * <p> @@ -331,14 +340,14 @@ public final class SystemClock { * at any time. Due to network delays, variations between servers, or local * (client side) clock drift, the accuracy of the returned times cannot be * guaranteed. In extreme cases, consecutive calls to {@link - * #currentNetworkTimeMillis()} could return times that are out of order. + * #currentNetworkTimeMillis(ITimeDetectorService)} could return times that + * are out of order. * * @throws DateTimeException when no network time can be provided. * @hide */ - public static long currentNetworkTimeMillis() { - ITimeDetectorService timeDetectorService = ITimeDetectorService.Stub - .asInterface(ServiceManager.getService(Context.TIME_DETECTOR_SERVICE)); + public static long currentNetworkTimeMillis( + ITimeDetectorService timeDetectorService) { if (timeDetectorService != null) { UnixEpochTime time; try { @@ -380,16 +389,21 @@ public final class SystemClock { * at any time. Due to network delays, variations between servers, or local * (client side) clock drift, the accuracy of the returned times cannot be * guaranteed. In extreme cases, consecutive calls to {@link - * Clock#millis()} on the returned {@link Clock}could return times that are + * Clock#millis()} on the returned {@link Clock} could return times that are * out of order. * * @throws DateTimeException when no network time can be provided. */ public static @NonNull Clock currentNetworkTimeClock() { return new SimpleClock(ZoneOffset.UTC) { + private ITimeDetectorService mSvc; @Override public long millis() { - return SystemClock.currentNetworkTimeMillis(); + if (mSvc == null) { + mSvc = ITimeDetectorService.Stub + .asInterface(ServiceManager.getService(Context.TIME_DETECTOR_SERVICE)); + } + return SystemClock.currentNetworkTimeMillis(mSvc); } }; } |