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

Commit fc26d5e9 authored by Shai Barack's avatar Shai Barack
Browse files

Cache ITimeDetectorService in network time Clock

This should eliminate the #1 source of redundant calls to
ServiceManager.

Bug: 361329788
Flag: EXEMPT bugfix
Test: SystemClockNetworkTimeTest
Change-Id: I7fd218b530f460e37ead9277c8056cbf469b82f1
parent 79675a50
Loading
Loading
Loading
Loading
+20 −6
Original line number Diff line number Diff line
@@ -313,6 +313,15 @@ 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.
@@ -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 {
@@ -387,9 +396,14 @@ public final class SystemClock {
     */
    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);
            }
        };
    }