From e932ab9a5e535ba555d7206908569a15e45f2ee0 Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Fri, 30 Sep 2022 18:10:37 +0100 Subject: [PATCH] Remove usages of TimestampedValue Remove usages of TimestampedValue now we have UnixEpochTime, which is the same thing but intended to be public and without the use of generic Long (so no null checks needed). Bug: 236612872 Test: Build only Change-Id: I340754389536c09613d8c092ce5005a7eec605ef --- .../app/time/ExternalTimeSuggestion.java | 11 +-- core/java/android/app/time/TimeManager.java | 6 +- .../app/time/TimeZoneCapabilities.java | 17 ++-- core/java/android/app/time/UnixEpochTime.java | 7 -- .../timedetector/ManualTimeSuggestion.java | 6 +- .../timedetector/TelephonyTimeSuggestion.java | 37 +++---- .../app/timedetector/TimeDetector.java | 7 +- .../timedetector/TimeSuggestionHelper.java | 29 +++--- .../app/time/ExternalTimeSuggestionTest.java | 6 +- .../ManualTimeSuggestionTest.java | 19 ++-- .../TelephonyTimeSuggestionTest.java | 20 ++-- .../timedetector/GnssTimeSuggestion.java | 6 +- .../timedetector/GnssTimeUpdateService.java | 7 +- .../timedetector/NetworkTimeSuggestion.java | 27 +++--- .../NetworkTimeUpdateService.java | 4 +- .../timedetector/TimeDetectorStrategy.java | 10 -- .../TimeDetectorStrategyImpl.java | 97 +++++++++---------- .../TimeZoneDetectorStrategyImpl.java | 4 +- .../timedetector/GnssTimeSuggestionTest.java | 20 ++-- .../GnssTimeUpdateServiceTest.java | 6 +- .../NetworkTimeSuggestionTest.java | 22 ++--- .../TimeDetectorInternalImplTest.java | 6 +- .../timedetector/TimeDetectorServiceTest.java | 9 +- .../TimeDetectorStrategyImplTest.java | 65 ++++++------- .../TimeDetectorStrategyTest.java | 47 --------- 25 files changed, 203 insertions(+), 292 deletions(-) delete mode 100644 services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyTest.java diff --git a/core/java/android/app/time/ExternalTimeSuggestion.java b/core/java/android/app/time/ExternalTimeSuggestion.java index a7828ab4c9dcf..f4826ecea894f 100644 --- a/core/java/android/app/time/ExternalTimeSuggestion.java +++ b/core/java/android/app/time/ExternalTimeSuggestion.java @@ -24,7 +24,6 @@ import android.app.timedetector.TimeSuggestionHelper; import android.os.Parcel; import android.os.Parcelable; import android.os.ShellCommand; -import android.os.TimestampedValue; import java.io.PrintWriter; import java.util.List; @@ -48,9 +47,9 @@ import java.util.Objects; * *

The creator of an external suggestion is expected to be separate Android process, e.g. a * process integrating with the external time source via a HAL or local network. The creator must - * capture the elapsed realtime reference clock, e.g. via {@link SystemClock#elapsedRealtime()}, - * when the Unix epoch time is first obtained (usually under a wakelock). This enables Android to - * adjust for latency introduced between suggestion creation and eventual use. Adjustments for other + * capture the elapsed realtime clock value, e.g. via {@link SystemClock#elapsedRealtime()}, when + * the Unix epoch time is first obtained (usually under a wakelock). This enables Android to adjust + * for latency introduced between suggestion creation and eventual use. Adjustments for other * sources of latency, i.e. those before the external time suggestion is created, must be handled by * the creator. * @@ -97,7 +96,7 @@ public final class ExternalTimeSuggestion implements Parcelable { public ExternalTimeSuggestion(@ElapsedRealtimeLong long elapsedRealtimeMillis, @CurrentTimeMillisLong long suggestionMillis) { mTimeSuggestionHelper = new TimeSuggestionHelper(ExternalTimeSuggestion.class, - new TimestampedValue<>(elapsedRealtimeMillis, suggestionMillis)); + new UnixEpochTime(elapsedRealtimeMillis, suggestionMillis)); } private ExternalTimeSuggestion(@NonNull TimeSuggestionHelper helper) { @@ -118,7 +117,7 @@ public final class ExternalTimeSuggestion implements Parcelable { * {@hide} */ @NonNull - public TimestampedValue getUnixEpochTime() { + public UnixEpochTime getUnixEpochTime() { return mTimeSuggestionHelper.getUnixEpochTime(); } diff --git a/core/java/android/app/time/TimeManager.java b/core/java/android/app/time/TimeManager.java index 6a833fd772afa..9f66f094786b9 100644 --- a/core/java/android/app/time/TimeManager.java +++ b/core/java/android/app/time/TimeManager.java @@ -28,7 +28,6 @@ import android.content.Context; import android.os.RemoteException; import android.os.ServiceManager; import android.os.ServiceManager.ServiceNotFoundException; -import android.os.TimestampedValue; import android.util.ArrayMap; import android.util.Log; @@ -339,10 +338,7 @@ public final class TimeManager { Log.d(TAG, "setTime called: " + unixEpochTime); } try { - TimestampedValue manualTime = new TimestampedValue<>( - unixEpochTime.getElapsedRealtimeMillis(), - unixEpochTime.getUnixEpochTimeMillis()); - ManualTimeSuggestion manualTimeSuggestion = new ManualTimeSuggestion(manualTime); + ManualTimeSuggestion manualTimeSuggestion = new ManualTimeSuggestion(unixEpochTime); manualTimeSuggestion.addDebugInfo("TimeManager.setTime()"); manualTimeSuggestion.addDebugInfo("UID: " + android.os.Process.myUid()); manualTimeSuggestion.addDebugInfo("UserHandle: " + android.os.Process.myUserHandle()); diff --git a/core/java/android/app/time/TimeZoneCapabilities.java b/core/java/android/app/time/TimeZoneCapabilities.java index 5d4629f814937..2f147cef9ffe7 100644 --- a/core/java/android/app/time/TimeZoneCapabilities.java +++ b/core/java/android/app/time/TimeZoneCapabilities.java @@ -41,16 +41,15 @@ import java.util.Objects; @SystemApi public final class TimeZoneCapabilities implements Parcelable { - public static final @NonNull Creator CREATOR = - new Creator() { - public TimeZoneCapabilities createFromParcel(Parcel in) { - return TimeZoneCapabilities.createFromParcel(in); - } + public static final @NonNull Creator CREATOR = new Creator<>() { + public TimeZoneCapabilities createFromParcel(Parcel in) { + return TimeZoneCapabilities.createFromParcel(in); + } - public TimeZoneCapabilities[] newArray(int size) { - return new TimeZoneCapabilities[size]; - } - }; + public TimeZoneCapabilities[] newArray(int size) { + return new TimeZoneCapabilities[size]; + } + }; /** * The user the capabilities are for. This is used for object equality and debugging but there diff --git a/core/java/android/app/time/UnixEpochTime.java b/core/java/android/app/time/UnixEpochTime.java index 2683547e92092..f1a176eacc9b9 100644 --- a/core/java/android/app/time/UnixEpochTime.java +++ b/core/java/android/app/time/UnixEpochTime.java @@ -23,7 +23,6 @@ import android.os.Parcel; import android.os.Parcelable; import android.os.ShellCommand; import android.os.SystemClock; -import android.os.TimestampedValue; import java.io.PrintWriter; import java.util.Objects; @@ -181,10 +180,4 @@ public final class UnixEpochTime implements Parcelable { @NonNull UnixEpochTime one, @NonNull UnixEpochTime two) { return one.mElapsedRealtimeMillis - two.mElapsedRealtimeMillis; } - - // TODO(b/246256335) Switch to using UnixEpochTime where possible and remove this method. - /** @hide */ - public TimestampedValue toTimestampedValue() { - return new TimestampedValue<>(mElapsedRealtimeMillis, mUnixEpochTimeMillis); - } } diff --git a/core/java/android/app/timedetector/ManualTimeSuggestion.java b/core/java/android/app/timedetector/ManualTimeSuggestion.java index b447799eb84c9..0be42675d95f1 100644 --- a/core/java/android/app/timedetector/ManualTimeSuggestion.java +++ b/core/java/android/app/timedetector/ManualTimeSuggestion.java @@ -18,10 +18,10 @@ package android.app.timedetector; import android.annotation.NonNull; import android.annotation.Nullable; +import android.app.time.UnixEpochTime; import android.os.Parcel; import android.os.Parcelable; import android.os.ShellCommand; -import android.os.TimestampedValue; import java.io.PrintWriter; import java.util.List; @@ -51,7 +51,7 @@ public final class ManualTimeSuggestion implements Parcelable { @NonNull private final TimeSuggestionHelper mTimeSuggestionHelper; - public ManualTimeSuggestion(@NonNull TimestampedValue unixEpochTime) { + public ManualTimeSuggestion(@NonNull UnixEpochTime unixEpochTime) { mTimeSuggestionHelper = new TimeSuggestionHelper(ManualTimeSuggestion.class, unixEpochTime); } @@ -70,7 +70,7 @@ public final class ManualTimeSuggestion implements Parcelable { } @NonNull - public TimestampedValue getUnixEpochTime() { + public UnixEpochTime getUnixEpochTime() { return mTimeSuggestionHelper.getUnixEpochTime(); } diff --git a/core/java/android/app/timedetector/TelephonyTimeSuggestion.java b/core/java/android/app/timedetector/TelephonyTimeSuggestion.java index e0347c07e52a1..f149a261f9bbb 100644 --- a/core/java/android/app/timedetector/TelephonyTimeSuggestion.java +++ b/core/java/android/app/timedetector/TelephonyTimeSuggestion.java @@ -18,10 +18,10 @@ package android.app.timedetector; import android.annotation.NonNull; import android.annotation.Nullable; +import android.app.time.UnixEpochTime; import android.os.Parcel; import android.os.Parcelable; import android.os.ShellCommand; -import android.os.TimestampedValue; import java.io.PrintWriter; import java.util.ArrayList; @@ -67,7 +67,7 @@ public final class TelephonyTimeSuggestion implements Parcelable { }; private final int mSlotIndex; - @Nullable private final TimestampedValue mUnixEpochTime; + @Nullable private final UnixEpochTime mUnixEpochTime; @Nullable private ArrayList mDebugInfo; private TelephonyTimeSuggestion(Builder builder) { @@ -78,13 +78,13 @@ public final class TelephonyTimeSuggestion implements Parcelable { private static TelephonyTimeSuggestion createFromParcel(Parcel in) { int slotIndex = in.readInt(); - TimestampedValue unixEpochTime = - in.readParcelable(null /* classLoader */, android.os.TimestampedValue.class); + UnixEpochTime unixEpochTime = + in.readParcelable(null /* classLoader */, UnixEpochTime.class); TelephonyTimeSuggestion suggestion = new TelephonyTimeSuggestion.Builder(slotIndex) .setUnixEpochTime(unixEpochTime) .build(); @SuppressWarnings("unchecked") - ArrayList debugInfo = (ArrayList) in.readArrayList( + ArrayList debugInfo = in.readArrayList( null /* classLoader */, java.lang.String.class); if (debugInfo != null) { suggestion.addDebugInfo(debugInfo); @@ -96,7 +96,7 @@ public final class TelephonyTimeSuggestion implements Parcelable { public static TelephonyTimeSuggestion parseCommandLineArg(@NonNull ShellCommand cmd) throws IllegalArgumentException { Integer slotIndex = null; - Long referenceTimeMillis = null; + Long elapsedRealtimeMillis = null; Long unixEpochTimeMillis = null; String opt; while ((opt = cmd.getNextArg()) != null) { @@ -105,8 +105,9 @@ public final class TelephonyTimeSuggestion implements Parcelable { slotIndex = Integer.parseInt(cmd.getNextArgRequired()); break; } - case "--reference_time": { - referenceTimeMillis = Long.parseLong(cmd.getNextArgRequired()); + case "--reference_time": + case "--elapsed_realtime": { + elapsedRealtimeMillis = Long.parseLong(cmd.getNextArgRequired()); break; } case "--unix_epoch_time": { @@ -122,15 +123,14 @@ public final class TelephonyTimeSuggestion implements Parcelable { if (slotIndex == null) { throw new IllegalArgumentException("No slotIndex specified."); } - if (referenceTimeMillis == null) { - throw new IllegalArgumentException("No referenceTimeMillis specified."); + if (elapsedRealtimeMillis == null) { + throw new IllegalArgumentException("No elapsedRealtimeMillis specified."); } if (unixEpochTimeMillis == null) { throw new IllegalArgumentException("No unixEpochTimeMillis specified."); } - TimestampedValue timeSignal = - new TimestampedValue<>(referenceTimeMillis, unixEpochTimeMillis); + UnixEpochTime timeSignal = new UnixEpochTime(elapsedRealtimeMillis, unixEpochTimeMillis); Builder builder = new Builder(slotIndex) .setUnixEpochTime(timeSignal) .addDebugInfo("Command line injection"); @@ -141,7 +141,7 @@ public final class TelephonyTimeSuggestion implements Parcelable { public static void printCommandLineOpts(PrintWriter pw) { pw.println("Telephony suggestion options:"); pw.println(" --slot_index "); - pw.println(" --reference_time "); + pw.println(" --elapsed_realtime "); pw.println(" --unix_epoch_time "); pw.println(); pw.println("See " + TelephonyTimeSuggestion.class.getName() + " for more information"); @@ -174,7 +174,7 @@ public final class TelephonyTimeSuggestion implements Parcelable { *

See {@link TelephonyTimeSuggestion} for more information about {@code unixEpochTime}. */ @Nullable - public TimestampedValue getUnixEpochTime() { + public UnixEpochTime getUnixEpochTime() { return mUnixEpochTime; } @@ -247,7 +247,7 @@ public final class TelephonyTimeSuggestion implements Parcelable { */ public static final class Builder { private final int mSlotIndex; - @Nullable private TimestampedValue mUnixEpochTime; + @Nullable private UnixEpochTime mUnixEpochTime; @Nullable private List mDebugInfo; /** @@ -265,12 +265,7 @@ public final class TelephonyTimeSuggestion implements Parcelable { *

See {@link TelephonyTimeSuggestion} for more information about {@code unixEpochTime}. */ @NonNull - public Builder setUnixEpochTime(@Nullable TimestampedValue unixEpochTime) { - if (unixEpochTime != null) { - // unixEpochTime can be null, but the value it holds cannot. - Objects.requireNonNull(unixEpochTime.getValue()); - } - + public Builder setUnixEpochTime(@Nullable UnixEpochTime unixEpochTime) { mUnixEpochTime = unixEpochTime; return this; } diff --git a/core/java/android/app/timedetector/TimeDetector.java b/core/java/android/app/timedetector/TimeDetector.java index 9d996adc051a9..f95d6d3bb0563 100644 --- a/core/java/android/app/timedetector/TimeDetector.java +++ b/core/java/android/app/timedetector/TimeDetector.java @@ -19,9 +19,9 @@ package android.app.timedetector; import android.annotation.NonNull; import android.annotation.RequiresPermission; import android.annotation.SystemService; +import android.app.time.UnixEpochTime; import android.content.Context; import android.os.SystemClock; -import android.os.TimestampedValue; /** * The interface through which system components can query and send signals to the @@ -108,9 +108,8 @@ public interface TimeDetector { * @hide */ static ManualTimeSuggestion createManualTimeSuggestion(long when, String why) { - TimestampedValue utcTime = - new TimestampedValue<>(SystemClock.elapsedRealtime(), when); - ManualTimeSuggestion manualTimeSuggestion = new ManualTimeSuggestion(utcTime); + UnixEpochTime unixEpochTime = new UnixEpochTime(SystemClock.elapsedRealtime(), when); + ManualTimeSuggestion manualTimeSuggestion = new ManualTimeSuggestion(unixEpochTime); manualTimeSuggestion.addDebugInfo(why); return manualTimeSuggestion; } diff --git a/core/java/android/app/timedetector/TimeSuggestionHelper.java b/core/java/android/app/timedetector/TimeSuggestionHelper.java index e89839c591d9b..67dc6b8d4e1b5 100644 --- a/core/java/android/app/timedetector/TimeSuggestionHelper.java +++ b/core/java/android/app/timedetector/TimeSuggestionHelper.java @@ -18,9 +18,9 @@ package android.app.timedetector; import android.annotation.NonNull; import android.annotation.Nullable; +import android.app.time.UnixEpochTime; import android.os.Parcel; import android.os.ShellCommand; -import android.os.TimestampedValue; import java.io.PrintWriter; import java.util.ArrayList; @@ -51,20 +51,19 @@ import java.util.Objects; public final class TimeSuggestionHelper { @NonNull private final Class mHelpedClass; - @NonNull private final TimestampedValue mUnixEpochTime; + @NonNull private final UnixEpochTime mUnixEpochTime; @Nullable private ArrayList mDebugInfo; /** Creates a helper for the specified class, containing the supplied properties. */ public TimeSuggestionHelper(@NonNull Class helpedClass, - @NonNull TimestampedValue unixEpochTime) { + @NonNull UnixEpochTime unixEpochTime) { mHelpedClass = Objects.requireNonNull(helpedClass); mUnixEpochTime = Objects.requireNonNull(unixEpochTime); - Objects.requireNonNull(unixEpochTime.getValue()); } /** See {@link TimeSuggestionHelper} for property details. */ @NonNull - public TimestampedValue getUnixEpochTime() { + public UnixEpochTime getUnixEpochTime() { return mUnixEpochTime; } @@ -146,8 +145,8 @@ public final class TimeSuggestionHelper { public static TimeSuggestionHelper handleCreateFromParcel(@NonNull Class helpedClass, @NonNull Parcel in) { @SuppressWarnings("unchecked") - TimestampedValue unixEpochTime = in.readParcelable( - null /* classLoader */, TimestampedValue.class); + UnixEpochTime unixEpochTime = + in.readParcelable(null /* classLoader */, UnixEpochTime.class); TimeSuggestionHelper suggestionHelper = new TimeSuggestionHelper(helpedClass, unixEpochTime); suggestionHelper.mDebugInfo = in.readArrayList(null /* classLoader */, String.class); @@ -164,13 +163,14 @@ public final class TimeSuggestionHelper { public static TimeSuggestionHelper handleParseCommandLineArg( @NonNull Class helpedClass, @NonNull ShellCommand cmd) throws IllegalArgumentException { - Long referenceTimeMillis = null; + Long elapsedRealtimeMillis = null; Long unixEpochTimeMillis = null; String opt; while ((opt = cmd.getNextArg()) != null) { switch (opt) { - case "--reference_time": { - referenceTimeMillis = Long.parseLong(cmd.getNextArgRequired()); + case "--reference_time": + case "--elapsed_realtime": { + elapsedRealtimeMillis = Long.parseLong(cmd.getNextArgRequired()); break; } case "--unix_epoch_time": { @@ -183,15 +183,14 @@ public final class TimeSuggestionHelper { } } - if (referenceTimeMillis == null) { + if (elapsedRealtimeMillis == null) { throw new IllegalArgumentException("No referenceTimeMillis specified."); } if (unixEpochTimeMillis == null) { throw new IllegalArgumentException("No unixEpochTimeMillis specified."); } - TimestampedValue timeSignal = - new TimestampedValue<>(referenceTimeMillis, unixEpochTimeMillis); + UnixEpochTime timeSignal = new UnixEpochTime(elapsedRealtimeMillis, unixEpochTimeMillis); TimeSuggestionHelper suggestionHelper = new TimeSuggestionHelper(helpedClass, timeSignal); suggestionHelper.addDebugInfo("Command line injection"); return suggestionHelper; @@ -201,8 +200,8 @@ public final class TimeSuggestionHelper { public static void handlePrintCommandLineOpts( @NonNull PrintWriter pw, @NonNull String typeName, @NonNull Class clazz) { pw.printf("%s suggestion options:\n", typeName); - pw.println(" --reference_time - the elapsed realtime millis when" - + " unix epoch time was read"); + pw.println(" --elapsed_realtime - the elapsed realtime millis" + + " when unix epoch time was read"); pw.println(" --unix_epoch_time "); pw.println(); pw.println("See " + clazz.getName() + " for more information"); diff --git a/core/tests/coretests/src/android/app/time/ExternalTimeSuggestionTest.java b/core/tests/coretests/src/android/app/time/ExternalTimeSuggestionTest.java index 90b33058d4e88..92149ebb6cab0 100644 --- a/core/tests/coretests/src/android/app/time/ExternalTimeSuggestionTest.java +++ b/core/tests/coretests/src/android/app/time/ExternalTimeSuggestionTest.java @@ -40,14 +40,14 @@ public class ExternalTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_noUnixEpochTime() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321"); + "--elapsed_realtime 54321"); ExternalTimeSuggestion.parseCommandLineArg(testShellCommand); } @Test public void testParseCommandLineArg_validSuggestion() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345"); + "--elapsed_realtime 54321 --unix_epoch_time 12345"); ExternalTimeSuggestion expectedSuggestion = new ExternalTimeSuggestion(54321L, 12345L); ExternalTimeSuggestion actualSuggestion = ExternalTimeSuggestion.parseCommandLineArg(testShellCommand); @@ -57,7 +57,7 @@ public class ExternalTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_unknownArgument() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + "--elapsed_realtime 54321 --unix_epoch_time 12345 --bad_arg 0"); ExternalTimeSuggestion.parseCommandLineArg(testShellCommand); } } diff --git a/core/tests/coretests/src/android/app/timedetector/ManualTimeSuggestionTest.java b/core/tests/coretests/src/android/app/timedetector/ManualTimeSuggestionTest.java index 94218cdea0edd..0c7c8c1803eb0 100644 --- a/core/tests/coretests/src/android/app/timedetector/ManualTimeSuggestionTest.java +++ b/core/tests/coretests/src/android/app/timedetector/ManualTimeSuggestionTest.java @@ -23,15 +23,14 @@ import static android.app.timezonedetector.ShellCommandTestSupport.createShellCo import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import android.app.time.UnixEpochTime; import android.os.ShellCommand; -import android.os.TimestampedValue; import org.junit.Test; public class ManualTimeSuggestionTest { - private static final TimestampedValue ARBITRARY_TIME = - new TimestampedValue<>(1111L, 2222L); + private static final UnixEpochTime ARBITRARY_TIME = new UnixEpochTime(1111L, 2222L); @Test public void testEquals() { @@ -42,9 +41,9 @@ public class ManualTimeSuggestionTest { assertEquals(one, two); assertEquals(two, one); - TimestampedValue differentTime = new TimestampedValue<>( - ARBITRARY_TIME.getReferenceTimeMillis() + 1, - ARBITRARY_TIME.getValue()); + UnixEpochTime differentTime = new UnixEpochTime( + ARBITRARY_TIME.getElapsedRealtimeMillis() + 1, + ARBITRARY_TIME.getUnixEpochTimeMillis()); ManualTimeSuggestion three = new ManualTimeSuggestion(differentTime); assertNotEquals(one, three); assertNotEquals(three, one); @@ -76,15 +75,15 @@ public class ManualTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_noUnixEpochTime() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321"); + "--elapsed_realtime 54321"); ManualTimeSuggestion.parseCommandLineArg(testShellCommand); } @Test public void testParseCommandLineArg_validSuggestion() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345"); - TimestampedValue timeSignal = new TimestampedValue<>(54321L, 12345L); + "--elapsed_realtime 54321 --unix_epoch_time 12345"); + UnixEpochTime timeSignal = new UnixEpochTime(54321L, 12345L); ManualTimeSuggestion expectedSuggestion = new ManualTimeSuggestion(timeSignal); ManualTimeSuggestion actualSuggestion = ManualTimeSuggestion.parseCommandLineArg(testShellCommand); @@ -94,7 +93,7 @@ public class ManualTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_unknownArgument() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + "--elapsed_realtime 54321 --unix_epoch_time 12345 --bad_arg 0"); ManualTimeSuggestion.parseCommandLineArg(testShellCommand); } } diff --git a/core/tests/coretests/src/android/app/timedetector/TelephonyTimeSuggestionTest.java b/core/tests/coretests/src/android/app/timedetector/TelephonyTimeSuggestionTest.java index bb995a8526372..26cb902319312 100644 --- a/core/tests/coretests/src/android/app/timedetector/TelephonyTimeSuggestionTest.java +++ b/core/tests/coretests/src/android/app/timedetector/TelephonyTimeSuggestionTest.java @@ -23,8 +23,8 @@ import static android.app.timezonedetector.ShellCommandTestSupport.createShellCo import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import android.app.time.UnixEpochTime; import android.os.ShellCommand; -import android.os.TimestampedValue; import org.junit.Test; @@ -47,13 +47,13 @@ public class TelephonyTimeSuggestionTest { assertEquals(two, one); } - builder1.setUnixEpochTime(new TimestampedValue<>(1111L, 2222L)); + builder1.setUnixEpochTime(new UnixEpochTime(1111L, 2222L)); { TelephonyTimeSuggestion one = builder1.build(); assertEquals(one, one); } - builder2.setUnixEpochTime(new TimestampedValue<>(1111L, 2222L)); + builder2.setUnixEpochTime(new UnixEpochTime(1111L, 2222L)); { TelephonyTimeSuggestion one = builder1.build(); TelephonyTimeSuggestion two = builder2.build(); @@ -63,7 +63,7 @@ public class TelephonyTimeSuggestionTest { TelephonyTimeSuggestion.Builder builder3 = new TelephonyTimeSuggestion.Builder(SLOT_INDEX + 1); - builder3.setUnixEpochTime(new TimestampedValue<>(1111L, 2222L)); + builder3.setUnixEpochTime(new UnixEpochTime(1111L, 2222L)); { TelephonyTimeSuggestion one = builder1.build(); TelephonyTimeSuggestion three = builder3.build(); @@ -86,7 +86,7 @@ public class TelephonyTimeSuggestionTest { TelephonyTimeSuggestion.Builder builder = new TelephonyTimeSuggestion.Builder(SLOT_INDEX); assertRoundTripParcelable(builder.build()); - builder.setUnixEpochTime(new TimestampedValue<>(1111L, 2222L)); + builder.setUnixEpochTime(new UnixEpochTime(1111L, 2222L)); assertRoundTripParcelable(builder.build()); // DebugInfo should also be stored (but is not checked by equals() @@ -101,7 +101,7 @@ public class TelephonyTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_noSlotIndex() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345"); + "--elapsed_realtime 54321 --unix_epoch_time 12345"); TelephonyTimeSuggestion.parseCommandLineArg(testShellCommand); } @@ -115,17 +115,17 @@ public class TelephonyTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_noUnixEpochTime() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--slot_index 0 --reference_time 54321"); + "--slot_index 0 --elapsed_realtime 54321"); TelephonyTimeSuggestion.parseCommandLineArg(testShellCommand); } @Test public void testParseCommandLineArg_validSuggestion() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--slot_index 0 --reference_time 54321 --unix_epoch_time 12345"); + "--slot_index 0 --elapsed_realtime 54321 --unix_epoch_time 12345"); TelephonyTimeSuggestion expectedSuggestion = new TelephonyTimeSuggestion.Builder(0) - .setUnixEpochTime(new TimestampedValue<>(54321L, 12345L)) + .setUnixEpochTime(new UnixEpochTime(54321L, 12345L)) .build(); TelephonyTimeSuggestion actualSuggestion = TelephonyTimeSuggestion.parseCommandLineArg(testShellCommand); @@ -135,7 +135,7 @@ public class TelephonyTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_unknownArgument() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--slot_index 0 --reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + "--slot_index 0 --elapsed_realtime 54321 --unix_epoch_time 12345 --bad_arg 0"); TelephonyTimeSuggestion.parseCommandLineArg(testShellCommand); } } diff --git a/services/core/java/com/android/server/timedetector/GnssTimeSuggestion.java b/services/core/java/com/android/server/timedetector/GnssTimeSuggestion.java index 33499754fbc5d..a7b1e233929af 100644 --- a/services/core/java/com/android/server/timedetector/GnssTimeSuggestion.java +++ b/services/core/java/com/android/server/timedetector/GnssTimeSuggestion.java @@ -17,9 +17,9 @@ package com.android.server.timedetector; import android.annotation.NonNull; +import android.app.time.UnixEpochTime; import android.app.timedetector.TimeSuggestionHelper; import android.os.ShellCommand; -import android.os.TimestampedValue; import java.io.PrintWriter; import java.util.List; @@ -34,7 +34,7 @@ public final class GnssTimeSuggestion { @NonNull private final TimeSuggestionHelper mTimeSuggestionHelper; - public GnssTimeSuggestion(@NonNull TimestampedValue unixEpochTime) { + public GnssTimeSuggestion(@NonNull UnixEpochTime unixEpochTime) { mTimeSuggestionHelper = new TimeSuggestionHelper(GnssTimeSuggestion.class, unixEpochTime); } @@ -43,7 +43,7 @@ public final class GnssTimeSuggestion { } @NonNull - public TimestampedValue getUnixEpochTime() { + public UnixEpochTime getUnixEpochTime() { return mTimeSuggestionHelper.getUnixEpochTime(); } diff --git a/services/core/java/com/android/server/timedetector/GnssTimeUpdateService.java b/services/core/java/com/android/server/timedetector/GnssTimeUpdateService.java index 421f7ce9cf581..fef7148ed0968 100644 --- a/services/core/java/com/android/server/timedetector/GnssTimeUpdateService.java +++ b/services/core/java/com/android/server/timedetector/GnssTimeUpdateService.java @@ -20,6 +20,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.app.AlarmManager; +import android.app.time.UnixEpochTime; import android.content.Context; import android.location.LocationListener; import android.location.LocationManager; @@ -31,7 +32,6 @@ import android.os.Handler; import android.os.ResultReceiver; import android.os.ShellCallback; import android.os.SystemClock; -import android.os.TimestampedValue; import android.util.LocalLog; import android.util.Log; @@ -122,7 +122,7 @@ public final class GnssTimeUpdateService extends Binder { @GuardedBy("mLock") @Nullable private AlarmManager.OnAlarmListener mAlarmListener; @GuardedBy("mLock") @Nullable private LocationListener mLocationListener; - @Nullable private volatile TimestampedValue mLastSuggestedGnssTime; + @Nullable private volatile UnixEpochTime mLastSuggestedGnssTime; @VisibleForTesting GnssTimeUpdateService(@NonNull Context context, @NonNull AlarmManager alarmManager, @@ -263,8 +263,7 @@ public final class GnssTimeUpdateService extends Binder { long gnssUnixEpochTimeMillis = locationTime.getUnixEpochTimeMillis(); long elapsedRealtimeMs = locationTime.getElapsedRealtimeNanos() / 1_000_000L; - TimestampedValue timeSignal = - new TimestampedValue<>(elapsedRealtimeMs, gnssUnixEpochTimeMillis); + UnixEpochTime timeSignal = new UnixEpochTime(elapsedRealtimeMs, gnssUnixEpochTimeMillis); mLastSuggestedGnssTime = timeSignal; GnssTimeSuggestion timeSuggestion = new GnssTimeSuggestion(timeSignal); diff --git a/services/core/java/com/android/server/timedetector/NetworkTimeSuggestion.java b/services/core/java/com/android/server/timedetector/NetworkTimeSuggestion.java index f62c7edb8349b..71c5d4281cab6 100644 --- a/services/core/java/com/android/server/timedetector/NetworkTimeSuggestion.java +++ b/services/core/java/com/android/server/timedetector/NetworkTimeSuggestion.java @@ -18,8 +18,8 @@ package com.android.server.timedetector; import android.annotation.NonNull; import android.annotation.Nullable; +import android.app.time.UnixEpochTime; import android.os.ShellCommand; -import android.os.TimestampedValue; import java.io.PrintWriter; import java.util.ArrayList; @@ -48,7 +48,7 @@ import java.util.Objects; */ public final class NetworkTimeSuggestion { - @NonNull private final TimestampedValue mUnixEpochTime; + @NonNull private final UnixEpochTime mUnixEpochTime; private final int mUncertaintyMillis; @Nullable private ArrayList mDebugInfo; @@ -57,8 +57,7 @@ public final class NetworkTimeSuggestion { * *

See {@link NetworkTimeSuggestion} for property details. */ - public NetworkTimeSuggestion( - @NonNull TimestampedValue unixEpochTime, int uncertaintyMillis) { + public NetworkTimeSuggestion(@NonNull UnixEpochTime unixEpochTime, int uncertaintyMillis) { mUnixEpochTime = Objects.requireNonNull(unixEpochTime); if (uncertaintyMillis < 0) { throw new IllegalArgumentException("uncertaintyMillis < 0"); @@ -68,7 +67,7 @@ public final class NetworkTimeSuggestion { /** See {@link NetworkTimeSuggestion} for property details. */ @NonNull - public TimestampedValue getUnixEpochTime() { + public UnixEpochTime getUnixEpochTime() { return mUnixEpochTime; } @@ -126,14 +125,15 @@ public final class NetworkTimeSuggestion { /** Parses command line args to create a {@link NetworkTimeSuggestion}. */ public static NetworkTimeSuggestion parseCommandLineArg(@NonNull ShellCommand cmd) throws IllegalArgumentException { - Long referenceTimeMillis = null; + Long elapsedRealtimeMillis = null; Long unixEpochTimeMillis = null; Integer uncertaintyMillis = null; String opt; while ((opt = cmd.getNextArg()) != null) { switch (opt) { - case "--reference_time": { - referenceTimeMillis = Long.parseLong(cmd.getNextArgRequired()); + case "--reference_time": + case "--elapsed_realtime": { + elapsedRealtimeMillis = Long.parseLong(cmd.getNextArgRequired()); break; } case "--unix_epoch_time": { @@ -150,8 +150,8 @@ public final class NetworkTimeSuggestion { } } - if (referenceTimeMillis == null) { - throw new IllegalArgumentException("No referenceTimeMillis specified."); + if (elapsedRealtimeMillis == null) { + throw new IllegalArgumentException("No elapsedRealtimeMillis specified."); } if (unixEpochTimeMillis == null) { throw new IllegalArgumentException("No unixEpochTimeMillis specified."); @@ -160,8 +160,7 @@ public final class NetworkTimeSuggestion { throw new IllegalArgumentException("No uncertaintyMillis specified."); } - TimestampedValue timeSignal = - new TimestampedValue<>(referenceTimeMillis, unixEpochTimeMillis); + UnixEpochTime timeSignal = new UnixEpochTime(elapsedRealtimeMillis, unixEpochTimeMillis); NetworkTimeSuggestion networkTimeSuggestion = new NetworkTimeSuggestion(timeSignal, uncertaintyMillis); networkTimeSuggestion.addDebugInfo("Command line injection"); @@ -171,8 +170,8 @@ public final class NetworkTimeSuggestion { /** Prints the command line args needed to create a {@link NetworkTimeSuggestion}. */ public static void printCommandLineOpts(PrintWriter pw) { pw.printf("%s suggestion options:\n", "Network"); - pw.println(" --reference_time - the elapsed realtime millis when" - + " unix epoch time was read"); + pw.println(" --elapsed_realtime - the elapsed realtime millis" + + " when unix epoch time was read"); pw.println(" --unix_epoch_time "); pw.println(" --uncertainty_millis - a positive error bound (+/-)" + " estimate for unix epoch time"); diff --git a/services/core/java/com/android/server/timedetector/NetworkTimeUpdateService.java b/services/core/java/com/android/server/timedetector/NetworkTimeUpdateService.java index 59c118d2f8ef5..4803011ae4f53 100644 --- a/services/core/java/com/android/server/timedetector/NetworkTimeUpdateService.java +++ b/services/core/java/com/android/server/timedetector/NetworkTimeUpdateService.java @@ -20,6 +20,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.app.AlarmManager; import android.app.PendingIntent; +import android.app.time.UnixEpochTime; import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; @@ -38,7 +39,6 @@ import android.os.PowerManager; import android.os.ResultReceiver; import android.os.ShellCallback; import android.os.SystemClock; -import android.os.TimestampedValue; import android.provider.Settings; import android.util.LocalLog; import android.util.Log; @@ -278,7 +278,7 @@ public class NetworkTimeUpdateService extends Binder { /** Suggests the time to the time detector. It may choose use it to set the system clock. */ private void makeNetworkTimeSuggestion( @NonNull TimeResult ntpResult, @NonNull String debugInfo) { - TimestampedValue timeSignal = new TimestampedValue<>( + UnixEpochTime timeSignal = new UnixEpochTime( ntpResult.getElapsedRealtimeMillis(), ntpResult.getTimeMillis()); NetworkTimeSuggestion timeSuggestion = new NetworkTimeSuggestion(timeSignal, ntpResult.getUncertaintyMillis()); diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorStrategy.java b/services/core/java/com/android/server/timedetector/TimeDetectorStrategy.java index ac2a391ca2c7a..bc86ed057fb62 100644 --- a/services/core/java/com/android/server/timedetector/TimeDetectorStrategy.java +++ b/services/core/java/com/android/server/timedetector/TimeDetectorStrategy.java @@ -24,7 +24,6 @@ import android.app.time.TimeState; import android.app.time.UnixEpochTime; import android.app.timedetector.ManualTimeSuggestion; import android.app.timedetector.TelephonyTimeSuggestion; -import android.os.TimestampedValue; import android.util.IndentingPrintWriter; import com.android.internal.util.Preconditions; @@ -108,15 +107,6 @@ public interface TimeDetectorStrategy extends Dumpable { // Utility methods below are to be moved to a better home when one becomes more obvious. - /** - * Adjusts the supplied time value by applying the difference between the reference time - * supplied and the reference time associated with the time. - */ - static long getTimeAt(@NonNull TimestampedValue timeValue, long referenceClockMillisNow) { - return (referenceClockMillisNow - timeValue.getReferenceTimeMillis()) - + timeValue.getValue(); - } - /** * Converts one of the {@code ORIGIN_} constants to a human readable string suitable for config * and debug usage. Throws an {@link IllegalArgumentException} if the value is unrecognized. diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java b/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java index 3235bf077305e..510896d258530 100644 --- a/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java +++ b/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java @@ -32,7 +32,6 @@ import android.app.timedetector.ManualTimeSuggestion; import android.app.timedetector.TelephonyTimeSuggestion; import android.content.Context; import android.os.Handler; -import android.os.TimestampedValue; import android.util.IndentingPrintWriter; import android.util.Slog; @@ -99,7 +98,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { // going through this strategy code. @GuardedBy("this") @Nullable - private TimestampedValue mLastAutoSystemClockTimeSet; + private UnixEpochTime mLastAutoSystemClockTimeSet; /** * A mapping from slotIndex to a time suggestion. We typically expect one or two mappings: @@ -209,7 +208,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } Objects.requireNonNull(suggestion); - final TimestampedValue newUnixEpochTime = suggestion.getUnixEpochTime(); + final UnixEpochTime newUnixEpochTime = suggestion.getUnixEpochTime(); if (!validateAutoSuggestionTime(newUnixEpochTime, suggestion)) { return; @@ -231,7 +230,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } Objects.requireNonNull(suggestion); - final TimestampedValue newUnixEpochTime = suggestion.getUnixEpochTime(); + final UnixEpochTime newUnixEpochTime = suggestion.getUnixEpochTime(); if (!validateAutoSuggestionTime(newUnixEpochTime, suggestion)) { return; @@ -258,7 +257,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { Objects.requireNonNull(suggestion); - final TimestampedValue newUnixEpochTime = suggestion.getUnixEpochTime(); + final UnixEpochTime newUnixEpochTime = suggestion.getUnixEpochTime(); if (!validateManualSuggestionTime(newUnixEpochTime, suggestion)) { return false; @@ -321,7 +320,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { @Origin int origin = ORIGIN_MANUAL; UnixEpochTime unixEpochTime = timeState.getUnixEpochTime(); setSystemClockAndConfidenceUnderWakeLock( - origin, unixEpochTime.toTimestampedValue(), confidence, "setTimeZoneState()"); + origin, unixEpochTime, confidence, "setTimeZoneState()"); } finally { mEnvironment.releaseWakeLock(); } @@ -473,14 +472,13 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { @GuardedBy("this") private boolean storeTelephonySuggestion( @NonNull TelephonyTimeSuggestion suggestion) { - TimestampedValue newUnixEpochTime = suggestion.getUnixEpochTime(); + UnixEpochTime newUnixEpochTime = suggestion.getUnixEpochTime(); int slotIndex = suggestion.getSlotIndex(); TelephonyTimeSuggestion previousSuggestion = mSuggestionBySlotIndex.get(slotIndex); if (previousSuggestion != null) { - // We can log / discard suggestions with obvious issues with the reference time clock. - if (previousSuggestion.getUnixEpochTime() == null - || previousSuggestion.getUnixEpochTime().getValue() == null) { + // We can log / discard suggestions with obvious issues with the elapsed realtime clock. + if (previousSuggestion.getUnixEpochTime() == null) { // This should be impossible given we only store validated suggestions. Slog.w(LOG_TAG, "Previous suggestion is null or has a null time." + " previousSuggestion=" + previousSuggestion @@ -488,10 +486,10 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { return false; } - long referenceTimeDifference = TimestampedValue.referenceTimeDifference( + long referenceTimeDifference = UnixEpochTime.elapsedRealtimeDifference( newUnixEpochTime, previousSuggestion.getUnixEpochTime()); if (referenceTimeDifference < 0) { - // The reference time is before the previously received suggestion. Ignore it. + // The elapsed realtime is before the previously received suggestion. Ignore it. Slog.w(LOG_TAG, "Out of order telephony suggestion received." + " referenceTimeDifference=" + referenceTimeDifference + " previousSuggestion=" + previousSuggestion @@ -507,23 +505,18 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { @GuardedBy("this") private boolean validateSuggestionCommon( - @NonNull TimestampedValue newUnixEpochTime, @NonNull Object suggestion) { - if (newUnixEpochTime.getValue() == null) { - Slog.w(LOG_TAG, "Suggested time value is null. suggestion=" + suggestion); - return false; - } - - // We can validate the suggestion against the reference time clock. + @NonNull UnixEpochTime newUnixEpochTime, @NonNull Object suggestion) { + // We can validate the suggestion against the elapsed realtime clock. long elapsedRealtimeMillis = mEnvironment.elapsedRealtimeMillis(); - if (elapsedRealtimeMillis < newUnixEpochTime.getReferenceTimeMillis()) { + if (elapsedRealtimeMillis < newUnixEpochTime.getElapsedRealtimeMillis()) { // elapsedRealtime clock went backwards? - Slog.w(LOG_TAG, "New reference time is in the future? Ignoring." + Slog.w(LOG_TAG, "New elapsed realtime is in the future? Ignoring." + " elapsedRealtimeMillis=" + elapsedRealtimeMillis + ", suggestion=" + suggestion); return false; } - if (newUnixEpochTime.getValue() + if (newUnixEpochTime.getUnixEpochTimeMillis() > mCurrentConfigurationInternal.getSuggestionUpperBound().toEpochMilli()) { // 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 @@ -537,11 +530,11 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { /** * Returns {@code true} if an automatic time suggestion time is valid. - * See also {@link #validateManualSuggestionTime(TimestampedValue, Object)}. + * See also {@link #validateManualSuggestionTime(UnixEpochTime, Object)}. */ @GuardedBy("this") private boolean validateAutoSuggestionTime( - @NonNull TimestampedValue newUnixEpochTime, @NonNull Object suggestion) { + @NonNull UnixEpochTime newUnixEpochTime, @NonNull Object suggestion) { Instant lowerBound = mCurrentConfigurationInternal.getAutoSuggestionLowerBound(); return validateSuggestionCommon(newUnixEpochTime, suggestion) && validateSuggestionAgainstLowerBound(newUnixEpochTime, suggestion, @@ -550,11 +543,11 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { /** * Returns {@code true} if a manual time suggestion time is valid. - * See also {@link #validateAutoSuggestionTime(TimestampedValue, Object)}. + * See also {@link #validateAutoSuggestionTime(UnixEpochTime, Object)}. */ @GuardedBy("this") private boolean validateManualSuggestionTime( - @NonNull TimestampedValue newUnixEpochTime, @NonNull Object suggestion) { + @NonNull UnixEpochTime newUnixEpochTime, @NonNull Object suggestion) { Instant lowerBound = mCurrentConfigurationInternal.getManualSuggestionLowerBound(); // Suggestion is definitely wrong if it comes before lower time bound. @@ -564,11 +557,11 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { @GuardedBy("this") private boolean validateSuggestionAgainstLowerBound( - @NonNull TimestampedValue newUnixEpochTime, @NonNull Object suggestion, + @NonNull UnixEpochTime newUnixEpochTime, @NonNull Object suggestion, @NonNull Instant lowerBound) { // Suggestion is definitely wrong if it comes before lower time bound. - if (lowerBound.toEpochMilli() > newUnixEpochTime.getValue()) { + if (lowerBound.toEpochMilli() > newUnixEpochTime.getUnixEpochTimeMillis()) { Slog.w(LOG_TAG, "Suggestion points to time before lower bound, skipping it. " + "suggestion=" + suggestion + ", lower bound=" + lowerBound); return false; @@ -582,7 +575,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { // Try the different origins one at a time. int[] originPriorities = mCurrentConfigurationInternal.getAutoOriginPriorities(); for (int origin : originPriorities) { - TimestampedValue newUnixEpochTime = null; + UnixEpochTime newUnixEpochTime = null; String cause = null; if (origin == ORIGIN_TELEPHONY) { TelephonyTimeSuggestion bestTelephonySuggestion = findBestTelephonySuggestion(); @@ -670,7 +663,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { // The heuristic works as follows: // Recency: The most recent suggestion from each slotIndex is scored. The score is based on // a discrete age bucket, i.e. so signals received around the same time will be in the same - // bucket, thus applying a loose reference time ordering. The suggestion with the highest + // bucket, thus applying a loose elapsed realtime ordering. The suggestion with the highest // score is used. // Consistency: If there a multiple suggestions with the same score, the suggestion with the // lowest slotIndex is always taken. @@ -722,7 +715,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { long elapsedRealtimeMillis, @NonNull TelephonyTimeSuggestion timeSuggestion) { // Validate first. - TimestampedValue unixEpochTime = timeSuggestion.getUnixEpochTime(); + UnixEpochTime unixEpochTime = timeSuggestion.getUnixEpochTime(); if (!validateSuggestionUnixEpochTime(elapsedRealtimeMillis, unixEpochTime)) { Slog.w(LOG_TAG, "Existing suggestion found to be invalid" + " elapsedRealtimeMillis=" + elapsedRealtimeMillis @@ -732,7 +725,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { // The score is based on the age since receipt. Suggestions are bucketed so two // suggestions in the same bucket from different slotIndexs are scored the same. - long ageMillis = elapsedRealtimeMillis - unixEpochTime.getReferenceTimeMillis(); + long ageMillis = elapsedRealtimeMillis - unixEpochTime.getElapsedRealtimeMillis(); // Turn the age into a discrete value: 0 <= bucketIndex < TELEPHONY_BUCKET_COUNT. int bucketIndex = (int) (ageMillis / TELEPHONY_BUCKET_SIZE_MILLIS); @@ -754,7 +747,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { return null; } - TimestampedValue unixEpochTime = networkSuggestion.getUnixEpochTime(); + UnixEpochTime unixEpochTime = networkSuggestion.getUnixEpochTime(); long elapsedRealTimeMillis = mEnvironment.elapsedRealtimeMillis(); if (!validateSuggestionUnixEpochTime(elapsedRealTimeMillis, unixEpochTime)) { // The latest suggestion is not valid, usually due to its age. @@ -774,7 +767,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { return null; } - TimestampedValue unixEpochTime = gnssTimeSuggestion.getUnixEpochTime(); + UnixEpochTime unixEpochTime = gnssTimeSuggestion.getUnixEpochTime(); long elapsedRealTimeMillis = mEnvironment.elapsedRealtimeMillis(); if (!validateSuggestionUnixEpochTime(elapsedRealTimeMillis, unixEpochTime)) { // The latest suggestion is not valid, usually due to its age. @@ -794,7 +787,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { return null; } - TimestampedValue unixEpochTime = externalTimeSuggestion.getUnixEpochTime(); + UnixEpochTime unixEpochTime = externalTimeSuggestion.getUnixEpochTime(); long elapsedRealTimeMillis = mEnvironment.elapsedRealtimeMillis(); if (!validateSuggestionUnixEpochTime(elapsedRealTimeMillis, unixEpochTime)) { // The latest suggestion is not valid, usually due to its age. @@ -806,7 +799,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { @GuardedBy("this") private boolean setSystemClockAndConfidenceIfRequired( - @Origin int origin, @NonNull TimestampedValue time, @NonNull String cause) { + @Origin int origin, @NonNull UnixEpochTime time, @NonNull String cause) { // Any time set through this class is inherently high confidence. Either it came directly // from a user, or it was detected automatically. @@ -851,7 +844,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { */ @GuardedBy("this") private void upgradeSystemClockConfidenceIfRequired( - @NonNull TimestampedValue autoDetectedUnixEpochTime, @NonNull String cause) { + @NonNull UnixEpochTime autoDetectedUnixEpochTime, @NonNull String cause) { @TimeConfidence int newTimeConfidence = TIME_CONFIDENCE_HIGH; @TimeConfidence int currentTimeConfidence = mEnvironment.systemClockConfidence(); boolean timeNeedsConfirmation = currentTimeConfidence < newTimeConfidence; @@ -866,8 +859,8 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { // enough) to raise the confidence. long elapsedRealtimeMillis = mEnvironment.elapsedRealtimeMillis(); long actualSystemClockMillis = mEnvironment.systemClockMillis(); - long adjustedAutoDetectedUnixEpochMillis = TimeDetectorStrategy.getTimeAt( - autoDetectedUnixEpochTime, elapsedRealtimeMillis); + long adjustedAutoDetectedUnixEpochMillis = + autoDetectedUnixEpochTime.at(elapsedRealtimeMillis).getUnixEpochTimeMillis(); long absTimeDifferenceMillis = Math.abs(adjustedAutoDetectedUnixEpochMillis - actualSystemClockMillis); int confidenceUpgradeThresholdMillis = @@ -899,7 +892,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { @GuardedBy("this") private boolean setSystemClockAndConfidenceUnderWakeLock( - @Origin int origin, @NonNull TimestampedValue newTime, + @Origin int origin, @NonNull UnixEpochTime newTime, @TimeConfidence int newTimeConfidence, @NonNull String cause) { long elapsedRealtimeMillis = mEnvironment.elapsedRealtimeMillis(); @@ -909,8 +902,8 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { // CLOCK_PARANOIA : Check to see if this class owns the clock or if something else // may be setting the clock. if (mLastAutoSystemClockTimeSet != null) { - long expectedTimeMillis = TimeDetectorStrategy.getTimeAt( - mLastAutoSystemClockTimeSet, elapsedRealtimeMillis); + long expectedTimeMillis = mLastAutoSystemClockTimeSet.at(elapsedRealtimeMillis) + .getUnixEpochTimeMillis(); long absSystemClockDifference = Math.abs(expectedTimeMillis - actualSystemClockMillis); if (absSystemClockDifference > SYSTEM_CLOCK_PARANOIA_THRESHOLD_MILLIS) { @@ -931,7 +924,7 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { // in confidence then system state must be updated. // Adjust for the time that has elapsed since the signal was received. - long newSystemClockMillis = TimeDetectorStrategy.getTimeAt(newTime, elapsedRealtimeMillis); + long newSystemClockMillis = newTime.at(elapsedRealtimeMillis).getUnixEpochTimeMillis(); long absTimeDifference = Math.abs(newSystemClockMillis - actualSystemClockMillis); long systemClockUpdateThreshold = mCurrentConfigurationInternal.getSystemClockUpdateThresholdMillis(); @@ -1073,21 +1066,21 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } private static boolean validateSuggestionUnixEpochTime( - long elapsedRealtimeMillis, TimestampedValue unixEpochTime) { - long referenceTimeMillis = unixEpochTime.getReferenceTimeMillis(); - if (referenceTimeMillis > elapsedRealtimeMillis) { - // Future reference times are ignored. They imply the reference time was wrong, or the - // elapsed realtime clock used to derive it has gone backwards, neither of which are + long currentElapsedRealtimeMillis, UnixEpochTime unixEpochTime) { + long suggestionElapsedRealtimeMillis = unixEpochTime.getElapsedRealtimeMillis(); + if (suggestionElapsedRealtimeMillis > currentElapsedRealtimeMillis) { + // Future elapsed realtimes are ignored. They imply the elapsed realtime was wrong, or + // the elapsed realtime clock used to derive it has gone backwards, neither of which are // supportable situations. return false; } // Any suggestion > MAX_AGE_MILLIS is treated as too old. Although time is relentless and - // predictable, the accuracy of the reference time clock may be poor over long periods which - // would lead to errors creeping in. Also, in edge cases where a bad suggestion has been - // made and never replaced, it could also mean that the time detection code remains + // predictable, the accuracy of the elapsed realtime clock may be poor over long periods + // which would lead to errors creeping in. Also, in edge cases where a bad suggestion has + // been made and never replaced, it could also mean that the time detection code remains // opinionated using a bad invalid suggestion. This caps that edge case at MAX_AGE_MILLIS. - long ageMillis = elapsedRealtimeMillis - referenceTimeMillis; + long ageMillis = currentElapsedRealtimeMillis - suggestionElapsedRealtimeMillis; return ageMillis <= MAX_SUGGESTION_TIME_AGE_MILLIS; } } diff --git a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategyImpl.java b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategyImpl.java index 85986ddaa5642..1e88c47e7b306 100644 --- a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategyImpl.java +++ b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorStrategyImpl.java @@ -216,8 +216,8 @@ public final class TimeZoneDetectorStrategyImpl implements TimeZoneDetectorStrat * *

This field is only actually used when telephony time zone fallback is supported, but the * value is maintained even when it isn't supported as it can be turned on at any time via - * server flags. The reference time is the elapsed realtime when the mode last changed to help - * ordering between fallback mode switches and suggestions. + * server flags. The elapsed realtime when the mode last changed is used to help ordering + * between fallback mode switches and suggestions. * *

See {@link TimeZoneDetectorStrategy} for more information. */ diff --git a/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeSuggestionTest.java b/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeSuggestionTest.java index f25d94e0d7eff..57d546929d8b6 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeSuggestionTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeSuggestionTest.java @@ -21,15 +21,15 @@ import static com.android.server.timezonedetector.ShellCommandTestSupport.create import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import android.app.time.UnixEpochTime; import android.os.ShellCommand; -import android.os.TimestampedValue; import org.junit.Test; public class GnssTimeSuggestionTest { - private static final TimestampedValue ARBITRARY_TIME = - new TimestampedValue<>(1111L, 2222L); + private static final UnixEpochTime ARBITRARY_TIME = + new UnixEpochTime(1111L, 2222L); @Test public void testEquals() { @@ -40,9 +40,9 @@ public class GnssTimeSuggestionTest { assertEquals(one, two); assertEquals(two, one); - TimestampedValue differentTime = new TimestampedValue<>( - ARBITRARY_TIME.getReferenceTimeMillis() + 1, - ARBITRARY_TIME.getValue()); + UnixEpochTime differentTime = new UnixEpochTime( + ARBITRARY_TIME.getElapsedRealtimeMillis() + 1, + ARBITRARY_TIME.getUnixEpochTimeMillis()); GnssTimeSuggestion three = new GnssTimeSuggestion(differentTime); assertNotEquals(one, three); assertNotEquals(three, one); @@ -63,15 +63,15 @@ public class GnssTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_noUnixEpochTime() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321"); + "--elapsed_realtime 54321"); GnssTimeSuggestion.parseCommandLineArg(testShellCommand); } @Test public void testParseCommandLineArg_validSuggestion() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345"); - TimestampedValue timeSignal = new TimestampedValue<>(54321L, 12345L); + "--elapsed_realtime 54321 --unix_epoch_time 12345"); + UnixEpochTime timeSignal = new UnixEpochTime(54321L, 12345L); GnssTimeSuggestion expectedSuggestion = new GnssTimeSuggestion(timeSignal); GnssTimeSuggestion actualSuggestion = GnssTimeSuggestion.parseCommandLineArg(testShellCommand); @@ -81,7 +81,7 @@ public class GnssTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_unknownArgument() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + "--elapsed_realtime 54321 --unix_epoch_time 12345 --bad_arg 0"); GnssTimeSuggestion.parseCommandLineArg(testShellCommand); } } diff --git a/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeUpdateServiceTest.java b/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeUpdateServiceTest.java index 3f550d0aa0c10..c8afb78bc12f0 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeUpdateServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeUpdateServiceTest.java @@ -28,6 +28,7 @@ import static org.mockito.Mockito.when; import android.app.AlarmManager; import android.app.AlarmManager.OnAlarmListener; +import android.app.time.UnixEpochTime; import android.content.Context; import android.location.Location; import android.location.LocationListener; @@ -35,7 +36,6 @@ import android.location.LocationManager; import android.location.LocationManagerInternal; import android.location.LocationRequest; import android.location.LocationTime; -import android.os.TimestampedValue; import androidx.test.runner.AndroidJUnit4; @@ -73,7 +73,7 @@ public final class GnssTimeUpdateServiceTest { @Test public void testLocationListenerOnLocationChanged_validLocationTime_suggestsGnssTime() { - TimestampedValue timeSignal = new TimestampedValue<>( + UnixEpochTime timeSignal = new UnixEpochTime( ELAPSED_REALTIME_MS, GNSS_TIME); GnssTimeSuggestion timeSuggestion = new GnssTimeSuggestion(timeSignal); LocationTime locationTime = new LocationTime(GNSS_TIME, ELAPSED_REALTIME_NS); @@ -178,7 +178,7 @@ public final class GnssTimeUpdateServiceTest { private void advanceServiceToSleepingState( ArgumentCaptor locationListenerCaptor, ArgumentCaptor alarmListenerCaptor) { - TimestampedValue timeSignal = new TimestampedValue<>( + UnixEpochTime timeSignal = new UnixEpochTime( ELAPSED_REALTIME_MS, GNSS_TIME); GnssTimeSuggestion timeSuggestion = new GnssTimeSuggestion(timeSignal); LocationTime locationTime = new LocationTime(GNSS_TIME, ELAPSED_REALTIME_NS); diff --git a/services/tests/servicestests/src/com/android/server/timedetector/NetworkTimeSuggestionTest.java b/services/tests/servicestests/src/com/android/server/timedetector/NetworkTimeSuggestionTest.java index f5a37b964508b..fcc76d37de2b1 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/NetworkTimeSuggestionTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/NetworkTimeSuggestionTest.java @@ -21,15 +21,15 @@ import static com.android.server.timezonedetector.ShellCommandTestSupport.create import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import android.app.time.UnixEpochTime; import android.os.ShellCommand; -import android.os.TimestampedValue; import org.junit.Test; public class NetworkTimeSuggestionTest { - private static final TimestampedValue ARBITRARY_TIME = - new TimestampedValue<>(1111L, 2222L); + private static final UnixEpochTime ARBITRARY_TIME = + new UnixEpochTime(1111L, 2222L); private static final int ARBITRARY_UNCERTAINTY_MILLIS = 3333; @Test @@ -43,9 +43,9 @@ public class NetworkTimeSuggestionTest { assertEquals(one, two); assertEquals(two, one); - TimestampedValue differentTime = new TimestampedValue<>( - ARBITRARY_TIME.getReferenceTimeMillis() + 1, - ARBITRARY_TIME.getValue()); + UnixEpochTime differentTime = new UnixEpochTime( + ARBITRARY_TIME.getElapsedRealtimeMillis() + 1, + ARBITRARY_TIME.getUnixEpochTimeMillis()); NetworkTimeSuggestion three = new NetworkTimeSuggestion( differentTime, ARBITRARY_UNCERTAINTY_MILLIS); assertNotEquals(one, three); @@ -73,22 +73,22 @@ public class NetworkTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_noUnixEpochTime() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --uncertainty_millis 111"); + "--elapsed_realtime 54321 --uncertainty_millis 111"); NetworkTimeSuggestion.parseCommandLineArg(testShellCommand); } @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_noUncertaintyMillis() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345"); + "--elapsed_realtime 54321 --unix_epoch_time 12345"); NetworkTimeSuggestion.parseCommandLineArg(testShellCommand); } @Test public void testParseCommandLineArg_validSuggestion() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345 --uncertainty_millis 111"); - TimestampedValue timeSignal = new TimestampedValue<>(54321L, 12345L); + "--elapsed_realtime 54321 --unix_epoch_time 12345 --uncertainty_millis 111"); + UnixEpochTime timeSignal = new UnixEpochTime(54321L, 12345L); NetworkTimeSuggestion expectedSuggestion = new NetworkTimeSuggestion(timeSignal, 111); NetworkTimeSuggestion actualSuggestion = NetworkTimeSuggestion.parseCommandLineArg(testShellCommand); @@ -98,7 +98,7 @@ public class NetworkTimeSuggestionTest { @Test(expected = IllegalArgumentException.class) public void testParseCommandLineArg_unknownArgument() { ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( - "--reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + "--elapsed_realtime 54321 --unix_epoch_time 12345 --bad_arg 0"); NetworkTimeSuggestion.parseCommandLineArg(testShellCommand); } } diff --git a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorInternalImplTest.java b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorInternalImplTest.java index f8092a67e8c75..5b61752175681 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorInternalImplTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorInternalImplTest.java @@ -18,9 +18,9 @@ package com.android.server.timedetector; import static org.mockito.Mockito.mock; +import android.app.time.UnixEpochTime; import android.content.Context; import android.os.HandlerThread; -import android.os.TimestampedValue; import androidx.test.runner.AndroidJUnit4; @@ -74,7 +74,7 @@ public class TimeDetectorInternalImplTest { } private static NetworkTimeSuggestion createNetworkTimeSuggestion() { - TimestampedValue timeValue = new TimestampedValue<>(100L, 1_000_000L); + UnixEpochTime timeValue = new UnixEpochTime(100L, 1_000_000L); return new NetworkTimeSuggestion(timeValue, 123); } @@ -90,7 +90,7 @@ public class TimeDetectorInternalImplTest { } private static GnssTimeSuggestion createGnssTimeSuggestion() { - TimestampedValue timeValue = new TimestampedValue<>(100L, 1_000_000L); + UnixEpochTime timeValue = new UnixEpochTime(100L, 1_000_000L); return new GnssTimeSuggestion(timeValue); } } 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 12184894a9437..ba5f66cdbcccb 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java @@ -49,7 +49,6 @@ import android.content.pm.PackageManager; import android.os.HandlerThread; import android.os.IBinder; import android.os.ParcelableException; -import android.os.TimestampedValue; import android.util.NtpTrustedTime; import androidx.test.runner.AndroidJUnit4; @@ -578,24 +577,24 @@ public class TimeDetectorServiceTest { private static TelephonyTimeSuggestion createTelephonyTimeSuggestion() { int slotIndex = 1234; - TimestampedValue timeValue = new TimestampedValue<>(100L, 1_000_000L); + UnixEpochTime timeValue = new UnixEpochTime(100L, 1_000_000L); return new TelephonyTimeSuggestion.Builder(slotIndex) .setUnixEpochTime(timeValue) .build(); } private static ManualTimeSuggestion createManualTimeSuggestion() { - TimestampedValue timeValue = new TimestampedValue<>(100L, 1_000_000L); + UnixEpochTime timeValue = new UnixEpochTime(100L, 1_000_000L); return new ManualTimeSuggestion(timeValue); } private static NetworkTimeSuggestion createNetworkTimeSuggestion() { - TimestampedValue timeValue = new TimestampedValue<>(100L, 1_000_000L); + UnixEpochTime timeValue = new UnixEpochTime(100L, 1_000_000L); return new NetworkTimeSuggestion(timeValue, 123); } private static GnssTimeSuggestion createGnssTimeSuggestion() { - TimestampedValue timeValue = new TimestampedValue<>(100L, 1_000_000L); + UnixEpochTime timeValue = new UnixEpochTime(100L, 1_000_000L); return new GnssTimeSuggestion(timeValue); } 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 9ec70dc5ec3be..08632284cfc89 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyImplTest.java @@ -320,7 +320,7 @@ public class TimeDetectorStrategyImplTest { Instant suggestionInstant = initialClockTime.getValue() .plusMillis(timeElapsedMillis) .plusMillis(confidenceUpgradeThresholdMillis); - TimestampedValue matchingClockTime = new TimestampedValue<>( + UnixEpochTime matchingClockTime = new UnixEpochTime( script.peekElapsedRealtimeMillis(), suggestionInstant.toEpochMilli()); TelephonyTimeSuggestion timeSuggestion = new TelephonyTimeSuggestion.Builder(slotIndex) @@ -357,7 +357,7 @@ public class TimeDetectorStrategyImplTest { Instant suggestionInstant = initialClockTime.getValue() .plusMillis(timeElapsedMillis) .plusMillis(confidenceUpgradeThresholdMillis + 1); - TimestampedValue mismatchingClockTime = new TimestampedValue<>( + UnixEpochTime mismatchingClockTime = new UnixEpochTime( script.peekElapsedRealtimeMillis(), suggestionInstant.toEpochMilli()); TelephonyTimeSuggestion timeSuggestion = new TelephonyTimeSuggestion.Builder(slotIndex) @@ -392,7 +392,7 @@ public class TimeDetectorStrategyImplTest { // Create a suggestion time that doesn't closely match the current system clock. Instant initialClockInstant = initialClockTime.getValue(); - TimestampedValue mismatchingClockTime = new TimestampedValue<>( + UnixEpochTime mismatchingClockTime = new UnixEpochTime( script.peekElapsedRealtimeMillis(), initialClockInstant.plusMillis(timeElapsedMillis + 1_000_000).toEpochMilli()); TelephonyTimeSuggestion timeSuggestion = new TelephonyTimeSuggestion.Builder(slotIndex) @@ -418,7 +418,7 @@ public class TimeDetectorStrategyImplTest { TelephonyTimeSuggestion timeSuggestion1 = script.generateTelephonyTimeSuggestion(slotIndex, testTime); - TimestampedValue unixEpochTime1 = timeSuggestion1.getUnixEpochTime(); + UnixEpochTime unixEpochTime1 = timeSuggestion1.getUnixEpochTime(); // Initialize the strategy / device with a time set from a telephony suggestion. script.simulateTimePassing(); @@ -430,13 +430,13 @@ 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() + long validUnixEpochTimeMillis = unixEpochTime1.getUnixEpochTimeMillis() + (2 * systemClockUpdateThresholdMillis); - // Now supply a new signal that has an obviously bogus reference time : older than the last - // one. - long referenceTimeBeforeLastSignalMillis = unixEpochTime1.getReferenceTimeMillis() - 1; - TimestampedValue unixEpochTime2 = new TimestampedValue<>( + // Now supply a new signal that has an obviously bogus elapsed realtime : older than the + // last one. + long referenceTimeBeforeLastSignalMillis = unixEpochTime1.getElapsedRealtimeMillis() - 1; + UnixEpochTime unixEpochTime2 = new UnixEpochTime( referenceTimeBeforeLastSignalMillis, validUnixEpochTimeMillis); TelephonyTimeSuggestion timeSuggestion2 = createTelephonyTimeSuggestion(slotIndex, unixEpochTime2); @@ -445,11 +445,11 @@ public class TimeDetectorStrategyImplTest { .verifySystemClockWasNotSetAndResetCallTracking() .assertLatestTelephonySuggestion(slotIndex, timeSuggestion1); - // Now supply a new signal that has an obviously bogus reference time : substantially in the - // future. + // Now supply a new signal that has an obviously bogus elapsed realtime; one substantially + // in the future. long referenceTimeInFutureMillis = - unixEpochTime1.getReferenceTimeMillis() + Integer.MAX_VALUE + 1; - TimestampedValue unixEpochTime3 = new TimestampedValue<>( + unixEpochTime1.getElapsedRealtimeMillis() + Integer.MAX_VALUE + 1; + UnixEpochTime unixEpochTime3 = new UnixEpochTime( referenceTimeInFutureMillis, validUnixEpochTimeMillis); TelephonyTimeSuggestion timeSuggestion3 = createTelephonyTimeSuggestion(slotIndex, unixEpochTime3); @@ -459,8 +459,8 @@ public class TimeDetectorStrategyImplTest { .assertLatestTelephonySuggestion(slotIndex, timeSuggestion1); // Just to prove validUnixEpochTimeMillis is valid. - long validReferenceTimeMillis = unixEpochTime1.getReferenceTimeMillis() + 100; - TimestampedValue unixEpochTime4 = new TimestampedValue<>( + long validReferenceTimeMillis = unixEpochTime1.getElapsedRealtimeMillis() + 100; + UnixEpochTime unixEpochTime4 = new UnixEpochTime( validReferenceTimeMillis, validUnixEpochTimeMillis); long expectedSystemClockMillis4 = script.calculateTimeInMillisForNow(unixEpochTime4); TelephonyTimeSuggestion timeSuggestion4 = @@ -486,7 +486,7 @@ public class TimeDetectorStrategyImplTest { Instant testTime = ARBITRARY_TEST_TIME; TelephonyTimeSuggestion timeSuggestion1 = script.generateTelephonyTimeSuggestion(slotIndex, testTime); - TimestampedValue unixEpochTime1 = timeSuggestion1.getUnixEpochTime(); + UnixEpochTime unixEpochTime1 = timeSuggestion1.getUnixEpochTime(); // Simulate time passing. script.simulateTimePassing(clockIncrementMillis); @@ -2099,11 +2099,11 @@ public class TimeDetectorStrategyImplTest { /** * Generates a ManualTimeSuggestion using the current elapsed realtime clock for the - * reference time. + * elapsed realtime. */ ManualTimeSuggestion generateManualTimeSuggestion(Instant suggestedTime) { - TimestampedValue unixEpochTime = - new TimestampedValue<>( + UnixEpochTime unixEpochTime = + new UnixEpochTime( mFakeEnvironment.peekElapsedRealtimeMillis(), suggestedTime.toEpochMilli()); return new ManualTimeSuggestion(unixEpochTime); @@ -2111,17 +2111,16 @@ public class TimeDetectorStrategyImplTest { /** * Generates a {@link TelephonyTimeSuggestion} using the current elapsed realtime clock for - * the reference time. + * the elapsed realtime. */ TelephonyTimeSuggestion generateTelephonyTimeSuggestion(int slotIndex, long timeMillis) { - TimestampedValue time = - new TimestampedValue<>(peekElapsedRealtimeMillis(), timeMillis); + UnixEpochTime time = new UnixEpochTime(peekElapsedRealtimeMillis(), timeMillis); return createTelephonyTimeSuggestion(slotIndex, time); } /** * Generates a {@link TelephonyTimeSuggestion} using the current elapsed realtime clock for - * the reference time. + * the elapsed realtime. */ TelephonyTimeSuggestion generateTelephonyTimeSuggestion( int slotIndex, Instant suggestedTime) { @@ -2133,11 +2132,11 @@ public class TimeDetectorStrategyImplTest { /** * Generates a NetworkTimeSuggestion using the current elapsed realtime clock for the - * reference time. + * elapsed realtime. */ NetworkTimeSuggestion generateNetworkTimeSuggestion(Instant suggestedTime) { - TimestampedValue unixEpochTime = - new TimestampedValue<>( + UnixEpochTime unixEpochTime = + new UnixEpochTime( mFakeEnvironment.peekElapsedRealtimeMillis(), suggestedTime.toEpochMilli()); return new NetworkTimeSuggestion(unixEpochTime, 123); @@ -2145,11 +2144,11 @@ public class TimeDetectorStrategyImplTest { /** * Generates a GnssTimeSuggestion using the current elapsed realtime clock for the - * reference time. + * elapsed realtime. */ GnssTimeSuggestion generateGnssTimeSuggestion(Instant suggestedTime) { - TimestampedValue unixEpochTime = - new TimestampedValue<>( + UnixEpochTime unixEpochTime = + new UnixEpochTime( mFakeEnvironment.peekElapsedRealtimeMillis(), suggestedTime.toEpochMilli()); return new GnssTimeSuggestion(unixEpochTime); @@ -2157,7 +2156,7 @@ public class TimeDetectorStrategyImplTest { /** * Generates a ExternalTimeSuggestion using the current elapsed realtime clock for the - * reference time. + * elapsed realtime. */ ExternalTimeSuggestion generateExternalTimeSuggestion(Instant suggestedTime) { return new ExternalTimeSuggestion(mFakeEnvironment.peekElapsedRealtimeMillis(), @@ -2168,8 +2167,8 @@ public class TimeDetectorStrategyImplTest { * Calculates what the supplied time would be when adjusted for the movement of the fake * elapsed realtime clock. */ - long calculateTimeInMillisForNow(TimestampedValue unixEpochTime) { - return TimeDetectorStrategy.getTimeAt(unixEpochTime, peekElapsedRealtimeMillis()); + long calculateTimeInMillisForNow(UnixEpochTime unixEpochTime) { + return unixEpochTime.at(peekElapsedRealtimeMillis()).getUnixEpochTimeMillis(); } Script simulateConfirmTime(UnixEpochTime confirmationTime, boolean expectedReturnValue) { @@ -2179,7 +2178,7 @@ public class TimeDetectorStrategyImplTest { } private static TelephonyTimeSuggestion createTelephonyTimeSuggestion(int slotIndex, - TimestampedValue unixEpochTime) { + UnixEpochTime unixEpochTime) { return new TelephonyTimeSuggestion.Builder(slotIndex) .setUnixEpochTime(unixEpochTime) .build(); diff --git a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyTest.java b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyTest.java deleted file mode 100644 index f1e9191ddb4fd..0000000000000 --- a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorStrategyTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.server.timedetector; - -import static org.junit.Assert.assertEquals; - -import android.os.TimestampedValue; - -import androidx.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -public class TimeDetectorStrategyTest { - - @Test - public void testGetTimeAt() { - long timeMillis = 1000L; - int referenceTimeMillis = 100; - TimestampedValue timestampedValue = - new TimestampedValue<>(referenceTimeMillis, timeMillis); - // Reference time is after the timestamp. - assertEquals( - timeMillis + (125 - referenceTimeMillis), - TimeDetectorStrategy.getTimeAt(timestampedValue, 125)); - - // Reference time is before the timestamp. - assertEquals( - timeMillis + (75 - referenceTimeMillis), - TimeDetectorStrategy.getTimeAt(timestampedValue, 75)); - } -}