From 376a91befa178332c08b37ce0b4e4dbd7351bb6f Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Wed, 12 Aug 2020 17:46:47 +0100 Subject: [PATCH] Basic changes to the LocationTimeZoneProvider API These changes have been broken out as they stand alone and should be relatively uncontroversial. The LocationTimeZoneEvent is having UserHandle added, mostly for logging at this point so we can debug issues with multi-user providers. Bug: 152744911 Bug: 149014708 Test: atest services/tests/servicestests/src/android/location/timezone/LocationTimeZoneEventTest.java Change-Id: Iafbd5bc9778d68bc9c3046244e7dca6d9892519e --- .../timezone/LocationTimeZoneEvent.java | 54 ++++++++++++++----- .../LocationTimeZoneProviderBase.java | 2 +- ...ationTimeZoneProviderRequestUnbundled.java | 18 +++++++ .../timezone/LocationTimeZoneEventTest.java | 27 ++++++++++ 4 files changed, 86 insertions(+), 15 deletions(-) diff --git a/location/java/android/location/timezone/LocationTimeZoneEvent.java b/location/java/android/location/timezone/LocationTimeZoneEvent.java index ea3353c245e5a..540bdfffe16ac 100644 --- a/location/java/android/location/timezone/LocationTimeZoneEvent.java +++ b/location/java/android/location/timezone/LocationTimeZoneEvent.java @@ -21,6 +21,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.os.Parcel; import android.os.Parcelable; +import android.os.UserHandle; import java.util.ArrayList; import java.util.Collections; @@ -37,7 +38,7 @@ public final class LocationTimeZoneEvent implements Parcelable { @IntDef({ EVENT_TYPE_UNKNOWN, EVENT_TYPE_SUCCESS, EVENT_TYPE_SUCCESS }) @interface EventType {} - /** Uninitialized value for {@link #mEventType} */ + /** Uninitialized value for {@link #mEventType} - must not be used for real events. */ private static final int EVENT_TYPE_UNKNOWN = 0; /** @@ -60,6 +61,9 @@ public final class LocationTimeZoneEvent implements Parcelable { private static final int EVENT_TYPE_MAX = EVENT_TYPE_UNCERTAIN; + @NonNull + private final UserHandle mUserHandle; + @EventType private final int mEventType; @@ -68,8 +72,9 @@ public final class LocationTimeZoneEvent implements Parcelable { private final long mElapsedRealtimeNanos; - private LocationTimeZoneEvent(@EventType int eventType, @NonNull List timeZoneIds, - long elapsedRealtimeNanos) { + private LocationTimeZoneEvent(@NonNull UserHandle userHandle, @EventType int eventType, + @NonNull List timeZoneIds, long elapsedRealtimeNanos) { + mUserHandle = Objects.requireNonNull(userHandle); mEventType = checkValidEventType(eventType); mTimeZoneIds = immutableList(timeZoneIds); @@ -82,6 +87,14 @@ public final class LocationTimeZoneEvent implements Parcelable { mElapsedRealtimeNanos = elapsedRealtimeNanos; } + /** + * Returns the current user when the event was generated. + */ + @NonNull + public UserHandle getUserHandle() { + return mUserHandle; + } + /** * Returns the time of this fix, in elapsed real-time since system boot. * @@ -117,7 +130,8 @@ public final class LocationTimeZoneEvent implements Parcelable { @Override public String toString() { return "LocationTimeZoneEvent{" - + "mEventType=" + mEventType + + "mUserHandle=" + mUserHandle + + ", mEventType=" + mEventType + ", mTimeZoneIds=" + mTimeZoneIds + ", mElapsedRealtimeNanos=" + mElapsedRealtimeNanos + '}'; @@ -127,12 +141,14 @@ public final class LocationTimeZoneEvent implements Parcelable { new Parcelable.Creator() { @Override public LocationTimeZoneEvent createFromParcel(Parcel in) { + UserHandle userHandle = UserHandle.readFromParcel(in); int eventType = in.readInt(); @SuppressWarnings("unchecked") ArrayList timeZoneIds = (ArrayList) in.readArrayList(null /* classLoader */); long elapsedRealtimeNanos = in.readLong(); - return new LocationTimeZoneEvent(eventType, timeZoneIds, elapsedRealtimeNanos); + return new LocationTimeZoneEvent( + userHandle, eventType, timeZoneIds, elapsedRealtimeNanos); } @Override @@ -148,6 +164,7 @@ public final class LocationTimeZoneEvent implements Parcelable { @Override public void writeToParcel(Parcel parcel, int flags) { + mUserHandle.writeToParcel(parcel, flags); parcel.writeInt(mEventType); parcel.writeList(mTimeZoneIds); parcel.writeLong(mElapsedRealtimeNanos); @@ -162,19 +179,21 @@ public final class LocationTimeZoneEvent implements Parcelable { return false; } LocationTimeZoneEvent that = (LocationTimeZoneEvent) o; - return mEventType == that.mEventType + return mUserHandle.equals(that.mUserHandle) + && mEventType == that.mEventType && mElapsedRealtimeNanos == that.mElapsedRealtimeNanos && mTimeZoneIds.equals(that.mTimeZoneIds); } @Override public int hashCode() { - return Objects.hash(mEventType, mTimeZoneIds, mElapsedRealtimeNanos); + return Objects.hash(mUserHandle, mEventType, mTimeZoneIds, mElapsedRealtimeNanos); } /** @hide */ public static final class Builder { + private UserHandle mUserHandle; private @EventType int mEventType = EVENT_TYPE_UNKNOWN; private @NonNull List mTimeZoneIds = Collections.emptyList(); private long mElapsedRealtimeNanos; @@ -186,13 +205,22 @@ public final class LocationTimeZoneEvent implements Parcelable { * Sets the contents of this from the supplied instance. */ public Builder(@NonNull LocationTimeZoneEvent ltz) { + mUserHandle = ltz.mUserHandle; mEventType = ltz.mEventType; mTimeZoneIds = ltz.mTimeZoneIds; mElapsedRealtimeNanos = ltz.mElapsedRealtimeNanos; } /** - * Set the time zone ID of this fix. + * Set the current user when this event was generated. + */ + public Builder setUserHandle(@NonNull UserHandle userHandle) { + mUserHandle = Objects.requireNonNull(userHandle); + return this; + } + + /** + * Set the time zone ID of this event. */ public Builder setEventType(@EventType int eventType) { checkValidEventType(eventType); @@ -201,7 +229,7 @@ public final class LocationTimeZoneEvent implements Parcelable { } /** - * Sets the time zone IDs of this fix. + * Sets the time zone IDs of this event. */ public Builder setTimeZoneIds(@NonNull List timeZoneIds) { mTimeZoneIds = Objects.requireNonNull(timeZoneIds); @@ -209,9 +237,7 @@ public final class LocationTimeZoneEvent implements Parcelable { } /** - * Sets the time of this fix, in elapsed real-time since system boot. - * - * @param time elapsed real-time of fix, in nanoseconds since system boot. + * Sets the time of this event, in elapsed real-time since system boot. */ public Builder setElapsedRealtimeNanos(long time) { mElapsedRealtimeNanos = time; @@ -222,8 +248,8 @@ public final class LocationTimeZoneEvent implements Parcelable { * Builds a {@link LocationTimeZoneEvent} instance. */ public LocationTimeZoneEvent build() { - return new LocationTimeZoneEvent(this.mEventType, this.mTimeZoneIds, - this.mElapsedRealtimeNanos); + return new LocationTimeZoneEvent( + mUserHandle, mEventType, mTimeZoneIds, mElapsedRealtimeNanos); } } diff --git a/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneProviderBase.java b/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneProviderBase.java index 0143c88e0898d..c533c20d07e33 100644 --- a/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneProviderBase.java +++ b/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneProviderBase.java @@ -32,7 +32,7 @@ import java.util.Objects; /** * Base class for location time zone providers implemented as unbundled services. * - * TODO Provide details of the expected service actions. + * TODO(b/152744911): Provide details of the expected service actions and threading. * *

IMPORTANT: This class is effectively a public API for unbundled applications, and must remain * API stable. diff --git a/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneProviderRequestUnbundled.java b/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneProviderRequestUnbundled.java index dd80466637e67..e898bbf3ecc0c 100644 --- a/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneProviderRequestUnbundled.java +++ b/location/lib/java/com/android/location/timezone/provider/LocationTimeZoneProviderRequestUnbundled.java @@ -43,6 +43,24 @@ public final class LocationTimeZoneProviderRequestUnbundled { return mRequest.getReportLocationTimeZone(); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + LocationTimeZoneProviderRequestUnbundled that = + (LocationTimeZoneProviderRequestUnbundled) o; + return mRequest.equals(that.mRequest); + } + + @Override + public int hashCode() { + return Objects.hash(mRequest); + } + @Override public String toString() { return mRequest.toString(); diff --git a/services/tests/servicestests/src/android/location/timezone/LocationTimeZoneEventTest.java b/services/tests/servicestests/src/android/location/timezone/LocationTimeZoneEventTest.java index 80373ac661098..f9dd7dc86ad58 100644 --- a/services/tests/servicestests/src/android/location/timezone/LocationTimeZoneEventTest.java +++ b/services/tests/servicestests/src/android/location/timezone/LocationTimeZoneEventTest.java @@ -23,6 +23,8 @@ import static org.junit.Assert.assertNotEquals; import static java.util.Collections.singletonList; +import android.os.UserHandle; + import org.junit.Test; import java.util.List; @@ -33,6 +35,10 @@ public class LocationTimeZoneEventTest { private static final List ARBITRARY_TIME_ZONE_IDS = singletonList("Europe/London"); + private static final UserHandle ARBITRARY_USER_HANDLE = UserHandle.SYSTEM; + private static final UserHandle ARBITRARY_USER_HANDLE2 = + UserHandle.of(ARBITRARY_USER_HANDLE.getIdentifier() + 1); + @Test(expected = RuntimeException.class) public void testSetInvalidEventType() { new LocationTimeZoneEvent.Builder().setEventType(Integer.MAX_VALUE); @@ -41,6 +47,7 @@ public class LocationTimeZoneEventTest { @Test(expected = RuntimeException.class) public void testBuildUnsetEventType() { new LocationTimeZoneEvent.Builder() + .setUserHandle(ARBITRARY_USER_HANDLE) .setTimeZoneIds(ARBITRARY_TIME_ZONE_IDS) .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS) .build(); @@ -49,6 +56,7 @@ public class LocationTimeZoneEventTest { @Test(expected = RuntimeException.class) public void testInvalidTimeZoneIds() { new LocationTimeZoneEvent.Builder() + .setUserHandle(ARBITRARY_USER_HANDLE) .setEventType(LocationTimeZoneEvent.EVENT_TYPE_UNCERTAIN) .setTimeZoneIds(ARBITRARY_TIME_ZONE_IDS) .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS) @@ -58,6 +66,7 @@ public class LocationTimeZoneEventTest { @Test public void testEquals() { LocationTimeZoneEvent.Builder builder1 = new LocationTimeZoneEvent.Builder() + .setUserHandle(ARBITRARY_USER_HANDLE) .setEventType(LocationTimeZoneEvent.EVENT_TYPE_UNCERTAIN) .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS); { @@ -66,6 +75,7 @@ public class LocationTimeZoneEventTest { } LocationTimeZoneEvent.Builder builder2 = new LocationTimeZoneEvent.Builder() + .setUserHandle(ARBITRARY_USER_HANDLE) .setEventType(LocationTimeZoneEvent.EVENT_TYPE_UNCERTAIN) .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS); { @@ -75,6 +85,22 @@ public class LocationTimeZoneEventTest { assertEquals(two, one); } + builder1.setUserHandle(ARBITRARY_USER_HANDLE2); + { + LocationTimeZoneEvent one = builder1.build(); + LocationTimeZoneEvent two = builder2.build(); + assertNotEquals(one, two); + assertNotEquals(two, one); + } + + builder2.setUserHandle(ARBITRARY_USER_HANDLE2); + { + LocationTimeZoneEvent one = builder1.build(); + LocationTimeZoneEvent two = builder2.build(); + assertEquals(one, two); + assertEquals(two, one); + } + builder1.setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS + 1); { LocationTimeZoneEvent one = builder1.build(); @@ -127,6 +153,7 @@ public class LocationTimeZoneEventTest { @Test public void testParcelable() { LocationTimeZoneEvent.Builder builder = new LocationTimeZoneEvent.Builder() + .setUserHandle(ARBITRARY_USER_HANDLE) .setEventType(LocationTimeZoneEvent.EVENT_TYPE_PERMANENT_FAILURE) .setElapsedRealtimeNanos(ARBITRARY_ELAPSED_REALTIME_NANOS); assertRoundTripParcelable(builder.build());