From 384434eaf1b283d1a10cca0c43154ba65f1ee778 Mon Sep 17 00:00:00 2001 From: Shai Barack Date: Thu, 5 Sep 2024 17:25:29 +0000 Subject: Cache ITimeDetectorService in SystemClock Move the cache from the Clock instance to a static member of SystemClock, since many clients don't cache the Clock instance. Bug: 361329788 Change-Id: I67cd68f50a40215653226e52ba83099df52bc117 Flag: EXEMPT bugfix Test: SystemClockNetworkTimeTest --- core/java/android/os/SystemClock.java | 67 ++++++++++++++++------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/core/java/android/os/SystemClock.java b/core/java/android/os/SystemClock.java index 0ed1ab6c8d72..4c9a02c1fc49 100644 --- a/core/java/android/os/SystemClock.java +++ b/core/java/android/os/SystemClock.java @@ -109,6 +109,7 @@ public final class SystemClock { private static final String TAG = "SystemClock"; private static volatile IAlarmManager sIAlarmManager; + private static volatile ITimeDetectorService sITimeDetectorService; /** * Since {@code nanoTime()} is arbitrary, anchor our Ravenwood clocks against it. @@ -188,6 +189,14 @@ public final class SystemClock { return sIAlarmManager; } + private static ITimeDetectorService getITimeDetectorService() { + if (sITimeDetectorService == null) { + sITimeDetectorService = ITimeDetectorService.Stub + .asInterface(ServiceManager.getService(Context.TIME_DETECTOR_SERVICE)); + } + return sITimeDetectorService; + } + /** * Returns milliseconds since boot, not counting time spent in deep sleep. * @@ -313,15 +322,6 @@ public final class SystemClock { return System.nanoTime() / 1000L; } - /** - * @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. @@ -346,29 +346,29 @@ public final class SystemClock { * @throws DateTimeException when no network time can be provided. * @hide */ - public static long currentNetworkTimeMillis( - ITimeDetectorService timeDetectorService) { - if (timeDetectorService != null) { - UnixEpochTime time; - try { - time = timeDetectorService.latestNetworkTime(); - } catch (ParcelableException e) { - e.maybeRethrow(DateTimeException.class); - throw new RuntimeException(e); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - - if (time == null) { - // This is not expected. - throw new DateTimeException("Network based time is not available."); - } - long currentMillis = elapsedRealtime(); - long deltaMs = currentMillis - time.getElapsedRealtimeMillis(); - return time.getUnixEpochTimeMillis() + deltaMs; - } else { + public static long currentNetworkTimeMillis() { + ITimeDetectorService timeDetectorService = getITimeDetectorService(); + if (timeDetectorService == null) { throw new RuntimeException(new DeadSystemException()); } + + UnixEpochTime time; + try { + time = timeDetectorService.latestNetworkTime(); + } catch (ParcelableException e) { + e.maybeRethrow(DateTimeException.class); + throw new RuntimeException(e); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + if (time == null) { + // This is not expected. + throw new DateTimeException("Network based time is not available."); + } + + long currentMillis = elapsedRealtime(); + long deltaMs = currentMillis - time.getElapsedRealtimeMillis(); + return time.getUnixEpochTimeMillis() + deltaMs; } /** @@ -396,14 +396,9 @@ public final class SystemClock { */ public static @NonNull Clock currentNetworkTimeClock() { return new SimpleClock(ZoneOffset.UTC) { - private ITimeDetectorService mSvc; @Override public long millis() { - if (mSvc == null) { - mSvc = ITimeDetectorService.Stub - .asInterface(ServiceManager.getService(Context.TIME_DETECTOR_SERVICE)); - } - return SystemClock.currentNetworkTimeMillis(mSvc); + return SystemClock.currentNetworkTimeMillis(); } }; } -- cgit v1.2.3-59-g8ed1b