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

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

Merge "Cache ITimeDetectorService in SystemClock" into main

parents 7b528682 384434ea
Loading
Loading
Loading
Loading
+31 −36
Original line number Original line Diff line number Diff line
@@ -109,6 +109,7 @@ public final class SystemClock {
    private static final String TAG = "SystemClock";
    private static final String TAG = "SystemClock";


    private static volatile IAlarmManager sIAlarmManager;
    private static volatile IAlarmManager sIAlarmManager;
    private static volatile ITimeDetectorService sITimeDetectorService;


    /**
    /**
     * Since {@code nanoTime()} is arbitrary, anchor our Ravenwood clocks against it.
     * Since {@code nanoTime()} is arbitrary, anchor our Ravenwood clocks against it.
@@ -188,6 +189,14 @@ public final class SystemClock {
        return sIAlarmManager;
        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.
     * Returns milliseconds since boot, not counting time spent in deep sleep.
     *
     *
@@ -313,15 +322,6 @@ public final class SystemClock {
        return System.nanoTime() / 1000L;
        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
     * Returns milliseconds since January 1, 1970 00:00:00.0 UTC, synchronized
     * using a remote network source outside the device.
     * 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.
     * @throws DateTimeException when no network time can be provided.
     * @hide
     * @hide
     */
     */
    public static long currentNetworkTimeMillis(
    public static long currentNetworkTimeMillis() {
            ITimeDetectorService timeDetectorService) {
        ITimeDetectorService timeDetectorService = getITimeDetectorService();
        if (timeDetectorService != null) {
        if (timeDetectorService == null) {
            throw new RuntimeException(new DeadSystemException());
        }

        UnixEpochTime time;
        UnixEpochTime time;
        try {
        try {
            time = timeDetectorService.latestNetworkTime();
            time = timeDetectorService.latestNetworkTime();
@@ -358,17 +361,14 @@ public final class SystemClock {
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
            throw e.rethrowFromSystemServer();
        }
        }

        if (time == null) {
        if (time == null) {
            // This is not expected.
            // This is not expected.
            throw new DateTimeException("Network based time is not available.");
            throw new DateTimeException("Network based time is not available.");
        }
        }

        long currentMillis = elapsedRealtime();
        long currentMillis = elapsedRealtime();
        long deltaMs = currentMillis - time.getElapsedRealtimeMillis();
        long deltaMs = currentMillis - time.getElapsedRealtimeMillis();
        return time.getUnixEpochTimeMillis() + deltaMs;
        return time.getUnixEpochTimeMillis() + deltaMs;
        } else {
            throw new RuntimeException(new DeadSystemException());
        }
    }
    }


   /**
   /**
@@ -396,14 +396,9 @@ public final class SystemClock {
     */
     */
    public static @NonNull Clock currentNetworkTimeClock() {
    public static @NonNull Clock currentNetworkTimeClock() {
        return new SimpleClock(ZoneOffset.UTC) {
        return new SimpleClock(ZoneOffset.UTC) {
            private ITimeDetectorService mSvc;
            @Override
            @Override
            public long millis() {
            public long millis() {
                if (mSvc == null) {
                return SystemClock.currentNetworkTimeMillis();
                    mSvc = ITimeDetectorService.Stub
                            .asInterface(ServiceManager.getService(Context.TIME_DETECTOR_SERVICE));
                }
                return SystemClock.currentNetworkTimeMillis(mSvc);
            }
            }
        };
        };
    }
    }