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

Commit 160d3e44 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Cache ITimeDetectorService in network time Clock" into main

parents 29f70f3a fc26d5e9
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);
            }
        };
    }