From 568fd897eb1c5fe9a973346433ca6369a6d6a036 Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Wed, 20 Nov 2019 14:39:06 +0000 Subject: [PATCH] Allow for empty time suggestions Allow for empty time suggestions in the TimeDetectorService. The empty suggestions are currently ignored but are likely to be used in future time detector strategy implementations. Bug: 140712361 Test: atest android.app.timedetector.PhoneTimeSuggestionTest Test: atest com.android.server.timedetector Change-Id: Ifb219702fa8bc53665a0d74752e06d851d86f8bb --- .../app/timedetector/PhoneTimeSuggestion.java | 23 ++++++----- .../timedetector/PhoneTimeSuggestionTest.java | 23 +++++++---- .../SimpleTimeDetectorStrategy.java | 5 +++ .../SimpleTimeZoneDetectorStrategyTest.java | 39 +++++++++++++++---- .../timedetector/TimeDetectorServiceTest.java | 4 +- 5 files changed, 68 insertions(+), 26 deletions(-) diff --git a/core/java/android/app/timedetector/PhoneTimeSuggestion.java b/core/java/android/app/timedetector/PhoneTimeSuggestion.java index 475a4aafd929f..233dbbc42f504 100644 --- a/core/java/android/app/timedetector/PhoneTimeSuggestion.java +++ b/core/java/android/app/timedetector/PhoneTimeSuggestion.java @@ -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. @@ -50,20 +52,17 @@ public final class PhoneTimeSuggestion implements Parcelable { }; private final int mPhoneId; - @NonNull - private final TimestampedValue mUtcTime; - @Nullable - private ArrayList mDebugInfo; + @Nullable private TimestampedValue mUtcTime; + @Nullable private ArrayList mDebugInfo; - public PhoneTimeSuggestion(int phoneId, @NonNull TimestampedValue utcTime) { + public PhoneTimeSuggestion(int phoneId) { mPhoneId = phoneId; - mUtcTime = Objects.requireNonNull(utcTime); } private static PhoneTimeSuggestion createFromParcel(Parcel in) { int phoneId = in.readInt(); - TimestampedValue 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 debugInfo = (ArrayList) in.readArrayList(null /* classLoader */); suggestion.mDebugInfo = debugInfo; @@ -86,7 +85,11 @@ public final class PhoneTimeSuggestion implements Parcelable { return mPhoneId; } - @NonNull + public void setUtcTime(@Nullable TimestampedValue utcTime) { + mUtcTime = utcTime; + } + + @Nullable public TimestampedValue getUtcTime() { return mUtcTime; } diff --git a/core/tests/coretests/src/android/app/timedetector/PhoneTimeSuggestionTest.java b/core/tests/coretests/src/android/app/timedetector/PhoneTimeSuggestionTest.java index 1b5ad8868a01b..c9a86dcea84c7 100644 --- a/core/tests/coretests/src/android/app/timedetector/PhoneTimeSuggestionTest.java +++ b/core/tests/coretests/src/android/app/timedetector/PhoneTimeSuggestionTest.java @@ -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); @@ -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() diff --git a/services/core/java/com/android/server/timedetector/SimpleTimeDetectorStrategy.java b/services/core/java/com/android/server/timedetector/SimpleTimeDetectorStrategy.java index 9dbbf16e77347..fd8cc24b658b3 100644 --- a/services/core/java/com/android/server/timedetector/SimpleTimeDetectorStrategy.java +++ b/services/core/java/com/android/server/timedetector/SimpleTimeDetectorStrategy.java @@ -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) { diff --git a/services/tests/servicestests/src/com/android/server/timedetector/SimpleTimeZoneDetectorStrategyTest.java b/services/tests/servicestests/src/com/android/server/timedetector/SimpleTimeZoneDetectorStrategyTest.java index d79795593456b..b49845ae4006e 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/SimpleTimeZoneDetectorStrategyTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/SimpleTimeZoneDetectorStrategyTest.java @@ -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; @@ -99,7 +111,8 @@ public class SimpleTimeZoneDetectorStrategyTest { TimestampedValue 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(); @@ -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 = @@ -158,7 +172,8 @@ public class SimpleTimeZoneDetectorStrategyTest { long referenceTimeBeforeLastSignalMillis = utcTime1.getReferenceTimeMillis() - 1; TimestampedValue utcTime2 = new TimestampedValue<>( referenceTimeBeforeLastSignalMillis, validUtcTimeMillis); - PhoneTimeSuggestion timeSuggestion2 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); + PhoneTimeSuggestion timeSuggestion2 = + createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime2); mScript.simulatePhoneTimeSuggestion(timeSuggestion2) .verifySystemClockWasNotSetAndResetCallTracking(); @@ -168,7 +183,8 @@ public class SimpleTimeZoneDetectorStrategyTest { utcTime1.getReferenceTimeMillis() + Integer.MAX_VALUE + 1; TimestampedValue utcTime3 = new TimestampedValue<>( referenceTimeInFutureMillis, validUtcTimeMillis); - PhoneTimeSuggestion timeSuggestion3 = new PhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); + PhoneTimeSuggestion timeSuggestion3 = + createPhoneTimeSuggestion(ARBITRARY_PHONE_ID, utcTime3); mScript.simulatePhoneTimeSuggestion(timeSuggestion3) .verifySystemClockWasNotSetAndResetCallTracking(); @@ -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); } @@ -223,7 +240,8 @@ public class SimpleTimeZoneDetectorStrategyTest { TimestampedValue 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); @@ -465,7 +483,7 @@ public class SimpleTimeZoneDetectorStrategyTest { PhoneTimeSuggestion createPhoneTimeSuggestionForActual(int phoneId) { TimestampedValue time = new TimestampedValue<>( mInitialDeviceRealtimeMillis, mActualTimeMillis); - return new PhoneTimeSuggestion(phoneId, time); + return createPhoneTimeSuggestion(phoneId, time); } static class Builder { @@ -500,6 +518,13 @@ public class SimpleTimeZoneDetectorStrategyTest { } } + private static PhoneTimeSuggestion createPhoneTimeSuggestion(int phoneId, + TimestampedValue 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")); 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 37da01824e886..cfd8a457d2f03 100644 --- a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java @@ -117,8 +117,10 @@ public class TimeDetectorServiceTest { private static PhoneTimeSuggestion createPhoneTimeSuggestion() { int phoneId = 1234; + PhoneTimeSuggestion suggestion = new PhoneTimeSuggestion(phoneId); TimestampedValue timeValue = new TimestampedValue<>(100L, 1_000_000L); - return new PhoneTimeSuggestion(phoneId, timeValue); + suggestion.setUtcTime(timeValue); + return suggestion; } private static class StubbedTimeDetectorStrategy implements TimeDetectorStrategy {