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

Commit ab325f71 authored by Neil Fuller's avatar Neil Fuller Committed by Android (Google) Code Review
Browse files

Merge "Add time detector network behavior"

parents ecac3d47 20d3b3c4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -124,7 +124,7 @@ public final class UnixEpochTime implements Parcelable {
    @Override
    public String toString() {
        return "UnixEpochTime{"
                + "mElapsedRealtimeTimeMillis=" + mElapsedRealtimeMillis
                + "mElapsedRealtimeMillis=" + mElapsedRealtimeMillis
                + ", mUnixEpochTimeMillis=" + mUnixEpochTimeMillis
                + '}';
    }
+12 −0
Original line number Diff line number Diff line
@@ -72,6 +72,18 @@ public interface TimeDetector {
     */
    String SHELL_COMMAND_SUGGEST_NETWORK_TIME = "suggest_network_time";

    /**
     * A shell command that prints the current network time information.
     * @hide
     */
    String SHELL_COMMAND_GET_NETWORK_TIME = "get_network_time";

    /**
     * A shell command that clears the detector's network time information.
     * @hide
     */
    String SHELL_COMMAND_CLEAR_NETWORK_TIME = "clear_network_time";

    /**
     * A shell command that injects a GNSS time suggestion.
     * @hide
+32 −2
Original line number Diff line number Diff line
@@ -143,7 +143,7 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        return getTimeCapabilitiesAndConfig(userId);
    }

    TimeCapabilitiesAndConfig getTimeCapabilitiesAndConfig(@UserIdInt int userId) {
    private TimeCapabilitiesAndConfig getTimeCapabilitiesAndConfig(@UserIdInt int userId) {
        enforceManageTimeDetectorPermission();

        final long token = mCallerIdentityInjector.clearCallingIdentity();
@@ -163,6 +163,9 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        return updateConfiguration(callingUserId, configuration);
    }

    /**
     * Updates the user's configuration. Exposed for use by {@link TimeDetectorShellCommand}.
     */
    boolean updateConfiguration(@UserIdInt int userId, @NonNull TimeConfiguration configuration) {
        // Resolve constants like USER_CURRENT to the true user ID as needed.
        int resolvedUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
@@ -256,7 +259,7 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        }
    }

    void handleConfigurationInternalChangedOnHandlerThread() {
    private void handleConfigurationInternalChangedOnHandlerThread() {
        // Configuration has changed, but each user may have a different view of the configuration.
        // It's possible that this will cause unnecessary notifications but that shouldn't be a
        // problem.
@@ -287,6 +290,10 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        }
    }

    /**
     * Sets the system time state. See {@link TimeState} for details. For use by {@link
     * TimeDetectorShellCommand}.
     */
    void setTimeState(@NonNull TimeState timeState) {
        enforceManageTimeDetectorPermission();

@@ -353,6 +360,9 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        }
    }

    /**
     * Suggests network time with permission checks. For use by {@link TimeDetectorShellCommand}.
     */
    void suggestNetworkTime(@NonNull NetworkTimeSuggestion timeSignal) {
        enforceSuggestNetworkTimePermission();
        Objects.requireNonNull(timeSignal);
@@ -360,6 +370,23 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        mHandler.post(() -> mTimeDetectorStrategy.suggestNetworkTime(timeSignal));
    }

    /**
     * Clears the cached network time information. For use during tests to simulate when no network
     * time has been made available. For use by {@link TimeDetectorShellCommand}.
     *
     * <p>This operation takes place in the calling thread.
     */
    void clearNetworkTime() {
        enforceSuggestNetworkTimePermission();

        final long token = Binder.clearCallingIdentity();
        try {
            mTimeDetectorStrategy.clearLatestNetworkSuggestion();
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    @Override
    public UnixEpochTime latestNetworkTime() {
        NetworkTimeSuggestion suggestion = getLatestNetworkSuggestion();
@@ -388,6 +415,9 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub
        }
    }

    /**
     * Suggests GNSS time with permission checks. For use by {@link TimeDetectorShellCommand}.
     */
    void suggestGnssTime(@NonNull GnssTimeSuggestion timeSignal) {
        enforceSuggestGnssTimePermission();
        Objects.requireNonNull(timeSignal);
+22 −0
Original line number Diff line number Diff line
@@ -15,7 +15,9 @@
 */
package com.android.server.timedetector;

import static android.app.timedetector.TimeDetector.SHELL_COMMAND_CLEAR_NETWORK_TIME;
import static android.app.timedetector.TimeDetector.SHELL_COMMAND_CONFIRM_TIME;
import static android.app.timedetector.TimeDetector.SHELL_COMMAND_GET_NETWORK_TIME;
import static android.app.timedetector.TimeDetector.SHELL_COMMAND_GET_TIME_STATE;
import static android.app.timedetector.TimeDetector.SHELL_COMMAND_IS_AUTO_DETECTION_ENABLED;
import static android.app.timedetector.TimeDetector.SHELL_COMMAND_SERVICE_NAME;
@@ -70,6 +72,10 @@ class TimeDetectorShellCommand extends ShellCommand {
                return runSuggestTelephonyTime();
            case SHELL_COMMAND_SUGGEST_NETWORK_TIME:
                return runSuggestNetworkTime();
            case SHELL_COMMAND_GET_NETWORK_TIME:
                return runGetNetworkTime();
            case SHELL_COMMAND_CLEAR_NETWORK_TIME:
                return runClearNetworkTime();
            case SHELL_COMMAND_SUGGEST_GNSS_TIME:
                return runSuggestGnssTime();
            case SHELL_COMMAND_SUGGEST_EXTERNAL_TIME:
@@ -122,6 +128,18 @@ class TimeDetectorShellCommand extends ShellCommand {
                mInterface::suggestNetworkTime);
    }

    private int runGetNetworkTime() {
        NetworkTimeSuggestion networkTimeSuggestion = mInterface.getLatestNetworkSuggestion();
        final PrintWriter pw = getOutPrintWriter();
        pw.println(networkTimeSuggestion);
        return 0;
    }

    private int runClearNetworkTime() {
        mInterface.clearNetworkTime();
        return 0;
    }

    private int runSuggestGnssTime() {
        return runSuggestTime(
                () -> GnssTimeSuggestion.parseCommandLineArg(this),
@@ -196,6 +214,10 @@ class TimeDetectorShellCommand extends ShellCommand {
        pw.printf("    Sets the current time state for tests.\n");
        pw.printf("  %s <unix epoch time options>\n", SHELL_COMMAND_CONFIRM_TIME);
        pw.printf("    Tries to confirms the time, raising the confidence.\n");
        pw.printf("  %s\n", SHELL_COMMAND_GET_NETWORK_TIME);
        pw.printf("    Prints the network time information held by the detector.\n");
        pw.printf("  %s\n", SHELL_COMMAND_CLEAR_NETWORK_TIME);
        pw.printf("    Clears the network time information held by the detector.\n");
        pw.println();
        ManualTimeSuggestion.printCommandLineOpts(pw);
        pw.println();
+15 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.timedetector;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.time.ExternalTimeSuggestion;
import android.app.time.TimeState;
@@ -103,6 +104,20 @@ public interface TimeDetectorStrategy extends Dumpable {
    /** Processes the suggested time from network sources. */
    void suggestNetworkTime(@NonNull NetworkTimeSuggestion timeSuggestion);

    /**
     * Returns the latest (accepted) network time suggestion. Returns {@code null} if there isn't
     * one.
     */
    @Nullable
    NetworkTimeSuggestion getLatestNetworkSuggestion();

    /**
     * Clears the latest network time suggestion, leaving none. The remaining time signals from
     * other sources will be reassessed causing the device's time to be updated if config and
     * settings allow.
     */
    void clearLatestNetworkSuggestion();

    /** Processes the suggested time from gnss sources. */
    void suggestGnssTime(@NonNull GnssTimeSuggestion timeSuggestion);

Loading