summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--services/core/java/com/android/server/vcn/util/PersistableBundleUtils.java16
-rw-r--r--tests/vcn/java/com/android/server/vcn/util/PersistableBundleUtilsTest.java11
2 files changed, 27 insertions, 0 deletions
diff --git a/services/core/java/com/android/server/vcn/util/PersistableBundleUtils.java b/services/core/java/com/android/server/vcn/util/PersistableBundleUtils.java
index 3672f9a2465e..5c1b5ffb2209 100644
--- a/services/core/java/com/android/server/vcn/util/PersistableBundleUtils.java
+++ b/services/core/java/com/android/server/vcn/util/PersistableBundleUtils.java
@@ -45,6 +45,7 @@ public class PersistableBundleUtils {
private static final String PARCEL_UUID_KEY = "PARCEL_UUID";
private static final String BYTE_ARRAY_KEY = "BYTE_ARRAY_KEY";
+ private static final String INTEGER_KEY = "INTEGER_KEY";
/**
* Functional interface to convert an object of the specified type to a PersistableBundle.
@@ -75,6 +76,21 @@ public class PersistableBundleUtils {
T fromPersistableBundle(PersistableBundle in);
}
+ /** Serializer to convert an integer to a PersistableBundle. */
+ public static final Serializer<Integer> INTEGER_SERIALIZER =
+ (i) -> {
+ final PersistableBundle result = new PersistableBundle();
+ result.putInt(INTEGER_KEY, i);
+ return result;
+ };
+
+ /** Deserializer to convert a PersistableBundle to an integer. */
+ public static final Deserializer<Integer> INTEGER_DESERIALIZER =
+ (bundle) -> {
+ Objects.requireNonNull(bundle, "PersistableBundle is null");
+ return bundle.getInt(INTEGER_KEY);
+ };
+
/**
* Converts a ParcelUuid to a PersistableBundle.
*
diff --git a/tests/vcn/java/com/android/server/vcn/util/PersistableBundleUtilsTest.java b/tests/vcn/java/com/android/server/vcn/util/PersistableBundleUtilsTest.java
index 4d8424bc8726..a44a734a2dce 100644
--- a/tests/vcn/java/com/android/server/vcn/util/PersistableBundleUtilsTest.java
+++ b/tests/vcn/java/com/android/server/vcn/util/PersistableBundleUtilsTest.java
@@ -200,4 +200,15 @@ public class PersistableBundleUtilsTest {
assertArrayEquals(byteArray, result);
}
+
+ @Test
+ public void testIntegerConversionLossless() throws Exception {
+ final int testInt = 1;
+ final PersistableBundle integerBundle =
+ PersistableBundleUtils.INTEGER_SERIALIZER.toPersistableBundle(testInt);
+ final int result =
+ PersistableBundleUtils.INTEGER_DESERIALIZER.fromPersistableBundle(integerBundle);
+
+ assertEquals(testInt, result);
+ }
}