Loading core/java/android/app/timedetector/PhoneTimeSuggestion.java +13 −10 Original line number Diff line number Diff line Loading @@ -29,7 +29,9 @@ import java.util.List; import java.util.Objects; /** * A time signal from a telephony source. The value consists of the number of milliseconds elapsed * A time signal from a telephony source. The value can be {@code null} to indicate that the * telephony source has entered an "un-opinionated" state and any previously sent suggestions are * being withdrawn. When not {@code null}, the value consists of the number of milliseconds elapsed * since 1/1/1970 00:00:00 UTC and the time according to the elapsed realtime clock when that number * was established. The elapsed realtime clock is considered accurate but volatile, so time signals * must not be persisted across device resets. Loading @@ -50,20 +52,17 @@ public final class PhoneTimeSuggestion implements Parcelable { }; private final int mPhoneId; @NonNull private final TimestampedValue<Long> mUtcTime; @Nullable private ArrayList<String> mDebugInfo; @Nullable private TimestampedValue<Long> mUtcTime; @Nullable private ArrayList<String> mDebugInfo; public PhoneTimeSuggestion(int phoneId, @NonNull TimestampedValue<Long> utcTime) { public PhoneTimeSuggestion(int phoneId) { mPhoneId = phoneId; mUtcTime = Objects.requireNonNull(utcTime); } private static PhoneTimeSuggestion createFromParcel(Parcel in) { int phoneId = in.readInt(); TimestampedValue<Long> utcTime = in.readParcelable(null /* classLoader */); PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId, utcTime); PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId); suggestion.setUtcTime(in.readParcelable(null /* classLoader */)); @SuppressWarnings("unchecked") ArrayList<String> debugInfo = (ArrayList<String>) in.readArrayList(null /* classLoader */); suggestion.mDebugInfo = debugInfo; Loading @@ -86,7 +85,11 @@ public final class PhoneTimeSuggestion implements Parcelable { return mPhoneId; } @NonNull public void setUtcTime(@Nullable TimestampedValue<Long> utcTime) { mUtcTime = utcTime; } @Nullable public TimestampedValue<Long> getUtcTime() { return mUtcTime; } Loading core/tests/coretests/src/android/app/timedetector/PhoneTimeSuggestionTest.java +15 −8 Original line number Diff line number Diff line Loading @@ -30,17 +30,22 @@ public class PhoneTimeSuggestionTest { @Test public void testEquals() { PhoneTimeSuggestion one = new PhoneTimeSuggestion(PHONE_ID, new TimestampedValue<>(1111L, 2222L)); PhoneTimeSuggestion one = new PhoneTimeSuggestion(PHONE_ID); assertEquals(one, one); PhoneTimeSuggestion two = new PhoneTimeSuggestion(PHONE_ID, new TimestampedValue<>(1111L, 2222L)); PhoneTimeSuggestion two = new PhoneTimeSuggestion(PHONE_ID); assertEquals(one, two); assertEquals(two, one); PhoneTimeSuggestion three = new PhoneTimeSuggestion(PHONE_ID + 1, new TimestampedValue<>(1111L, 2222L)); one.setUtcTime(new TimestampedValue<>(1111L, 2222L)); assertEquals(one, one); two.setUtcTime(new TimestampedValue<>(1111L, 2222L)); assertEquals(one, two); assertEquals(two, one); PhoneTimeSuggestion three = new PhoneTimeSuggestion(PHONE_ID + 1); three.setUtcTime(new TimestampedValue<>(1111L, 2222L)); assertNotEquals(one, three); assertNotEquals(three, one); Loading @@ -52,8 +57,10 @@ public class PhoneTimeSuggestionTest { @Test public void testParcelable() { PhoneTimeSuggestion one = new PhoneTimeSuggestion(PHONE_ID, new TimestampedValue<>(1111L, 2222L)); PhoneTimeSuggestion one = new PhoneTimeSuggestion(PHONE_ID); assertEquals(one, roundTripParcelable(one)); one.setUtcTime(new TimestampedValue<>(1111L, 2222L)); assertEquals(one, roundTripParcelable(one)); // DebugInfo should also be stored (but is not checked by equals() Loading services/core/java/com/android/server/timedetector/SimpleTimeDetectorStrategy.java +5 −0 Original line number Diff line number Diff line Loading @@ -67,6 +67,11 @@ public final class SimpleTimeDetectorStrategy implements TimeDetectorStrategy { public void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSuggestion) { // NITZ logic // Empty suggestions are just ignored as we don't currently keep track of suggestion origin. if (timeSuggestion.getUtcTime() == null) { return; } boolean timeSuggestionIsValid = validateNewPhoneSuggestion(timeSuggestion, mLastPhoneSuggestion); if (!timeSuggestionIsValid) { Loading services/tests/servicestests/src/com/android/server/timedetector/SimpleTimeZoneDetectorStrategyTest.java +32 −7 Original line number Diff line number Diff line Loading @@ -70,6 +70,18 @@ public class SimpleTimeZoneDetectorStrategyTest { .verifySystemClockWasSetAndResetCallTracking(expectSystemClockMillis); } @Test public void testSuggestPhoneTime_emptySuggestionIgnored() { Scenario scenario = SCENARIO_1; mScript.pokeFakeClocks(scenario) .pokeTimeDetectionEnabled(true); PhoneTimeSuggestion timeSuggestion = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, null); mScript.simulatePhoneTimeSuggestion(timeSuggestion) .verifySystemClockWasNotSetAndResetCallTracking(); } @Test public void testSuggestPhoneTime_systemClockThreshold() { Scenario scenario = SCENARIO_1; Loading Loading @@ -99,7 +111,8 @@ public class SimpleTimeZoneDetectorStrategyTest { TimestampedValue<Long> utcTime2 = new TimestampedValue<>( mScript.peekElapsedRealtimeMillis(), mScript.peekSystemClockMillis() + underThresholdMillis); PhoneTimeSuggestion timeSuggestion2 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); PhoneTimeSuggestion timeSuggestion2 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); mScript.simulateTimePassing(clockIncrement) .simulatePhoneTimeSuggestion(timeSuggestion2) .verifySystemClockWasNotSetAndResetCallTracking(); Loading @@ -109,7 +122,8 @@ public class SimpleTimeZoneDetectorStrategyTest { mScript.peekElapsedRealtimeMillis(), mScript.peekSystemClockMillis() + systemClockUpdateThresholdMillis); PhoneTimeSuggestion timeSuggestion3 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); PhoneTimeSuggestion timeSuggestion3 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); mScript.simulateTimePassing(clockIncrement); long expectSystemClockMillis3 = Loading Loading @@ -158,7 +172,8 @@ public class SimpleTimeZoneDetectorStrategyTest { long referenceTimeBeforeLastSignalMillis = utcTime1.getReferenceTimeMillis() - 1; TimestampedValue<Long> utcTime2 = new TimestampedValue<>( referenceTimeBeforeLastSignalMillis, validUtcTimeMillis); PhoneTimeSuggestion timeSuggestion2 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); PhoneTimeSuggestion timeSuggestion2 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); mScript.simulatePhoneTimeSuggestion(timeSuggestion2) .verifySystemClockWasNotSetAndResetCallTracking(); Loading @@ -168,7 +183,8 @@ public class SimpleTimeZoneDetectorStrategyTest { utcTime1.getReferenceTimeMillis() + Integer.MAX_VALUE + 1; TimestampedValue<Long> utcTime3 = new TimestampedValue<>( referenceTimeInFutureMillis, validUtcTimeMillis); PhoneTimeSuggestion timeSuggestion3 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); PhoneTimeSuggestion timeSuggestion3 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); mScript.simulatePhoneTimeSuggestion(timeSuggestion3) .verifySystemClockWasNotSetAndResetCallTracking(); Loading @@ -178,7 +194,8 @@ public class SimpleTimeZoneDetectorStrategyTest { validReferenceTimeMillis, validUtcTimeMillis); long expectedSystemClockMillis4 = TimeDetectorStrategy.getTimeAt(utcTime4, mScript.peekElapsedRealtimeMillis()); PhoneTimeSuggestion timeSuggestion4 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime4); PhoneTimeSuggestion timeSuggestion4 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime4); mScript.simulatePhoneTimeSuggestion(timeSuggestion4) .verifySystemClockWasSetAndResetCallTracking(expectedSystemClockMillis4); } Loading Loading @@ -223,7 +240,8 @@ public class SimpleTimeZoneDetectorStrategyTest { TimestampedValue<Long> utcTime2 = new TimestampedValue<>( mScript.peekElapsedRealtimeMillis(), mScript.peekSystemClockMillis() + systemClockUpdateThreshold); PhoneTimeSuggestion timeSuggestion2 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); PhoneTimeSuggestion timeSuggestion2 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); // Simulate more time passing. mScript.simulateTimePassing(clockIncrementMillis); Loading Loading @@ -465,7 +483,7 @@ public class SimpleTimeZoneDetectorStrategyTest { PhoneTimeSuggestion createPhoneTimeSuggestionForActual(int phoneId) { TimestampedValue<Long> time = new TimestampedValue<>( mInitialDeviceRealtimeMillis, mActualTimeMillis); return new PhoneTimeSuggestion(phoneId, time); return createPhoneTimeSuggestion(phoneId, time); } static class Builder { Loading Loading @@ -500,6 +518,13 @@ public class SimpleTimeZoneDetectorStrategyTest { } } private static PhoneTimeSuggestion createPhoneTimeSuggestion(int phoneId, TimestampedValue<Long> utcTime) { PhoneTimeSuggestion timeSuggestion = new PhoneTimeSuggestion(phoneId); timeSuggestion.setUtcTime(utcTime); return timeSuggestion; } private static long createUtcTime(int year, int monthInYear, int day, int hourOfDay, int minute, int second) { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Etc/UTC")); Loading services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java +3 −1 Original line number Diff line number Diff line Loading @@ -117,8 +117,10 @@ public class TimeDetectorServiceTest { private static PhoneTimeSuggestion createPhoneTimeSuggestion() { int phoneId = 1234; PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId); TimestampedValue<Long> timeValue = new TimestampedValue<>(100L, 1_000_000L); return new PhoneTimeSuggestion(phoneId, timeValue); suggestion.setUtcTime(timeValue); return suggestion; } private static class StubbedTimeDetectorStrategy implements TimeDetectorStrategy { Loading Loading
core/java/android/app/timedetector/PhoneTimeSuggestion.java +13 −10 Original line number Diff line number Diff line Loading @@ -29,7 +29,9 @@ import java.util.List; import java.util.Objects; /** * A time signal from a telephony source. The value consists of the number of milliseconds elapsed * A time signal from a telephony source. The value can be {@code null} to indicate that the * telephony source has entered an "un-opinionated" state and any previously sent suggestions are * being withdrawn. When not {@code null}, the value consists of the number of milliseconds elapsed * since 1/1/1970 00:00:00 UTC and the time according to the elapsed realtime clock when that number * was established. The elapsed realtime clock is considered accurate but volatile, so time signals * must not be persisted across device resets. Loading @@ -50,20 +52,17 @@ public final class PhoneTimeSuggestion implements Parcelable { }; private final int mPhoneId; @NonNull private final TimestampedValue<Long> mUtcTime; @Nullable private ArrayList<String> mDebugInfo; @Nullable private TimestampedValue<Long> mUtcTime; @Nullable private ArrayList<String> mDebugInfo; public PhoneTimeSuggestion(int phoneId, @NonNull TimestampedValue<Long> utcTime) { public PhoneTimeSuggestion(int phoneId) { mPhoneId = phoneId; mUtcTime = Objects.requireNonNull(utcTime); } private static PhoneTimeSuggestion createFromParcel(Parcel in) { int phoneId = in.readInt(); TimestampedValue<Long> utcTime = in.readParcelable(null /* classLoader */); PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId, utcTime); PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId); suggestion.setUtcTime(in.readParcelable(null /* classLoader */)); @SuppressWarnings("unchecked") ArrayList<String> debugInfo = (ArrayList<String>) in.readArrayList(null /* classLoader */); suggestion.mDebugInfo = debugInfo; Loading @@ -86,7 +85,11 @@ public final class PhoneTimeSuggestion implements Parcelable { return mPhoneId; } @NonNull public void setUtcTime(@Nullable TimestampedValue<Long> utcTime) { mUtcTime = utcTime; } @Nullable public TimestampedValue<Long> getUtcTime() { return mUtcTime; } Loading
core/tests/coretests/src/android/app/timedetector/PhoneTimeSuggestionTest.java +15 −8 Original line number Diff line number Diff line Loading @@ -30,17 +30,22 @@ public class PhoneTimeSuggestionTest { @Test public void testEquals() { PhoneTimeSuggestion one = new PhoneTimeSuggestion(PHONE_ID, new TimestampedValue<>(1111L, 2222L)); PhoneTimeSuggestion one = new PhoneTimeSuggestion(PHONE_ID); assertEquals(one, one); PhoneTimeSuggestion two = new PhoneTimeSuggestion(PHONE_ID, new TimestampedValue<>(1111L, 2222L)); PhoneTimeSuggestion two = new PhoneTimeSuggestion(PHONE_ID); assertEquals(one, two); assertEquals(two, one); PhoneTimeSuggestion three = new PhoneTimeSuggestion(PHONE_ID + 1, new TimestampedValue<>(1111L, 2222L)); one.setUtcTime(new TimestampedValue<>(1111L, 2222L)); assertEquals(one, one); two.setUtcTime(new TimestampedValue<>(1111L, 2222L)); assertEquals(one, two); assertEquals(two, one); PhoneTimeSuggestion three = new PhoneTimeSuggestion(PHONE_ID + 1); three.setUtcTime(new TimestampedValue<>(1111L, 2222L)); assertNotEquals(one, three); assertNotEquals(three, one); Loading @@ -52,8 +57,10 @@ public class PhoneTimeSuggestionTest { @Test public void testParcelable() { PhoneTimeSuggestion one = new PhoneTimeSuggestion(PHONE_ID, new TimestampedValue<>(1111L, 2222L)); PhoneTimeSuggestion one = new PhoneTimeSuggestion(PHONE_ID); assertEquals(one, roundTripParcelable(one)); one.setUtcTime(new TimestampedValue<>(1111L, 2222L)); assertEquals(one, roundTripParcelable(one)); // DebugInfo should also be stored (but is not checked by equals() Loading
services/core/java/com/android/server/timedetector/SimpleTimeDetectorStrategy.java +5 −0 Original line number Diff line number Diff line Loading @@ -67,6 +67,11 @@ public final class SimpleTimeDetectorStrategy implements TimeDetectorStrategy { public void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSuggestion) { // NITZ logic // Empty suggestions are just ignored as we don't currently keep track of suggestion origin. if (timeSuggestion.getUtcTime() == null) { return; } boolean timeSuggestionIsValid = validateNewPhoneSuggestion(timeSuggestion, mLastPhoneSuggestion); if (!timeSuggestionIsValid) { Loading
services/tests/servicestests/src/com/android/server/timedetector/SimpleTimeZoneDetectorStrategyTest.java +32 −7 Original line number Diff line number Diff line Loading @@ -70,6 +70,18 @@ public class SimpleTimeZoneDetectorStrategyTest { .verifySystemClockWasSetAndResetCallTracking(expectSystemClockMillis); } @Test public void testSuggestPhoneTime_emptySuggestionIgnored() { Scenario scenario = SCENARIO_1; mScript.pokeFakeClocks(scenario) .pokeTimeDetectionEnabled(true); PhoneTimeSuggestion timeSuggestion = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, null); mScript.simulatePhoneTimeSuggestion(timeSuggestion) .verifySystemClockWasNotSetAndResetCallTracking(); } @Test public void testSuggestPhoneTime_systemClockThreshold() { Scenario scenario = SCENARIO_1; Loading Loading @@ -99,7 +111,8 @@ public class SimpleTimeZoneDetectorStrategyTest { TimestampedValue<Long> utcTime2 = new TimestampedValue<>( mScript.peekElapsedRealtimeMillis(), mScript.peekSystemClockMillis() + underThresholdMillis); PhoneTimeSuggestion timeSuggestion2 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); PhoneTimeSuggestion timeSuggestion2 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); mScript.simulateTimePassing(clockIncrement) .simulatePhoneTimeSuggestion(timeSuggestion2) .verifySystemClockWasNotSetAndResetCallTracking(); Loading @@ -109,7 +122,8 @@ public class SimpleTimeZoneDetectorStrategyTest { mScript.peekElapsedRealtimeMillis(), mScript.peekSystemClockMillis() + systemClockUpdateThresholdMillis); PhoneTimeSuggestion timeSuggestion3 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); PhoneTimeSuggestion timeSuggestion3 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); mScript.simulateTimePassing(clockIncrement); long expectSystemClockMillis3 = Loading Loading @@ -158,7 +172,8 @@ public class SimpleTimeZoneDetectorStrategyTest { long referenceTimeBeforeLastSignalMillis = utcTime1.getReferenceTimeMillis() - 1; TimestampedValue<Long> utcTime2 = new TimestampedValue<>( referenceTimeBeforeLastSignalMillis, validUtcTimeMillis); PhoneTimeSuggestion timeSuggestion2 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); PhoneTimeSuggestion timeSuggestion2 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); mScript.simulatePhoneTimeSuggestion(timeSuggestion2) .verifySystemClockWasNotSetAndResetCallTracking(); Loading @@ -168,7 +183,8 @@ public class SimpleTimeZoneDetectorStrategyTest { utcTime1.getReferenceTimeMillis() + Integer.MAX_VALUE + 1; TimestampedValue<Long> utcTime3 = new TimestampedValue<>( referenceTimeInFutureMillis, validUtcTimeMillis); PhoneTimeSuggestion timeSuggestion3 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); PhoneTimeSuggestion timeSuggestion3 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); mScript.simulatePhoneTimeSuggestion(timeSuggestion3) .verifySystemClockWasNotSetAndResetCallTracking(); Loading @@ -178,7 +194,8 @@ public class SimpleTimeZoneDetectorStrategyTest { validReferenceTimeMillis, validUtcTimeMillis); long expectedSystemClockMillis4 = TimeDetectorStrategy.getTimeAt(utcTime4, mScript.peekElapsedRealtimeMillis()); PhoneTimeSuggestion timeSuggestion4 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime4); PhoneTimeSuggestion timeSuggestion4 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime4); mScript.simulatePhoneTimeSuggestion(timeSuggestion4) .verifySystemClockWasSetAndResetCallTracking(expectedSystemClockMillis4); } Loading Loading @@ -223,7 +240,8 @@ public class SimpleTimeZoneDetectorStrategyTest { TimestampedValue<Long> utcTime2 = new TimestampedValue<>( mScript.peekElapsedRealtimeMillis(), mScript.peekSystemClockMillis() + systemClockUpdateThreshold); PhoneTimeSuggestion timeSuggestion2 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); PhoneTimeSuggestion timeSuggestion2 = createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); // Simulate more time passing. mScript.simulateTimePassing(clockIncrementMillis); Loading Loading @@ -465,7 +483,7 @@ public class SimpleTimeZoneDetectorStrategyTest { PhoneTimeSuggestion createPhoneTimeSuggestionForActual(int phoneId) { TimestampedValue<Long> time = new TimestampedValue<>( mInitialDeviceRealtimeMillis, mActualTimeMillis); return new PhoneTimeSuggestion(phoneId, time); return createPhoneTimeSuggestion(phoneId, time); } static class Builder { Loading Loading @@ -500,6 +518,13 @@ public class SimpleTimeZoneDetectorStrategyTest { } } private static PhoneTimeSuggestion createPhoneTimeSuggestion(int phoneId, TimestampedValue<Long> utcTime) { PhoneTimeSuggestion timeSuggestion = new PhoneTimeSuggestion(phoneId); timeSuggestion.setUtcTime(utcTime); return timeSuggestion; } private static long createUtcTime(int year, int monthInYear, int day, int hourOfDay, int minute, int second) { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Etc/UTC")); Loading
services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java +3 −1 Original line number Diff line number Diff line Loading @@ -117,8 +117,10 @@ public class TimeDetectorServiceTest { private static PhoneTimeSuggestion createPhoneTimeSuggestion() { int phoneId = 1234; PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId); TimestampedValue<Long> timeValue = new TimestampedValue<>(100L, 1_000_000L); return new PhoneTimeSuggestion(phoneId, timeValue); suggestion.setUtcTime(timeValue); return suggestion; } private static class StubbedTimeDetectorStrategy implements TimeDetectorStrategy { Loading