Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e02dcaa9 authored by Neil Fuller's avatar Neil Fuller Committed by android-build-merger
Browse files

Merge "Allow for empty time suggestions"

am: a241a15c

Change-Id: Ib6e1ddbba9c474f06475a666d025ab8e0f98189b
parents 13dc5222 a241a15c
Loading
Loading
Loading
Loading
+13 −10
Original line number Diff line number Diff line
@@ -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<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;
@@ -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;
    }
+15 −8
Original line number Diff line number Diff line
@@ -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()
+5 −0
Original line number Diff line number Diff line
@@ -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) {
+32 −7
Original line number Diff line number Diff line
@@ -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<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();
@@ -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<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();

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

@@ -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<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);
@@ -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 {
@@ -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"));
+3 −1
Original line number Diff line number Diff line
@@ -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 {