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

This commit is contained in:
Neil Fuller
2021-04-19 15:56:26 +00:00
committed by Android (Google) Code Review
4 changed files with 42 additions and 3 deletions

View File

@@ -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.

View File

@@ -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);
}
/**

View File

@@ -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);

View File

@@ -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)