diff --git a/core/java/android/app/time/TimeState.java b/core/java/android/app/time/TimeState.java index 15411e547814c..01c869d993382 100644 --- a/core/java/android/app/time/TimeState.java +++ b/core/java/android/app/time/TimeState.java @@ -63,6 +63,12 @@ public final class TimeState implements Parcelable { return new TimeState(unixEpochTime, userShouldConfirmId); } + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeParcelable(mUnixEpochTime, 0); + dest.writeBoolean(mUserShouldConfirmTime); + } + /** @hide */ @Nullable public static TimeState parseCommandLineArgs(@NonNull ShellCommand cmd) { @@ -119,12 +125,6 @@ public final class TimeState implements Parcelable { return 0; } - @Override - public void writeToParcel(@NonNull Parcel dest, int flags) { - dest.writeParcelable(mUnixEpochTime, 0); - dest.writeBoolean(mUserShouldConfirmTime); - } - @NonNull public UnixEpochTime getUnixEpochTime() { return mUnixEpochTime; diff --git a/core/java/android/app/time/TimeZoneState.java b/core/java/android/app/time/TimeZoneState.java index efdff81b079af..8e87111986ceb 100644 --- a/core/java/android/app/time/TimeZoneState.java +++ b/core/java/android/app/time/TimeZoneState.java @@ -58,11 +58,17 @@ public final class TimeZoneState implements Parcelable { } private static TimeZoneState createFromParcel(Parcel in) { - String zoneId = in.readString(); + String zoneId = in.readString8(); boolean userShouldConfirmId = in.readBoolean(); return new TimeZoneState(zoneId, userShouldConfirmId); } + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeString8(mId); + dest.writeBoolean(mUserShouldConfirmId); + } + /** @hide */ @Nullable public static TimeZoneState parseCommandLineArgs(@NonNull ShellCommand cmd) { @@ -107,12 +113,6 @@ public final class TimeZoneState implements Parcelable { return 0; } - @Override - public void writeToParcel(@NonNull Parcel dest, int flags) { - dest.writeString(mId); - dest.writeBoolean(mUserShouldConfirmId); - } - @NonNull public String getId() { return mId; diff --git a/core/java/android/app/time/UnixEpochTime.java b/core/java/android/app/time/UnixEpochTime.java index f1a176eacc9b9..576bf6453ecac 100644 --- a/core/java/android/app/time/UnixEpochTime.java +++ b/core/java/android/app/time/UnixEpochTime.java @@ -94,7 +94,6 @@ public final class UnixEpochTime implements Parcelable { } /** Returns the unix epoch time value. See {@link UnixEpochTime} for more information. */ - @Nullable public long getUnixEpochTimeMillis() { return mUnixEpochTimeMillis; } @@ -109,7 +108,7 @@ public final class UnixEpochTime implements Parcelable { } UnixEpochTime that = (UnixEpochTime) o; return mElapsedRealtimeMillis == that.mElapsedRealtimeMillis - && Objects.equals(mUnixEpochTimeMillis, that.mUnixEpochTimeMillis); + && mUnixEpochTimeMillis == that.mUnixEpochTimeMillis; } @Override @@ -125,32 +124,19 @@ public final class UnixEpochTime implements Parcelable { + '}'; } - public static final @NonNull Creator CREATOR = - new ClassLoaderCreator() { + public static final @NonNull Creator CREATOR = new Creator<>() { + @Override + public UnixEpochTime createFromParcel(@NonNull Parcel source) { + long elapsedRealtimeMillis = source.readLong(); + long unixEpochTimeMillis = source.readLong(); + return new UnixEpochTime(elapsedRealtimeMillis, unixEpochTimeMillis); + } - @Override - public UnixEpochTime createFromParcel(@NonNull Parcel source) { - return createFromParcel(source, null); - } - - @Override - public UnixEpochTime createFromParcel( - @NonNull Parcel source, @Nullable ClassLoader classLoader) { - long elapsedRealtimeMillis = source.readLong(); - long unixEpochTimeMillis = source.readLong(); - return new UnixEpochTime(elapsedRealtimeMillis, unixEpochTimeMillis); - } - - @Override - public UnixEpochTime[] newArray(int size) { - return new UnixEpochTime[size]; - } - }; - - @Override - public int describeContents() { - return 0; - } + @Override + public UnixEpochTime[] newArray(int size) { + return new UnixEpochTime[size]; + } + }; @Override public void writeToParcel(@NonNull Parcel dest, int flags) { @@ -158,6 +144,11 @@ public final class UnixEpochTime implements Parcelable { dest.writeLong(mUnixEpochTimeMillis); } + @Override + public int describeContents() { + return 0; + } + /** * Creates a new Unix epoch time value at {@code elapsedRealtimeTimeMillis} by adjusting this * Unix epoch time by the difference between the elapsed realtime value supplied and the one diff --git a/core/tests/coretests/src/android/app/time/TimeStateTest.java b/core/tests/coretests/src/android/app/time/TimeStateTest.java index a03229060dfb3..bce09099e4d32 100644 --- a/core/tests/coretests/src/android/app/time/TimeStateTest.java +++ b/core/tests/coretests/src/android/app/time/TimeStateTest.java @@ -16,12 +16,12 @@ package android.app.time; +import static android.app.timezonedetector.ParcelableTestSupport.assertRoundTripParcelable; import static android.app.timezonedetector.ShellCommandTestSupport.createShellCommandWithArgsAndOptions; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import android.os.Parcel; import android.os.ShellCommand; import androidx.test.runner.AndroidJUnit4; @@ -60,19 +60,8 @@ public class TimeStateTest { @Test public void testParceling() { UnixEpochTime time = new UnixEpochTime(1, 2); - TimeState value = new TimeState(time, true); - Parcel parcel = Parcel.obtain(); - try { - parcel.writeParcelable(value, 0); - - parcel.setDataPosition(0); - - TimeState stringValueCopy = - parcel.readParcelable(null /* classLoader */, TimeState.class); - assertEquals(value, stringValueCopy); - } finally { - parcel.recycle(); - } + assertRoundTripParcelable(new TimeState(time, true)); + assertRoundTripParcelable(new TimeState(time, false)); } @Test(expected = IllegalArgumentException.class) diff --git a/core/tests/coretests/src/android/app/time/TimeZoneStateTest.java b/core/tests/coretests/src/android/app/time/TimeZoneStateTest.java index 9786bb044cb27..35a9dbc200e43 100644 --- a/core/tests/coretests/src/android/app/time/TimeZoneStateTest.java +++ b/core/tests/coretests/src/android/app/time/TimeZoneStateTest.java @@ -16,12 +16,12 @@ package android.app.time; +import static android.app.timezonedetector.ParcelableTestSupport.assertRoundTripParcelable; import static android.app.timezonedetector.ShellCommandTestSupport.createShellCommandWithArgsAndOptions; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import android.os.Parcel; import android.os.ShellCommand; import androidx.test.runner.AndroidJUnit4; @@ -59,19 +59,8 @@ public class TimeZoneStateTest { @Test public void testParceling() { - TimeZoneState value = new TimeZoneState("Europe/London", true); - Parcel parcel = Parcel.obtain(); - try { - parcel.writeParcelable(value, 0); - - parcel.setDataPosition(0); - - TimeZoneState stringValueCopy = - parcel.readParcelable(null /* classLoader */, TimeZoneState.class); - assertEquals(value, stringValueCopy); - } finally { - parcel.recycle(); - } + assertRoundTripParcelable(new TimeZoneState("Europe/London", true)); + assertRoundTripParcelable(new TimeZoneState("Europe/London", false)); } @Test(expected = IllegalArgumentException.class) diff --git a/core/tests/coretests/src/android/app/time/UnixEpochTimeTest.java b/core/tests/coretests/src/android/app/time/UnixEpochTimeTest.java index cd753489b50e8..3ab01f3d88328 100644 --- a/core/tests/coretests/src/android/app/time/UnixEpochTimeTest.java +++ b/core/tests/coretests/src/android/app/time/UnixEpochTimeTest.java @@ -16,12 +16,12 @@ package android.app.time; +import static android.app.timezonedetector.ParcelableTestSupport.assertRoundTripParcelable; import static android.app.timezonedetector.ShellCommandTestSupport.createShellCommandWithArgsAndOptions; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import android.os.Parcel; import android.os.ShellCommand; import androidx.test.runner.AndroidJUnit4; @@ -57,19 +57,7 @@ public class UnixEpochTimeTest { @Test public void testParceling() { - UnixEpochTime value = new UnixEpochTime(1000, 1); - Parcel parcel = Parcel.obtain(); - try { - parcel.writeParcelable(value, 0); - - parcel.setDataPosition(0); - - UnixEpochTime stringValueCopy = - parcel.readParcelable(null /* classLoader */, UnixEpochTime.class); - assertEquals(value, stringValueCopy); - } finally { - parcel.recycle(); - } + assertRoundTripParcelable(new UnixEpochTime(1000, 1)); } @Test(expected = IllegalArgumentException.class)