From 110409d7767b8c75888dbb8f4cf44c50fcc0e20d Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Wed, 25 Nov 2020 10:27:30 +0000 Subject: [PATCH] Change LocationTimeZoneEvent elapsed realtime Switch LocationTimeZoneEvent to elapsedRealtimeMillis from nanos. This makes it more consistent with other usages of the elapsed realtime clock in time zone detection. This information is currently only used for debug so there will be no functional change. Also change LocationTimeZoneEvent toString() to additionally report mElapsedRealtimeMillis value using Duration.toString(); this is done elsewhere in debug output for time zone detection so makes understanding log output easier. Bug: 169304499 Test: build only Change-Id: If307c9b27d64ca49a4fcc0d9bb6e36f362990b3e --- .../timezone/LocationTimeZoneEvent.java | 38 ++++++++++--------- .../LocationTimeZoneEventUnbundled.java | 2 +- .../SimulatedBinderProviderEvent.java | 2 +- .../timezone/LocationTimeZoneEventTest.java | 16 ++++---- .../location/timezone/ControllerImplTest.java | 4 +- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/location/java/com/android/internal/location/timezone/LocationTimeZoneEvent.java b/location/java/com/android/internal/location/timezone/LocationTimeZoneEvent.java index 7eb843e93e68d..31c27d1bb977d 100644 --- a/location/java/com/android/internal/location/timezone/LocationTimeZoneEvent.java +++ b/location/java/com/android/internal/location/timezone/LocationTimeZoneEvent.java @@ -24,6 +24,7 @@ import android.os.Parcelable; import com.android.internal.util.Preconditions; +import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -70,30 +71,30 @@ public final class LocationTimeZoneEvent implements Parcelable { @NonNull private final List mTimeZoneIds; - private final long mElapsedRealtimeNanos; + private final long mElapsedRealtimeMillis; private LocationTimeZoneEvent(@EventType int eventType, @NonNull List timeZoneIds, - long elapsedRealtimeNanos) { + long elapsedRealtimeMillis) { mEventType = checkValidEventType(eventType); mTimeZoneIds = immutableList(timeZoneIds); boolean emptyTimeZoneIdListExpected = eventType != EVENT_TYPE_SUCCESS; Preconditions.checkState(!emptyTimeZoneIdListExpected || timeZoneIds.isEmpty()); - mElapsedRealtimeNanos = elapsedRealtimeNanos; + mElapsedRealtimeMillis = elapsedRealtimeMillis; } /** * Returns the time of this fix, in elapsed real-time since system boot. * *

This value can be reliably compared to {@link - * android.os.SystemClock#elapsedRealtimeNanos}, to calculate the age of a fix and to compare + * android.os.SystemClock#elapsedRealtime()}, to calculate the age of a fix and to compare * {@link LocationTimeZoneEvent} instances. * - * @return elapsed real-time of fix, in nanoseconds since system boot. + * @return elapsed real-time of fix, in milliseconds */ - public long getElapsedRealtimeNanos() { - return mElapsedRealtimeNanos; + public long getElapsedRealtimeMillis() { + return mElapsedRealtimeMillis; } /** @@ -118,7 +119,8 @@ public final class LocationTimeZoneEvent implements Parcelable { return "LocationTimeZoneEvent{" + "mEventType=" + mEventType + ", mTimeZoneIds=" + mTimeZoneIds - + ", mElapsedRealtimeNanos=" + mElapsedRealtimeNanos + + ", mElapsedRealtimeMillis=" + mElapsedRealtimeMillis + + "(" + Duration.ofMillis(mElapsedRealtimeMillis) + ")" + '}'; } @@ -130,8 +132,8 @@ public final class LocationTimeZoneEvent implements Parcelable { @SuppressWarnings("unchecked") ArrayList timeZoneIds = (ArrayList) in.readArrayList(null /* classLoader */); - long elapsedRealtimeNanos = in.readLong(); - return new LocationTimeZoneEvent(eventType, timeZoneIds, elapsedRealtimeNanos); + long elapsedRealtimeMillis = in.readLong(); + return new LocationTimeZoneEvent(eventType, timeZoneIds, elapsedRealtimeMillis); } @Override @@ -149,7 +151,7 @@ public final class LocationTimeZoneEvent implements Parcelable { public void writeToParcel(Parcel parcel, int flags) { parcel.writeInt(mEventType); parcel.writeList(mTimeZoneIds); - parcel.writeLong(mElapsedRealtimeNanos); + parcel.writeLong(mElapsedRealtimeMillis); } @Override @@ -162,13 +164,13 @@ public final class LocationTimeZoneEvent implements Parcelable { } LocationTimeZoneEvent that = (LocationTimeZoneEvent) o; return mEventType == that.mEventType - && mElapsedRealtimeNanos == that.mElapsedRealtimeNanos + && mElapsedRealtimeMillis == that.mElapsedRealtimeMillis && mTimeZoneIds.equals(that.mTimeZoneIds); } @Override public int hashCode() { - return Objects.hash(mEventType, mTimeZoneIds, mElapsedRealtimeNanos); + return Objects.hash(mEventType, mTimeZoneIds, mElapsedRealtimeMillis); } /** @hide */ @@ -176,7 +178,7 @@ public final class LocationTimeZoneEvent implements Parcelable { private @EventType int mEventType = EVENT_TYPE_UNKNOWN; private @NonNull List mTimeZoneIds = Collections.emptyList(); - private long mElapsedRealtimeNanos; + private long mElapsedRealtimeMillis; public Builder() { } @@ -187,7 +189,7 @@ public final class LocationTimeZoneEvent implements Parcelable { public Builder(@NonNull LocationTimeZoneEvent ltz) { mEventType = ltz.mEventType; mTimeZoneIds = ltz.mTimeZoneIds; - mElapsedRealtimeNanos = ltz.mElapsedRealtimeNanos; + mElapsedRealtimeMillis = ltz.mElapsedRealtimeMillis; } /** @@ -210,8 +212,8 @@ public final class LocationTimeZoneEvent implements Parcelable { /** * Sets the time of this event, in elapsed real-time since system boot. */ - public Builder setElapsedRealtimeNanos(long time) { - mElapsedRealtimeNanos = time; + public Builder setElapsedRealtimeMillis(long time) { + mElapsedRealtimeMillis = time; return this; } @@ -219,7 +221,7 @@ public final class LocationTimeZoneEvent implements Parcelable { * Builds a {@link LocationTimeZoneEvent} instance. */ public LocationTimeZoneEvent build() { - return new LocationTimeZoneEvent(mEventType, mTimeZoneIds, mElapsedRealtimeNanos); + return new LocationTimeZoneEvent(mEventType, mTimeZoneIds, mElapsedRealtimeMillis); } } diff --git a/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneEventUnbundled.java b/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneEventUnbundled.java index d044c0b529394..55f5545544740 100644 --- a/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneEventUnbundled.java +++ b/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneEventUnbundled.java @@ -147,7 +147,7 @@ public final class LocationTimeZoneEventUnbundled { LocationTimeZoneEvent event = new LocationTimeZoneEvent.Builder() .setEventType(internalEventType) .setTimeZoneIds(mTimeZoneIds) - .setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()) + .setElapsedRealtimeMillis(SystemClock.elapsedRealtime()) .build(); return new LocationTimeZoneEventUnbundled(event); } diff --git a/services/core/java/com/android/server/location/timezone/SimulatedBinderProviderEvent.java b/services/core/java/com/android/server/location/timezone/SimulatedBinderProviderEvent.java index 9d262e33274bb..77253099d54f1 100644 --- a/services/core/java/com/android/server/location/timezone/SimulatedBinderProviderEvent.java +++ b/services/core/java/com/android/server/location/timezone/SimulatedBinderProviderEvent.java @@ -117,7 +117,7 @@ final class SimulatedBinderProviderEvent { private static LocationTimeZoneEvent parseLocationTimeZoneEventArgs(ShellCommand shellCommand) { LocationTimeZoneEvent.Builder eventBuilder = new LocationTimeZoneEvent.Builder() - .setElapsedRealtimeNanos(SystemClock.elapsedRealtime()); + .setElapsedRealtimeMillis(SystemClock.elapsedRealtime()); String eventTypeString = shellCommand.getNextArgRequired(); switch (eventTypeString.toUpperCase()) { diff --git a/services/tests/servicestests/src/com/android/internal/location/timezone/LocationTimeZoneEventTest.java b/services/tests/servicestests/src/com/android/internal/location/timezone/LocationTimeZoneEventTest.java index 9efb38a75f358..84b886eb4b3df 100644 --- a/services/tests/servicestests/src/com/android/internal/location/timezone/LocationTimeZoneEventTest.java +++ b/services/tests/servicestests/src/com/android/internal/location/timezone/LocationTimeZoneEventTest.java @@ -29,7 +29,7 @@ import java.util.List; public class LocationTimeZoneEventTest { - private static final long ARBITRARY_ELAPSED_REALTIME_NANOS = 9999; + private static final long ARBITRARY_ELAPSED_REALTIME_MILLIS = 9999; private static final List ARBITRARY_TIME_ZONE_IDS = singletonList("Europe/London"); @@ -42,7 +42,7 @@ public class LocationTimeZoneEventTest { public void testBuildUnsetEventType() { new LocationTimeZoneEvent.Builder() .setTimeZoneIds(ARBITRARY_TIME_ZONE_IDS) - .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS) + .setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS) .build(); } @@ -51,7 +51,7 @@ public class LocationTimeZoneEventTest { new LocationTimeZoneEvent.Builder() .setEventType(LocationTimeZoneEvent.EVENT_TYPE_UNCERTAIN) .setTimeZoneIds(ARBITRARY_TIME_ZONE_IDS) - .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS) + .setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS) .build(); } @@ -59,7 +59,7 @@ public class LocationTimeZoneEventTest { public void testEquals() { LocationTimeZoneEvent.Builder builder1 = new LocationTimeZoneEvent.Builder() .setEventType(LocationTimeZoneEvent.EVENT_TYPE_UNCERTAIN) - .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS); + .setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS); { LocationTimeZoneEvent one = builder1.build(); assertEquals(one, one); @@ -67,7 +67,7 @@ public class LocationTimeZoneEventTest { LocationTimeZoneEvent.Builder builder2 = new LocationTimeZoneEvent.Builder() .setEventType(LocationTimeZoneEvent.EVENT_TYPE_UNCERTAIN) - .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS); + .setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS); { LocationTimeZoneEvent one = builder1.build(); LocationTimeZoneEvent two = builder2.build(); @@ -75,7 +75,7 @@ public class LocationTimeZoneEventTest { assertEquals(two, one); } - builder1.setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS + 1); + builder1.setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS + 1); { LocationTimeZoneEvent one = builder1.build(); LocationTimeZoneEvent two = builder2.build(); @@ -83,7 +83,7 @@ public class LocationTimeZoneEventTest { assertNotEquals(two, one); } - builder2.setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS + 1); + builder2.setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS + 1); { LocationTimeZoneEvent one = builder1.build(); LocationTimeZoneEvent two = builder2.build(); @@ -128,7 +128,7 @@ public class LocationTimeZoneEventTest { public void testParcelable() { LocationTimeZoneEvent.Builder builder = new LocationTimeZoneEvent.Builder() .setEventType(LocationTimeZoneEvent.EVENT_TYPE_PERMANENT_FAILURE) - .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS); + .setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS); assertRoundTripParcelable(builder.build()); builder.setEventType(LocationTimeZoneEvent.EVENT_TYPE_SUCCESS) diff --git a/services/tests/servicestests/src/com/android/server/location/timezone/ControllerImplTest.java b/services/tests/servicestests/src/com/android/server/location/timezone/ControllerImplTest.java index 3f1653718fe73..00cef8fb84818 100644 --- a/services/tests/servicestests/src/com/android/server/location/timezone/ControllerImplTest.java +++ b/services/tests/servicestests/src/com/android/server/location/timezone/ControllerImplTest.java @@ -59,7 +59,7 @@ import java.util.Objects; @Presubmit public class ControllerImplTest { - private static final long ARBITRARY_TIME = 12345L; + private static final long ARBITRARY_TIME_MILLIS = 12345L; private static final LocationTimeZoneEvent USER1_SUCCESS_LOCATION_TIME_ZONE_EVENT1 = createLocationTimeZoneEvent(EVENT_TYPE_SUCCESS, asList("Europe/London")); @@ -935,7 +935,7 @@ public class ControllerImplTest { private static LocationTimeZoneEvent createLocationTimeZoneEvent( int eventType, @Nullable List timeZoneIds) { LocationTimeZoneEvent.Builder builder = new LocationTimeZoneEvent.Builder() - .setElapsedRealtimeNanos(ARBITRARY_TIME) + .setElapsedRealtimeMillis(ARBITRARY_TIME_MILLIS) .setEventType(eventType); if (timeZoneIds != null) { builder.setTimeZoneIds(timeZoneIds);