From f9b1bad833ead856870558240b099039b991c06c Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Fri, 27 May 2022 14:46:37 +0100 Subject: [PATCH] Move other time config to ConfigurationInternal Move other time detector config to ConfigurationInternal. This means that all configuration, regardless of origin (settings, static config xml, server flags, mixed) are accessed via ConfigurationInternal, not from Environment, simplifying listening, enabling them to be more dynamic in future, etc. Bug: 172891783 Bug: 229740080 Test: atest com.android.server.timedetector android.app.time Change-Id: I625e4fcfd6dd63ab4ebfb181611f977e8ad83f31 --- .../timedetector/ConfigurationInternal.java | 137 ++++++- .../server/timedetector/EnvironmentImpl.java | 31 -- .../timedetector/ServiceConfigAccessor.java | 26 -- .../ServiceConfigAccessorImpl.java | 59 +-- .../TimeDetectorStrategyImpl.java | 56 +-- .../ConfigurationInternalTest.java | 21 ++ .../FakeServiceConfigAccessor.java | 26 -- .../timedetector/TimeDetectorServiceTest.java | 10 + .../TimeDetectorStrategyImplTest.java | 345 +++++++++--------- 9 files changed, 373 insertions(+), 338 deletions(-) diff --git a/services/core/java/com/android/server/timedetector/ConfigurationInternal.java b/services/core/java/com/android/server/timedetector/ConfigurationInternal.java index 3f6e2117a1fca..683eaeb961b04 100644 --- a/services/core/java/com/android/server/timedetector/ConfigurationInternal.java +++ b/services/core/java/com/android/server/timedetector/ConfigurationInternal.java @@ -21,6 +21,8 @@ import static android.app.time.Capabilities.CAPABILITY_NOT_APPLICABLE; import static android.app.time.Capabilities.CAPABILITY_NOT_SUPPORTED; import static android.app.time.Capabilities.CAPABILITY_POSSESSED; +import static java.util.stream.Collectors.joining; + import android.annotation.NonNull; import android.annotation.UserIdInt; import android.app.time.Capabilities.CapabilityState; @@ -29,6 +31,10 @@ import android.app.time.TimeCapabilitiesAndConfig; import android.app.time.TimeConfiguration; import android.os.UserHandle; +import com.android.server.timedetector.TimeDetectorStrategy.Origin; + +import java.time.Instant; +import java.util.Arrays; import java.util.Objects; /** @@ -39,12 +45,20 @@ import java.util.Objects; public final class ConfigurationInternal { private final boolean mAutoDetectionSupported; + private final int mSystemClockUpdateThresholdMillis; + private final Instant mAutoTimeLowerBound; + private final @Origin int[] mOriginPriorities; + private final boolean mDeviceHasY2038Issue; private final boolean mAutoDetectionEnabledSetting; private final @UserIdInt int mUserId; private final boolean mUserConfigAllowed; private ConfigurationInternal(Builder builder) { mAutoDetectionSupported = builder.mAutoDetectionSupported; + mSystemClockUpdateThresholdMillis = builder.mSystemClockUpdateThresholdMillis; + mAutoTimeLowerBound = Objects.requireNonNull(builder.mAutoTimeLowerBound); + mOriginPriorities = Objects.requireNonNull(builder.mOriginPriorities); + mDeviceHasY2038Issue = builder.mDeviceHasY2038Issue; mAutoDetectionEnabledSetting = builder.mAutoDetectionEnabledSetting; mUserId = builder.mUserId; @@ -56,6 +70,42 @@ public final class ConfigurationInternal { return mAutoDetectionSupported; } + /** + * Returns the absolute threshold below which the system clock need not be updated. i.e. if + * setting the system clock would adjust it by less than this (either backwards or forwards) + * then it need not be set. + */ + public int getSystemClockUpdateThresholdMillis() { + return mSystemClockUpdateThresholdMillis; + } + + /** + * Returns the lower bound for valid automatic times. It is guaranteed to be in the past, + * i.e. it is unrelated to the current system clock time. + * It holds no other meaning; it could be related to when the device system image was built, + * or could be updated by a mainline module. + */ + @NonNull + public Instant getAutoTimeLowerBound() { + return mAutoTimeLowerBound; + } + + /** + * Returns the order to look at time suggestions when automatically detecting time. + * See {@code #ORIGIN_} constants + */ + public @Origin int[] getAutoOriginPriorities() { + return mOriginPriorities; + } + + /** + * Returns {@code true} if the device may be at risk of time_t overflow (because bionic + * defines time_t as a 32-bit signed integer for 32-bit processes). + */ + public boolean getDeviceHasY2038Issue() { + return mDeviceHasY2038Issue; + } + /** Returns the value of the auto time detection enabled setting. */ public boolean getAutoDetectionEnabledSetting() { return mAutoDetectionEnabledSetting; @@ -146,38 +196,61 @@ public final class ConfigurationInternal { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (!(o instanceof ConfigurationInternal)) { + return false; + } ConfigurationInternal that = (ConfigurationInternal) o; return mAutoDetectionSupported == that.mAutoDetectionSupported - && mUserId == that.mUserId - && mUserConfigAllowed == that.mUserConfigAllowed - && mAutoDetectionEnabledSetting == that.mAutoDetectionEnabledSetting; + && mAutoDetectionEnabledSetting == that.mAutoDetectionEnabledSetting + && mUserId == that.mUserId && mUserConfigAllowed == that.mUserConfigAllowed + && mSystemClockUpdateThresholdMillis == that.mSystemClockUpdateThresholdMillis + && mAutoTimeLowerBound.equals(that.mAutoTimeLowerBound) + && mDeviceHasY2038Issue == that.mDeviceHasY2038Issue + && Arrays.equals(mOriginPriorities, that.mOriginPriorities); } @Override public int hashCode() { - return Objects.hash(mAutoDetectionSupported, mUserId, - mUserConfigAllowed, mAutoDetectionEnabledSetting); + int result = Objects.hash(mAutoDetectionSupported, mAutoDetectionEnabledSetting, mUserId, + mUserConfigAllowed, mSystemClockUpdateThresholdMillis, mAutoTimeLowerBound, + mDeviceHasY2038Issue); + result = 31 * result + Arrays.hashCode(mOriginPriorities); + return result; } @Override public String toString() { + String originPrioritiesString = + Arrays.stream(mOriginPriorities) + .mapToObj(TimeDetectorStrategy::originToString) + .collect(joining(",", "[", "]")); return "ConfigurationInternal{" + "mAutoDetectionSupported=" + mAutoDetectionSupported - + "mUserId=" + mUserId - + ", mUserConfigAllowed=" + mUserConfigAllowed + + ", mSystemClockUpdateThresholdMillis=" + mSystemClockUpdateThresholdMillis + + ", mAutoTimeLowerBound=" + mAutoTimeLowerBound + + "(" + mAutoTimeLowerBound.toEpochMilli() + ")" + + ", mOriginPriorities=" + originPrioritiesString + + ", mDeviceHasY2038Issue=" + mDeviceHasY2038Issue + ", mAutoDetectionEnabled=" + mAutoDetectionEnabledSetting + + ", mUserId=" + mUserId + + ", mUserConfigAllowed=" + mUserConfigAllowed + '}'; } static final class Builder { - private final @UserIdInt int mUserId; - - private boolean mUserConfigAllowed; private boolean mAutoDetectionSupported; + private int mSystemClockUpdateThresholdMillis; + @NonNull private Instant mAutoTimeLowerBound; + @NonNull private @Origin int[] mOriginPriorities; + private boolean mDeviceHasY2038Issue; private boolean mAutoDetectionEnabledSetting; + private final @UserIdInt int mUserId; + private boolean mUserConfigAllowed; + Builder(@UserIdInt int userId) { mUserId = userId; } @@ -189,6 +262,10 @@ public final class ConfigurationInternal { this.mUserId = toCopy.mUserId; this.mUserConfigAllowed = toCopy.mUserConfigAllowed; this.mAutoDetectionSupported = toCopy.mAutoDetectionSupported; + this.mSystemClockUpdateThresholdMillis = toCopy.mSystemClockUpdateThresholdMillis; + this.mAutoTimeLowerBound = toCopy.mAutoTimeLowerBound; + this.mOriginPriorities = toCopy.mOriginPriorities; + this.mDeviceHasY2038Issue = toCopy.mDeviceHasY2038Issue; this.mAutoDetectionEnabledSetting = toCopy.mAutoDetectionEnabledSetting; } @@ -208,6 +285,33 @@ public final class ConfigurationInternal { return this; } + /** + * Sets the absolute threshold below which the system clock need not be updated. i.e. if + * setting the system clock would adjust it by less than this (either backwards or forwards) + * then it need not be set. + */ + public Builder setSystemClockUpdateThresholdMillis(int systemClockUpdateThresholdMillis) { + mSystemClockUpdateThresholdMillis = systemClockUpdateThresholdMillis; + return this; + } + + /** + * Sets the lower bound for valid automatic times. + */ + public Builder setAutoTimeLowerBound(@NonNull Instant autoTimeLowerBound) { + mAutoTimeLowerBound = Objects.requireNonNull(autoTimeLowerBound); + return this; + } + + /** + * Sets the order to look at time suggestions when automatically detecting time. + * See {@code #ORIGIN_} constants + */ + public Builder setOriginPriorities(@NonNull @Origin int... originPriorities) { + mOriginPriorities = Objects.requireNonNull(originPriorities); + return this; + } + /** * Sets the value of the automatic time detection enabled setting for this device. */ @@ -216,6 +320,15 @@ public final class ConfigurationInternal { return this; } + /** + * Returns {@code true} if the device may be at risk of time_t overflow (because bionic + * defines time_t as a 32-bit signed integer for 32-bit processes). + */ + Builder setDeviceHasY2038Issue(boolean deviceHasY2038Issue) { + mDeviceHasY2038Issue = deviceHasY2038Issue; + return this; + } + /** Returns a new {@link ConfigurationInternal}. */ @NonNull ConfigurationInternal build() { diff --git a/services/core/java/com/android/server/timedetector/EnvironmentImpl.java b/services/core/java/com/android/server/timedetector/EnvironmentImpl.java index fe4eb66922af7..3e02b463284dc 100644 --- a/services/core/java/com/android/server/timedetector/EnvironmentImpl.java +++ b/services/core/java/com/android/server/timedetector/EnvironmentImpl.java @@ -18,18 +18,14 @@ package com.android.server.timedetector; import android.annotation.NonNull; import android.app.AlarmManager; -import android.content.ContentResolver; import android.content.Context; -import android.os.Build; import android.os.Handler; import android.os.PowerManager; import android.os.SystemClock; -import android.os.UserManager; import android.util.Slog; import com.android.server.timezonedetector.ConfigurationChangeListener; -import java.time.Instant; import java.util.Objects; /** @@ -39,18 +35,13 @@ final class EnvironmentImpl implements TimeDetectorStrategyImpl.Environment { private static final String LOG_TAG = TimeDetectorService.TAG; - @NonNull private final Context mContext; @NonNull private final Handler mHandler; @NonNull private final ServiceConfigAccessor mServiceConfigAccessor; - @NonNull private final ContentResolver mContentResolver; @NonNull private final PowerManager.WakeLock mWakeLock; @NonNull private final AlarmManager mAlarmManager; - @NonNull private final UserManager mUserManager; EnvironmentImpl(@NonNull Context context, @NonNull Handler handler, @NonNull ServiceConfigAccessor serviceConfigAccessor) { - mContext = Objects.requireNonNull(context); - mContentResolver = Objects.requireNonNull(context.getContentResolver()); mHandler = Objects.requireNonNull(handler); mServiceConfigAccessor = Objects.requireNonNull(serviceConfigAccessor); @@ -59,8 +50,6 @@ final class EnvironmentImpl implements TimeDetectorStrategyImpl.Environment { powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG)); mAlarmManager = Objects.requireNonNull(context.getSystemService(AlarmManager.class)); - - mUserManager = Objects.requireNonNull(context.getSystemService(UserManager.class)); } @Override @@ -71,21 +60,6 @@ final class EnvironmentImpl implements TimeDetectorStrategyImpl.Environment { mServiceConfigAccessor.addConfigurationInternalChangeListener(configurationChangeListener); } - @Override - public int systemClockUpdateThresholdMillis() { - return mServiceConfigAccessor.systemClockUpdateThresholdMillis(); - } - - @Override - public Instant autoTimeLowerBound() { - return mServiceConfigAccessor.autoTimeLowerBound(); - } - - @Override - public int[] autoOriginPriorities() { - return mServiceConfigAccessor.getOriginPriorities(); - } - @Override public ConfigurationInternal getCurrentUserConfigurationInternal() { return mServiceConfigAccessor.getCurrentUserConfigurationInternal(); @@ -121,11 +95,6 @@ final class EnvironmentImpl implements TimeDetectorStrategyImpl.Environment { mWakeLock.release(); } - @Override - public boolean deviceHasY2038Issue() { - return Build.SUPPORTED_32_BIT_ABIS.length > 0; - } - private void checkWakeLockHeld() { if (!mWakeLock.isHeld()) { Slog.wtf(LOG_TAG, "WakeLock " + mWakeLock + " not held"); diff --git a/services/core/java/com/android/server/timedetector/ServiceConfigAccessor.java b/services/core/java/com/android/server/timedetector/ServiceConfigAccessor.java index 80ce5986ca79a..25a74ceeb56d4 100644 --- a/services/core/java/com/android/server/timedetector/ServiceConfigAccessor.java +++ b/services/core/java/com/android/server/timedetector/ServiceConfigAccessor.java @@ -19,11 +19,8 @@ import android.annotation.NonNull; import android.annotation.UserIdInt; import android.app.time.TimeConfiguration; -import com.android.server.timedetector.TimeDetectorStrategy.Origin; import com.android.server.timezonedetector.ConfigurationChangeListener; -import java.time.Instant; - /** * An interface that provides access to service configuration for time detection. This hides * how configuration is split between static, compile-time config, dynamic server-pushed flags and @@ -68,27 +65,4 @@ public interface ServiceConfigAccessor { */ @NonNull ConfigurationInternal getConfigurationInternal(@UserIdInt int userId); - - /** - * Returns the absolute threshold below which the system clock need not be updated. i.e. if - * setting the system clock would adjust it by less than this (either backwards or forwards) - * then it need not be set. - */ - int systemClockUpdateThresholdMillis(); - - /** - * Returns a lower bound for valid automatic times. It is guaranteed to be in the past, - * i.e. it is unrelated to the current system clock time. - * It holds no other meaning; it could be related to when the device system image was built, - * or could be updated by a mainline module. - */ - @NonNull - Instant autoTimeLowerBound(); - - /** - * Returns the order to look at time suggestions when automatically detecting time. - * See {@code #ORIGIN_} constants - */ - @NonNull - @Origin int[] getOriginPriorities(); } diff --git a/services/core/java/com/android/server/timedetector/ServiceConfigAccessorImpl.java b/services/core/java/com/android/server/timedetector/ServiceConfigAccessorImpl.java index b53c5124081e8..b161cc7204585 100644 --- a/services/core/java/com/android/server/timedetector/ServiceConfigAccessorImpl.java +++ b/services/core/java/com/android/server/timedetector/ServiceConfigAccessorImpl.java @@ -182,33 +182,6 @@ final class ServiceConfigAccessorImpl implements ServiceConfigAccessor { mConfigurationInternalListeners.remove(Objects.requireNonNull(listener)); } - @Override - @NonNull - public @Origin int[] getOriginPriorities() { - int[] serverFlagsValue = mServerFlagsOriginPrioritiesSupplier.get(); - if (serverFlagsValue != null) { - return serverFlagsValue; - } - - int[] configValue = mConfigOriginPrioritiesSupplier.get(); - if (configValue != null) { - return configValue; - } - return DEFAULT_AUTOMATIC_TIME_ORIGIN_PRIORITIES; - } - - @Override - public int systemClockUpdateThresholdMillis() { - return mSystemClockUpdateThresholdMillis; - } - - @Override - @NonNull - public Instant autoTimeLowerBound() { - return mServerFlags.getOptionalInstant(KEY_TIME_DETECTOR_LOWER_BOUND_MILLIS_OVERRIDE) - .orElse(TIME_LOWER_BOUND_DEFAULT); - } - @Override @NonNull public synchronized ConfigurationInternal getCurrentUserConfigurationInternal() { @@ -268,6 +241,10 @@ final class ServiceConfigAccessorImpl implements ServiceConfigAccessor { .setUserConfigAllowed(isUserConfigAllowed(userId)) .setAutoDetectionSupported(isAutoDetectionSupported()) .setAutoDetectionEnabledSetting(getAutoDetectionEnabledSetting()) + .setSystemClockUpdateThresholdMillis(getSystemClockUpdateThresholdMillis()) + .setAutoTimeLowerBound(getAutoTimeLowerBound()) + .setOriginPriorities(getOriginPriorities()) + .setDeviceHasY2038Issue(getDeviceHasY2038Issue()) .build(); } @@ -309,6 +286,34 @@ final class ServiceConfigAccessorImpl implements ServiceConfigAccessor { return false; } + private int getSystemClockUpdateThresholdMillis() { + return mSystemClockUpdateThresholdMillis; + } + + @NonNull + private Instant getAutoTimeLowerBound() { + return mServerFlags.getOptionalInstant(KEY_TIME_DETECTOR_LOWER_BOUND_MILLIS_OVERRIDE) + .orElse(TIME_LOWER_BOUND_DEFAULT); + } + + @NonNull + private @Origin int[] getOriginPriorities() { + @Origin int[] serverFlagsValue = mServerFlagsOriginPrioritiesSupplier.get(); + if (serverFlagsValue != null) { + return serverFlagsValue; + } + + @Origin int[] configValue = mConfigOriginPrioritiesSupplier.get(); + if (configValue != null) { + return configValue; + } + return DEFAULT_AUTOMATIC_TIME_ORIGIN_PRIORITIES; + } + + private boolean getDeviceHasY2038Issue() { + return Build.SUPPORTED_32_BIT_ABIS.length > 0; + } + /** * A base supplier of an array of time origin integers in priority order. * It handles memoization of the result to avoid repeated string parsing when nothing has diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java b/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java index ea6dfc31c31e1..ecbf1f88c1206 100644 --- a/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java +++ b/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java @@ -18,8 +18,6 @@ package com.android.server.timedetector; import static com.android.server.timedetector.TimeDetectorStrategy.originToString; -import static java.util.stream.Collectors.joining; - import android.annotation.CurrentTimeMillisLong; import android.annotation.ElapsedRealtimeLong; import android.annotation.NonNull; @@ -153,28 +151,6 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { /** Returns the {@link ConfigurationInternal} for the current user. */ @NonNull ConfigurationInternal getCurrentUserConfigurationInternal(); - /** - * Returns the absolute threshold below which the system clock need not be updated. i.e. if - * setting the system clock would adjust it by less than this (either backwards or forwards) - * then it need not be set. - */ - int systemClockUpdateThresholdMillis(); - - /** - * Returns a lower bound for valid automatic times. It is guaranteed to be in the past, - * i.e. it is unrelated to the current system clock time. - * It holds no other meaning; it could be related to when the device system image was built, - * or could be updated by a mainline module. - */ - @NonNull - Instant autoTimeLowerBound(); - - /** - * Returns the order to look at time suggestions when automatically detecting time. - * See {@code #ORIGIN_} constants - */ - @Origin int[] autoOriginPriorities(); - /** Acquire a suitable wake lock. Must be followed by {@link #releaseWakeLock()} */ void acquireWakeLock(); @@ -191,12 +167,6 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { /** Release the wake lock acquired by a call to {@link #acquireWakeLock()}. */ void releaseWakeLock(); - - /** - * Returns {@code true} if the device may be at risk of time_t overflow (because bionic - * defines time_t as a 32-bit signed integer for 32-bit processes). - */ - boolean deviceHasY2038Issue(); } static TimeDetectorStrategy create( @@ -384,25 +354,13 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { ipw.println("mLastAutoSystemClockTimeSet=" + mLastAutoSystemClockTimeSet); ipw.println("mCurrentConfigurationInternal=" + mCurrentConfigurationInternal); - ipw.println("[Capabilities=" + mCurrentConfigurationInternal.capabilitiesAndConfig() - + "]"); + ipw.println("[Capabilities=" + mCurrentConfigurationInternal.capabilitiesAndConfig() + "]"); long elapsedRealtimeMillis = mEnvironment.elapsedRealtimeMillis(); ipw.printf("mEnvironment.elapsedRealtimeMillis()=%s (%s)\n", Duration.ofMillis(elapsedRealtimeMillis), elapsedRealtimeMillis); long systemClockMillis = mEnvironment.systemClockMillis(); ipw.printf("mEnvironment.systemClockMillis()=%s (%s)\n", Instant.ofEpochMilli(systemClockMillis), systemClockMillis); - ipw.println("mEnvironment.systemClockUpdateThresholdMillis()=" - + mEnvironment.systemClockUpdateThresholdMillis()); - Instant autoTimeLowerBound = mEnvironment.autoTimeLowerBound(); - ipw.printf("mEnvironment.autoTimeLowerBound()=%s (%s)\n", - autoTimeLowerBound, autoTimeLowerBound.toEpochMilli()); - String priorities = - Arrays.stream(mEnvironment.autoOriginPriorities()) - .mapToObj(TimeDetectorStrategy::originToString) - .collect(joining(",", "[", "]")); - ipw.println("mEnvironment.autoOriginPriorities()=" + priorities); - ipw.println("mEnvironment.deviceHasY2038Issue()=" + mEnvironment.deviceHasY2038Issue()); ipw.println("Time change log:"); ipw.increaseIndent(); // level 2 @@ -467,6 +425,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { return true; } + @GuardedBy("this") private boolean validateSuggestionTime( @NonNull TimestampedValue newUnixEpochTime, @NonNull Object suggestion) { if (newUnixEpochTime.getValue() == null) { @@ -485,7 +444,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } if (newUnixEpochTime.getValue() > Y2038_LIMIT_IN_MILLIS - && mEnvironment.deviceHasY2038Issue()) { + && mCurrentConfigurationInternal.getDeviceHasY2038Issue()) { // This check won't prevent a device's system clock exceeding Integer.MAX_VALUE Unix // seconds through the normal passage of time, but it will stop it jumping above 2038 // because of a "bad" suggestion. b/204193177 @@ -496,15 +455,17 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { return true; } + @GuardedBy("this") private boolean validateAutoSuggestionTime( @NonNull TimestampedValue newUnixEpochTime, @NonNull Object suggestion) { return validateSuggestionTime(newUnixEpochTime, suggestion) && validateSuggestionAgainstLowerBound(newUnixEpochTime, suggestion); } + @GuardedBy("this") private boolean validateSuggestionAgainstLowerBound( @NonNull TimestampedValue newUnixEpochTime, @NonNull Object suggestion) { - Instant lowerBound = mEnvironment.autoTimeLowerBound(); + Instant lowerBound = mCurrentConfigurationInternal.getAutoTimeLowerBound(); // Suggestion is definitely wrong if it comes before lower time bound. if (lowerBound.isAfter(Instant.ofEpochMilli(newUnixEpochTime.getValue()))) { @@ -524,7 +485,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } // Try the different origins one at a time. - int[] originPriorities = mEnvironment.autoOriginPriorities(); + int[] originPriorities = mCurrentConfigurationInternal.getAutoOriginPriorities(); for (int origin : originPriorities) { TimestampedValue newUnixEpochTime = null; String cause = null; @@ -814,7 +775,8 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { // Check if the new signal would make sufficient difference to the system clock. If it's // below the threshold then ignore it. long absTimeDifference = Math.abs(newSystemClockMillis - actualSystemClockMillis); - long systemClockUpdateThreshold = mEnvironment.systemClockUpdateThresholdMillis(); + long systemClockUpdateThreshold = + mCurrentConfigurationInternal.getSystemClockUpdateThresholdMillis(); if (absTimeDifference < systemClockUpdateThreshold) { if (DBG) { Slog.d(LOG_TAG, "Not setting system clock. New time and" diff --git a/services/tests/servicestests/src/com/android/server/timedetector/ConfigurationInternalTest.java b/services/tests/servicestests/src/com/android/server/timedetector/ConfigurationInternalTest.java index 8425d13e36b74..c3d40daef412b 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/ConfigurationInternalTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/ConfigurationInternalTest.java @@ -21,6 +21,8 @@ import static android.app.time.Capabilities.CAPABILITY_NOT_APPLICABLE; import static android.app.time.Capabilities.CAPABILITY_NOT_SUPPORTED; import static android.app.time.Capabilities.CAPABILITY_POSSESSED; +import static com.android.server.timedetector.TimeDetectorStrategy.ORIGIN_NETWORK; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -31,13 +33,20 @@ import android.app.time.TimeConfiguration; import androidx.test.runner.AndroidJUnit4; +import com.android.server.timedetector.TimeDetectorStrategy.Origin; + import org.junit.Test; import org.junit.runner.RunWith; +import java.time.Instant; + @RunWith(AndroidJUnit4.class) public class ConfigurationInternalTest { private static final int ARBITRARY_USER_ID = 99999; + private static final int ARBITRARY_SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS = 1234; + private static final Instant ARBITRARY_AUTO_TIME_LOWER_BOUND = Instant.ofEpochMilli(0); + private static final @Origin int[] ARBITRARY_ORIGIN_PRIORITIES = { ORIGIN_NETWORK }; /** * Tests when {@link ConfigurationInternal#isUserConfigAllowed()} and @@ -49,6 +58,10 @@ public class ConfigurationInternalTest { baseConfig = new ConfigurationInternal.Builder(ARBITRARY_USER_ID) .setUserConfigAllowed(true) .setAutoDetectionSupported(true) + .setSystemClockUpdateThresholdMillis(ARBITRARY_SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS) + .setAutoTimeLowerBound(ARBITRARY_AUTO_TIME_LOWER_BOUND) + .setOriginPriorities(ARBITRARY_ORIGIN_PRIORITIES) + .setDeviceHasY2038Issue(true) .setAutoDetectionEnabledSetting(true) .build(); { @@ -96,6 +109,10 @@ public class ConfigurationInternalTest { baseConfig = new ConfigurationInternal.Builder(ARBITRARY_USER_ID) .setUserConfigAllowed(false) .setAutoDetectionSupported(true) + .setSystemClockUpdateThresholdMillis(ARBITRARY_SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS) + .setAutoTimeLowerBound(ARBITRARY_AUTO_TIME_LOWER_BOUND) + .setOriginPriorities(ARBITRARY_ORIGIN_PRIORITIES) + .setDeviceHasY2038Issue(true) .setAutoDetectionEnabledSetting(true) .build(); { @@ -141,6 +158,10 @@ public class ConfigurationInternalTest { ConfigurationInternal baseConfig = new ConfigurationInternal.Builder(ARBITRARY_USER_ID) .setUserConfigAllowed(true) .setAutoDetectionSupported(false) + .setSystemClockUpdateThresholdMillis(ARBITRARY_SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS) + .setAutoTimeLowerBound(ARBITRARY_AUTO_TIME_LOWER_BOUND) + .setOriginPriorities(ARBITRARY_ORIGIN_PRIORITIES) + .setDeviceHasY2038Issue(true) .setAutoDetectionEnabledSetting(true) .build(); { diff --git a/services/tests/servicestests/src/com/android/server/timedetector/FakeServiceConfigAccessor.java b/services/tests/servicestests/src/com/android/server/timedetector/FakeServiceConfigAccessor.java index dd686f3c08401..77b319b56acb6 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/FakeServiceConfigAccessor.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/FakeServiceConfigAccessor.java @@ -18,7 +18,6 @@ package com.android.server.timedetector; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; import android.annotation.NonNull; import android.annotation.UserIdInt; @@ -28,7 +27,6 @@ import android.app.time.TimeConfiguration; import com.android.server.timezonedetector.ConfigurationChangeListener; -import java.time.Instant; import java.util.ArrayList; import java.util.List; @@ -98,28 +96,4 @@ class FakeServiceConfigAccessor implements ServiceConfigAccessor { userId, mConfigurationInternal.getUserId()); return mConfigurationInternal; } - - @Override - public int systemClockUpdateThresholdMillis() { - failUnimplemented(); - return 0; - } - - @Override - public Instant autoTimeLowerBound() { - failUnimplemented(); - return null; - } - - @Override - public @TimeDetectorStrategy.Origin int[] getOriginPriorities() { - failUnimplemented(); - return new int[0]; - } - - @SuppressWarnings("UnusedReturnValue") - private static T failUnimplemented() { - fail("Unimplemented"); - return null; - } } diff --git a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java index 3086d90a2d2f3..e9617e9d973ad 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java @@ -16,6 +16,8 @@ package com.android.server.timedetector; +import static com.android.server.timedetector.TimeDetectorStrategy.ORIGIN_NETWORK; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -58,11 +60,15 @@ import org.junit.runner.RunWith; import java.io.PrintWriter; import java.io.StringWriter; +import java.time.Instant; @RunWith(AndroidJUnit4.class) public class TimeDetectorServiceTest { private static final int ARBITRARY_USER_ID = 9999; + private static final int ARBITRARY_SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS = 1234; + private static final Instant ARBITRARY_AUTO_TIME_LOWER_BOUND = Instant.ofEpochMilli(0); + private static final int[] ARBITRARY_ORIGIN_PRIORITIES = { ORIGIN_NETWORK }; private Context mMockContext; @@ -415,6 +421,10 @@ public class TimeDetectorServiceTest { return new ConfigurationInternal.Builder(ARBITRARY_USER_ID) .setUserConfigAllowed(true) .setAutoDetectionSupported(true) + .setSystemClockUpdateThresholdMillis(ARBITRARY_SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS) + .setAutoTimeLowerBound(ARBITRARY_AUTO_TIME_LOWER_BOUND) + .setOriginPriorities(ARBITRARY_ORIGIN_PRIORITIES) + .setDeviceHasY2038Issue(true) .setAutoDetectionEnabledSetting(autoDetectionEnabled) .build(); } diff --git a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java index 2d2b29887938b..15a8996aef4c3 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java @@ -53,7 +53,8 @@ import java.util.Objects; @RunWith(AndroidJUnit4.class) public class TimeDetectorStrategyImplTest { - private static final @UserIdInt int USER_ID = 9876; + private static final @UserIdInt int ARBITRARY_USER_ID = 9876; + private static final int ARBITRARY_SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS = 1234; private static final Instant TIME_LOWER_BOUND = createUnixEpochTime(2009, 1, 1, 12, 0, 0); private static final TimestampedValue ARBITRARY_CLOCK_INITIALIZATION_INFO = @@ -62,7 +63,7 @@ public class TimeDetectorStrategyImplTest { createUnixEpochTime(2010, 5, 23, 12, 0, 0)); // This is the traditional ordering for time detection on Android. - private static final @Origin int [] PROVIDERS_PRIORITY = { ORIGIN_TELEPHONY, ORIGIN_NETWORK }; + private static final @Origin int [] ORIGIN_PRIORITIES = { ORIGIN_TELEPHONY, ORIGIN_NETWORK }; /** * An arbitrary time, very different from the {@link #ARBITRARY_CLOCK_INITIALIZATION_INFO} @@ -73,16 +74,26 @@ public class TimeDetectorStrategyImplTest { private static final int ARBITRARY_SLOT_INDEX = 123456; private static final ConfigurationInternal CONFIG_AUTO_DISABLED = - new ConfigurationInternal.Builder(USER_ID) + new ConfigurationInternal.Builder(ARBITRARY_USER_ID) .setUserConfigAllowed(true) .setAutoDetectionSupported(true) + .setSystemClockUpdateThresholdMillis( + ARBITRARY_SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS) + .setAutoTimeLowerBound(TIME_LOWER_BOUND) + .setOriginPriorities(ORIGIN_PRIORITIES) + .setDeviceHasY2038Issue(true) .setAutoDetectionEnabledSetting(false) .build(); private static final ConfigurationInternal CONFIG_AUTO_ENABLED = - new ConfigurationInternal.Builder(USER_ID) + new ConfigurationInternal.Builder(ARBITRARY_USER_ID) .setUserConfigAllowed(true) .setAutoDetectionSupported(true) + .setSystemClockUpdateThresholdMillis( + ARBITRARY_SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS) + .setAutoTimeLowerBound(TIME_LOWER_BOUND) + .setOriginPriorities(ORIGIN_PRIORITIES) + .setDeviceHasY2038Issue(true) .setAutoDetectionEnabledSetting(true) .build(); @@ -97,8 +108,7 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestTelephonyTime_autoTimeEnabled() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); int slotIndex = ARBITRARY_SLOT_INDEX; Instant testTime = ARBITRARY_TEST_TIME; @@ -116,8 +126,7 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestTelephonyTime_emptySuggestionIgnored() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); int slotIndex = ARBITRARY_SLOT_INDEX; TelephonyTimeSuggestion timeSuggestion = @@ -131,9 +140,11 @@ public class TimeDetectorStrategyImplTest { public void testSuggestTelephonyTime_systemClockThreshold() { final int systemClockUpdateThresholdMillis = 1000; final int clockIncrementMillis = 100; - Script script = new Script() - .pokeThresholds(systemClockUpdateThresholdMillis) - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setSystemClockUpdateThresholdMillis(systemClockUpdateThresholdMillis) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); int slotIndex = ARBITRARY_SLOT_INDEX; @@ -183,8 +194,7 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestTelephonyTime_multipleSlotIndexsAndBucketing() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); // There are 2 slotIndexes in this test. slotIndex1 and slotIndex2 have different opinions // about the current time. slotIndex1 < slotIndex2 (which is important because the strategy @@ -261,8 +271,7 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestTelephonyTime_autoTimeDisabled() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); int slotIndex = ARBITRARY_SLOT_INDEX; TelephonyTimeSuggestion timeSuggestion = @@ -275,10 +284,12 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestTelephonyTime_invalidNitzReferenceTimesIgnored() { - final int systemClockUpdateThreshold = 2000; - Script script = new Script() - .pokeThresholds(systemClockUpdateThreshold) - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + final int systemClockUpdateThresholdMillis = 2000; + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setSystemClockUpdateThresholdMillis(systemClockUpdateThresholdMillis) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); Instant testTime = ARBITRARY_TEST_TIME; int slotIndex = ARBITRARY_SLOT_INDEX; @@ -297,7 +308,7 @@ public class TimeDetectorStrategyImplTest { // The Unix epoch time increment should be larger than the system clock update threshold so // we know it shouldn't be ignored for other reasons. long validUnixEpochTimeMillis = unixEpochTime1.getValue() - + (2 * systemClockUpdateThreshold); + + (2 * systemClockUpdateThresholdMillis); // Now supply a new signal that has an obviously bogus reference time : older than the last // one. @@ -336,8 +347,7 @@ public class TimeDetectorStrategyImplTest { @Test public void telephonyTimeSuggestion_ignoredWhenReferencedTimeIsInThePast() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); int slotIndex = ARBITRARY_SLOT_INDEX; Instant suggestedTime = TIME_LOWER_BOUND.minus(Duration.ofDays(1)); @@ -354,10 +364,12 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestTelephonyTime_timeDetectionToggled() { final int clockIncrementMillis = 100; - final int systemClockUpdateThreshold = 2000; - Script script = new Script() - .pokeThresholds(systemClockUpdateThreshold) - .simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); + final int systemClockUpdateThresholdMillis = 2000; + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_DISABLED) + .setSystemClockUpdateThresholdMillis(systemClockUpdateThresholdMillis) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); int slotIndex = ARBITRARY_SLOT_INDEX; Instant testTime = ARBITRARY_TEST_TIME; @@ -392,7 +404,7 @@ public class TimeDetectorStrategyImplTest { // Receive another valid time signal. // It should be on the threshold and accounting for the clock increments. TelephonyTimeSuggestion timeSuggestion2 = script.generateTelephonyTimeSuggestion( - slotIndex, script.peekSystemClockMillis() + systemClockUpdateThreshold); + slotIndex, script.peekSystemClockMillis() + systemClockUpdateThresholdMillis); // Simulate more time passing. script.simulateTimePassing(clockIncrementMillis); @@ -414,8 +426,7 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestTelephonyTime_maxSuggestionAge() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); int slotIndex = ARBITRARY_SLOT_INDEX; Instant testTime = ARBITRARY_TEST_TIME; @@ -445,8 +456,7 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestManualTime_autoTimeDisabled() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); ManualTimeSuggestion timeSuggestion = script.generateManualTimeSuggestion(ARBITRARY_TEST_TIME); @@ -455,15 +465,14 @@ public class TimeDetectorStrategyImplTest { long expectedSystemClockMillis = script.calculateTimeInMillisForNow(timeSuggestion.getUnixEpochTime()); - script.simulateManualTimeSuggestion(USER_ID, timeSuggestion, true /* expectedResult */) + script.simulateManualTimeSuggestion( + ARBITRARY_USER_ID, timeSuggestion, true /* expectedResult */) .verifySystemClockWasSetAndResetCallTracking(expectedSystemClockMillis); } @Test public void testSuggestManualTime_retainsAutoSignal() { - // Configure the start state. - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); int slotIndex = ARBITRARY_SLOT_INDEX; @@ -501,7 +510,7 @@ public class TimeDetectorStrategyImplTest { long expectedManualClockMillis = script.calculateTimeInMillisForNow(manualTimeSuggestion.getUnixEpochTime()); script.simulateManualTimeSuggestion( - USER_ID, manualTimeSuggestion, true /* expectedResult */) + ARBITRARY_USER_ID, manualTimeSuggestion, true /* expectedResult */) .verifySystemClockWasSetAndResetCallTracking(expectedManualClockMillis) .assertLatestTelephonySuggestion(slotIndex, telephonyTimeSuggestion); @@ -524,35 +533,37 @@ public class TimeDetectorStrategyImplTest { @Test public void manualTimeSuggestion_isIgnored_whenAutoTimeEnabled() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); ManualTimeSuggestion timeSuggestion = script.generateManualTimeSuggestion(ARBITRARY_TEST_TIME); script.simulateTimePassing() - .simulateManualTimeSuggestion(USER_ID, timeSuggestion, false /* expectedResult */) + .simulateManualTimeSuggestion( + ARBITRARY_USER_ID, timeSuggestion, false /* expectedResult */) .verifySystemClockWasNotSetAndResetCallTracking(); } @Test public void manualTimeSuggestion_ignoresTimeLowerBound() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); + Script script = new Script().simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); Instant suggestedTime = TIME_LOWER_BOUND.minus(Duration.ofDays(1)); ManualTimeSuggestion timeSuggestion = script.generateManualTimeSuggestion(suggestedTime); - script.simulateManualTimeSuggestion(USER_ID, timeSuggestion, true /* expectedResult */) + script.simulateManualTimeSuggestion( + ARBITRARY_USER_ID, timeSuggestion, true /* expectedResult */) .verifySystemClockWasSetAndResetCallTracking(suggestedTime.toEpochMilli()); } @Test public void testSuggestNetworkTime_autoTimeEnabled() { - Script script = new Script() - .pokeAutoOriginPriorities(ORIGIN_NETWORK) - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_NETWORK) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); NetworkTimeSuggestion timeSuggestion = script.generateNetworkTimeSuggestion(ARBITRARY_TEST_TIME); @@ -567,9 +578,11 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestNetworkTime_autoTimeDisabled() { - Script script = new Script() - .pokeAutoOriginPriorities(ORIGIN_NETWORK) - .simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_DISABLED) + .setOriginPriorities(ORIGIN_NETWORK) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); NetworkTimeSuggestion timeSuggestion = script.generateNetworkTimeSuggestion(ARBITRARY_TEST_TIME); @@ -581,9 +594,11 @@ public class TimeDetectorStrategyImplTest { @Test public void networkTimeSuggestion_ignoredWhenReferencedTimeIsInThePast() { - Script script = new Script() - .pokeAutoOriginPriorities(ORIGIN_NETWORK) - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_NETWORK) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); Instant suggestedTime = TIME_LOWER_BOUND.minus(Duration.ofDays(1)); NetworkTimeSuggestion timeSuggestion = @@ -596,9 +611,11 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestGnssTime_autoTimeEnabled() { - Script script = new Script() - .pokeAutoOriginPriorities(ORIGIN_GNSS) - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_GNSS) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); GnssTimeSuggestion timeSuggestion = script.generateGnssTimeSuggestion(ARBITRARY_TEST_TIME); @@ -613,9 +630,11 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestGnssTime_autoTimeDisabled() { - Script script = new Script() - .pokeAutoOriginPriorities(ORIGIN_GNSS) - .simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_DISABLED) + .setOriginPriorities(ORIGIN_GNSS) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); GnssTimeSuggestion timeSuggestion = script.generateGnssTimeSuggestion(ARBITRARY_TEST_TIME); @@ -627,9 +646,11 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestExternalTime_autoTimeEnabled() { - Script script = new Script() - .pokeAutoOriginPriorities(ORIGIN_EXTERNAL) - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_EXTERNAL) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); ExternalTimeSuggestion timeSuggestion = script.generateExternalTimeSuggestion(ARBITRARY_TEST_TIME); @@ -644,9 +665,11 @@ public class TimeDetectorStrategyImplTest { @Test public void testSuggestExternalTime_autoTimeDisabled() { - Script script = new Script() - .pokeAutoOriginPriorities(ORIGIN_EXTERNAL) - .simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_DISABLED) + .setOriginPriorities(ORIGIN_EXTERNAL) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); ExternalTimeSuggestion timeSuggestion = script.generateExternalTimeSuggestion(ARBITRARY_TEST_TIME); @@ -658,9 +681,11 @@ public class TimeDetectorStrategyImplTest { @Test public void externalTimeSuggestion_ignoredWhenReferencedTimeIsInThePast() { - Script script = new Script() - .pokeAutoOriginPriorities(ORIGIN_EXTERNAL) - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_EXTERNAL) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); Instant suggestedTime = TIME_LOWER_BOUND.minus(Duration.ofDays(1)); ExternalTimeSuggestion timeSuggestion = @@ -673,9 +698,11 @@ public class TimeDetectorStrategyImplTest { @Test public void highPrioritySuggestionsBeatLowerPrioritySuggestions_telephonyNetworkOrigins() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); // Three obviously different times that could not be mistaken for each other. Instant networkTime1 = ARBITRARY_TEST_TIME; @@ -778,9 +805,11 @@ public class TimeDetectorStrategyImplTest { @Test public void highPrioritySuggestionsBeatLowerPrioritySuggestions_networkGnssOrigins() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_NETWORK, ORIGIN_GNSS); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_NETWORK, ORIGIN_GNSS) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); // Three obviously different times that could not be mistaken for each other. Instant gnssTime1 = ARBITRARY_TEST_TIME; @@ -883,9 +912,11 @@ public class TimeDetectorStrategyImplTest { @Test public void highPrioritySuggestionsBeatLowerPrioritySuggestions_networkExternalOrigins() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_NETWORK, ORIGIN_EXTERNAL); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_NETWORK, ORIGIN_EXTERNAL) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); // Three obviously different times that could not be mistaken for each other. Instant externalTime1 = ARBITRARY_TEST_TIME; @@ -988,10 +1019,12 @@ public class TimeDetectorStrategyImplTest { @Test public void whenAllTimeSuggestionsAreAvailable_higherPriorityWins_lowerPriorityComesFirst() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK, ORIGIN_EXTERNAL, - ORIGIN_GNSS); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK, ORIGIN_EXTERNAL, + ORIGIN_GNSS) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); Instant networkTime = ARBITRARY_TEST_TIME; Instant externalTime = ARBITRARY_TEST_TIME.plus(Duration.ofDays(15)); @@ -1020,10 +1053,12 @@ public class TimeDetectorStrategyImplTest { @Test public void whenAllTimeSuggestionsAreAvailable_higherPriorityWins_higherPriorityComesFirst() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK, - ORIGIN_EXTERNAL, ORIGIN_GNSS); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK, ORIGIN_EXTERNAL, + ORIGIN_GNSS) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); Instant networkTime = ARBITRARY_TEST_TIME; Instant telephonyTime = ARBITRARY_TEST_TIME.plus(Duration.ofDays(30)); @@ -1052,9 +1087,11 @@ public class TimeDetectorStrategyImplTest { @Test public void whenHighestPrioritySuggestionIsNotAvailable_fallbacksToNext() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); NetworkTimeSuggestion timeSuggestion = script.generateNetworkTimeSuggestion(ARBITRARY_TEST_TIME); @@ -1066,10 +1103,12 @@ public class TimeDetectorStrategyImplTest { @Test public void whenHigherPrioritySuggestionsAreNotAvailable_fallbacksToNext() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK, - ORIGIN_EXTERNAL, ORIGIN_GNSS); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY, ORIGIN_NETWORK, ORIGIN_EXTERNAL, + ORIGIN_GNSS) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); GnssTimeSuggestion timeSuggestion = script.generateGnssTimeSuggestion(ARBITRARY_TEST_TIME); @@ -1081,9 +1120,11 @@ public class TimeDetectorStrategyImplTest { @Test public void suggestionsFromTelephonyOriginNotInPriorityList_areIgnored() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_NETWORK); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_NETWORK) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); int slotIndex = ARBITRARY_SLOT_INDEX; Instant testTime = ARBITRARY_TEST_TIME; @@ -1097,9 +1138,11 @@ public class TimeDetectorStrategyImplTest { @Test public void suggestionsFromNetworkOriginNotInPriorityList_areIgnored() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); NetworkTimeSuggestion timeSuggestion = script.generateNetworkTimeSuggestion( ARBITRARY_TEST_TIME); @@ -1111,9 +1154,11 @@ public class TimeDetectorStrategyImplTest { @Test public void suggestionsFromGnssOriginNotInPriorityList_areIgnored() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); GnssTimeSuggestion timeSuggestion = script.generateGnssTimeSuggestion( ARBITRARY_TEST_TIME); @@ -1125,9 +1170,11 @@ public class TimeDetectorStrategyImplTest { @Test public void suggestionsFromExternalOriginNotInPriorityList_areIgnored() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); ExternalTimeSuggestion timeSuggestion = script.generateExternalTimeSuggestion( ARBITRARY_TEST_TIME); @@ -1139,36 +1186,44 @@ public class TimeDetectorStrategyImplTest { @Test public void autoOriginPrioritiesList_doesNotAffectManualSuggestion() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_DISABLED) + .setOriginPriorities(ORIGIN_TELEPHONY) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); ManualTimeSuggestion timeSuggestion = script.generateManualTimeSuggestion(ARBITRARY_TEST_TIME); - script.simulateManualTimeSuggestion(USER_ID, timeSuggestion, true /* expectedResult */) + script.simulateManualTimeSuggestion( + ARBITRARY_USER_ID, timeSuggestion, true /* expectedResult */) .verifySystemClockWasSetAndResetCallTracking(ARBITRARY_TEST_TIME.toEpochMilli()); } @Test public void manualY2038SuggestionsAreRejectedOnAffectedDevices() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_DISABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY) - .pokeDeviceHasY2038Issues(true); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_DISABLED) + .setOriginPriorities(ORIGIN_TELEPHONY) + .setDeviceHasY2038Issue(true) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); Instant y2038IssueTime = Instant.ofEpochMilli((1L + Integer.MAX_VALUE) * 1000L); ManualTimeSuggestion timeSuggestion = script.generateManualTimeSuggestion(y2038IssueTime); - script.simulateManualTimeSuggestion(USER_ID, timeSuggestion, false /* expectedResult */) + script.simulateManualTimeSuggestion( + ARBITRARY_USER_ID, timeSuggestion, false /* expectedResult */) .verifySystemClockWasNotSetAndResetCallTracking(); } @Test public void telephonyY2038SuggestionsAreRejectedOnAffectedDevices() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY) - .pokeDeviceHasY2038Issues(true); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY) + .setDeviceHasY2038Issue(true) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); final int slotIndex = 0; Instant y2038IssueTime = Instant.ofEpochMilli((1L + Integer.MAX_VALUE) * 1000L); @@ -1180,10 +1235,12 @@ public class TimeDetectorStrategyImplTest { @Test public void telephonyY2038SuggestionsAreNotRejectedOnUnaffectedDevices() { - Script script = new Script() - .simulateConfigurationInternalChange(CONFIG_AUTO_ENABLED) - .pokeAutoOriginPriorities(ORIGIN_TELEPHONY) - .pokeDeviceHasY2038Issues(false); + ConfigurationInternal configInternal = + new ConfigurationInternal.Builder(CONFIG_AUTO_ENABLED) + .setOriginPriorities(ORIGIN_TELEPHONY) + .setDeviceHasY2038Issue(false) + .build(); + Script script = new Script().simulateConfigurationInternalChange(configInternal); final int slotIndex = 0; Instant y2038IssueTime = Instant.ofEpochMilli((1L + Integer.MAX_VALUE) * 1000L); @@ -1203,10 +1260,7 @@ public class TimeDetectorStrategyImplTest { private boolean mWakeLockAcquired; private long mElapsedRealtimeMillis; private long mSystemClockMillis; - private int mSystemClockUpdateThresholdMillis = 2000; - private int[] mAutoOriginPriorities = PROVIDERS_PRIORITY; private ConfigurationChangeListener mConfigurationInternalChangeListener; - private boolean mDeviceHas2038Issues = false; // Tracking operations. private boolean mSystemClockWasSet; @@ -1225,21 +1279,6 @@ public class TimeDetectorStrategyImplTest { mConfigurationInternalChangeListener = Objects.requireNonNull(listener); } - @Override - public int systemClockUpdateThresholdMillis() { - return mSystemClockUpdateThresholdMillis; - } - - @Override - public Instant autoTimeLowerBound() { - return TIME_LOWER_BOUND; - } - - @Override - public int[] autoOriginPriorities() { - return mAutoOriginPriorities; - } - @Override public ConfigurationInternal getCurrentUserConfigurationInternal() { return mConfigurationInternal; @@ -1276,15 +1315,6 @@ public class TimeDetectorStrategyImplTest { mWakeLockAcquired = false; } - public void setDeviceHas2038Issues(boolean hasIssues) { - mDeviceHas2038Issues = hasIssues; - } - - @Override - public boolean deviceHasY2038Issue() { - return mDeviceHas2038Issues; - } - // Methods below are for managing the fake's behavior. void simulateConfigurationInternalChange(ConfigurationInternal configurationInternal) { @@ -1292,10 +1322,6 @@ public class TimeDetectorStrategyImplTest { mConfigurationInternalChangeListener.onChange(); } - void pokeSystemClockUpdateThreshold(int thresholdMillis) { - mSystemClockUpdateThresholdMillis = thresholdMillis; - } - void pokeElapsedRealtimeMillis(long elapsedRealtimeMillis) { mElapsedRealtimeMillis = elapsedRealtimeMillis; } @@ -1304,10 +1330,6 @@ public class TimeDetectorStrategyImplTest { mSystemClockMillis = systemClockMillis; } - void pokeAutoOriginPriorities(@Origin int[] autoOriginPriorities) { - mAutoOriginPriorities = autoOriginPriorities; - } - long peekElapsedRealtimeMillis() { return mElapsedRealtimeMillis; } @@ -1355,21 +1377,6 @@ public class TimeDetectorStrategyImplTest { mTimeDetectorStrategy = new TimeDetectorStrategyImpl(mFakeEnvironment); } - Script pokeThresholds(int systemClockUpdateThreshold) { - mFakeEnvironment.pokeSystemClockUpdateThreshold(systemClockUpdateThreshold); - return this; - } - - Script pokeAutoOriginPriorities(@Origin int... autoOriginPriorities) { - mFakeEnvironment.pokeAutoOriginPriorities(autoOriginPriorities); - return this; - } - - Script pokeDeviceHasY2038Issues(boolean hasIssues) { - mFakeEnvironment.setDeviceHas2038Issues(hasIssues); - return this; - } - long peekElapsedRealtimeMillis() { return mFakeEnvironment.peekElapsedRealtimeMillis(); }