Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 384434ea authored by Shai Barack's avatar Shai Barack
Browse files

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
parent 76174605
Loading
Loading
Loading
Loading
+31 −36
Original line number Diff line number Diff line
@@ -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,9 +346,12 @@ public final class SystemClock {
     * @throws DateTimeException when no network time can be provided.
     * @hide
     */
    public static long currentNetworkTimeMillis(
            ITimeDetectorService timeDetectorService) {
        if (timeDetectorService != null) {
    public static long currentNetworkTimeMillis() {
        ITimeDetectorService timeDetectorService = getITimeDetectorService();
        if (timeDetectorService == null) {
            throw new RuntimeException(new DeadSystemException());
        }

        UnixEpochTime time;
        try {
            time = timeDetectorService.latestNetworkTime();
@@ -358,17 +361,14 @@ public final class SystemClock {
        } 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 {
            throw new RuntimeException(new DeadSystemException());
        }
    }

   /**
@@ -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();
            }
        };
    }