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

Commit 02f2b985 authored by Neil Fuller's avatar Neil Fuller Committed by Automerger Merge Worker
Browse files

Merge "Add server flag for the lower bound" into sc-dev am: c277d492

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14234546

Change-Id: Ib4521a6f9e685dc4fa2387458a75feb9d2752400
parents 6969dabc c277d492
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -30,7 +30,9 @@ import com.android.server.timezonedetector.ServiceConfigAccessor;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@@ -62,6 +64,8 @@ public final class ServerFlags {
            KEY_LOCATION_TIME_ZONE_DETECTION_UNCERTAINTY_DELAY_MILLIS,
            KEY_LOCATION_TIME_ZONE_DETECTION_SETTING_ENABLED_OVERRIDE,
            KEY_LOCATION_TIME_ZONE_DETECTION_SETTING_ENABLED_DEFAULT,
            KEY_TIME_DETECTOR_LOWER_BOUND_MILLIS_OVERRIDE,
            KEY_TIME_DETECTOR_ORIGIN_PRIORITIES_OVERRIDE,
    })
    @Retention(RetentionPolicy.SOURCE)
    @interface DeviceConfigKey {}
@@ -144,6 +148,14 @@ public final class ServerFlags {
    public static final String KEY_TIME_DETECTOR_ORIGIN_PRIORITIES_OVERRIDE =
            "time_detector_origin_priorities_override";

    /**
     * The key to override the time detector lower bound configuration. The values is the number of
     * milliseconds since the beginning of the Unix epoch.
     */
    @DeviceConfigKey
    public static final String KEY_TIME_DETECTOR_LOWER_BOUND_MILLIS_OVERRIDE =
            "time_detector_lower_bound_millis_override";

    @GuardedBy("mListeners")
    private final ArrayMap<ConfigurationChangeListener, Set<String>> mListeners = new ArrayMap<>();

@@ -230,6 +242,25 @@ public final class ServerFlags {
        return Optional.of(string.get().split(","));
    }

    /**
     * Returns an {@link Instant} from {@link DeviceConfig} from the system_time
     * namespace, returns the {@code defaultValue} if the value is missing or invalid.
     */
    @NonNull
    public Optional<Instant> getOptionalInstant(@DeviceConfigKey String key) {
        String value = DeviceConfig.getProperty(NAMESPACE_SYSTEM_TIME, key);
        if (value == null) {
            return Optional.empty();
        }

        try {
            long millis = Long.parseLong(value);
            return Optional.of(Instant.ofEpochMilli(millis));
        } catch (DateTimeException | NumberFormatException e) {
            return Optional.empty();
        }
    }

    /**
     * Returns an optional boolean value from {@link DeviceConfig} from the system_time
     * namespace, returns {@link Optional#empty()} if there is no explicit value set.
+5 −1
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.server.timedetector;

import static com.android.server.timedetector.ServerFlags.KEY_TIME_DETECTOR_LOWER_BOUND_MILLIS_OVERRIDE;
import static com.android.server.timedetector.ServerFlags.KEY_TIME_DETECTOR_ORIGIN_PRIORITIES_OVERRIDE;
import static com.android.server.timedetector.TimeDetectorStrategy.ORIGIN_NETWORK;
import static com.android.server.timedetector.TimeDetectorStrategy.ORIGIN_TELEPHONY;
@@ -65,6 +66,7 @@ final class ServiceConfigAccessor {

    private static final Set<String> SERVER_FLAGS_KEYS_TO_WATCH = Collections.unmodifiableSet(
            new ArraySet<>(new String[] {
                    KEY_TIME_DETECTOR_LOWER_BOUND_MILLIS_OVERRIDE,
                    KEY_TIME_DETECTOR_ORIGIN_PRIORITIES_OVERRIDE,
            }));

@@ -137,8 +139,10 @@ final class ServiceConfigAccessor {
        return mSystemClockUpdateThresholdMillis;
    }

    @NonNull
    Instant autoTimeLowerBound() {
        return TIME_LOWER_BOUND_DEFAULT;
        return mServerFlags.getOptionalInstant(KEY_TIME_DETECTOR_LOWER_BOUND_MILLIS_OVERRIDE)
                .orElse(TIME_LOWER_BOUND_DEFAULT);
    }

    /**
+4 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.timedetector;
import static android.app.timedetector.TimeDetector.SHELL_COMMAND_IS_AUTO_DETECTION_ENABLED;
import static android.provider.DeviceConfig.NAMESPACE_SYSTEM_TIME;

import static com.android.server.timedetector.ServerFlags.KEY_TIME_DETECTOR_LOWER_BOUND_MILLIS_OVERRIDE;
import static com.android.server.timedetector.ServerFlags.KEY_TIME_DETECTOR_ORIGIN_PRIORITIES_OVERRIDE;

import android.os.ShellCommand;
@@ -68,6 +69,9 @@ class TimeDetectorShellCommand extends ShellCommand {
        pw.println();
        pw.printf("This service is also affected by the following device_config flags in the"
                + " %s namespace:\n", NAMESPACE_SYSTEM_TIME);
        pw.printf("    %s - the lower bound used to validate time suggestions when they are"
                        + " received.\n", KEY_TIME_DETECTOR_LOWER_BOUND_MILLIS_OVERRIDE);
        pw.println("         Specified in milliseconds since the start of the Unix epoch.");
        pw.printf("    %s - [default=null], a comma separated list of origins. See"
                + " TimeDetectorStrategy for details\n",
                KEY_TIME_DETECTOR_ORIGIN_PRIORITIES_OVERRIDE);
+2 −2
Original line number Diff line number Diff line
@@ -325,9 +325,9 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy {
        ipw.println("mEnvironment.systemClockMillis()=" + mEnvironment.systemClockMillis());
        ipw.println("mEnvironment.systemClockUpdateThresholdMillis()="
                + mEnvironment.systemClockUpdateThresholdMillis());
        Instant autoTimeLowerBound = mEnvironment.autoTimeLowerBound();
        ipw.printf("mEnvironment.autoTimeLowerBound()=%s(%s)\n",
                mEnvironment.autoTimeLowerBound(),
                mEnvironment.autoTimeLowerBound().toEpochMilli());
                autoTimeLowerBound, autoTimeLowerBound.toEpochMilli());
        String priorities =
                Arrays.stream(mEnvironment.autoOriginPriorities())
                        .mapToObj(TimeDetectorStrategy::originToString)