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

Commit cfb6847e authored by Yan Yan's avatar Yan Yan
Browse files

Support converting integer to/from PersistableBundle

This CL is for facilitating converting Map and List that
contains integer type objects.

Bug: 163604823
Test: PersistableBundleUtilsTest(new tests added)
Change-Id: I24239caf70035e19c3fb5eb4a85b6a0c6ccadb5a
parent 42444745
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -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.
     *
+11 −0
Original line number Diff line number Diff line
@@ -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);
    }
}