Created
April 16, 2018 12:33
-
-
Save Steppschuh/31ce136673ce8057a2468cf8f576b3c1 to your computer and use it in GitHub Desktop.
Convenience methods to convert Android Bundle to PersistableBundle and back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.BaseBundle; | |
import android.os.Bundle; | |
import android.os.PersistableBundle; | |
public final class BundleUtil { | |
/** | |
* Creates a new {@link Bundle} based on the specified {@link PersistableBundle}. | |
*/ | |
public static Bundle toBundle(PersistableBundle persistableBundle) { | |
if (persistableBundle == null) { | |
return null; | |
} | |
Bundle bundle = new Bundle(); | |
bundle.putAll(persistableBundle); | |
return bundle; | |
} | |
/** | |
* Creates a new {@link PersistableBundle} from the specified {@link Bundle}. | |
* Will ignore all values that are not persistable, according | |
* to {@link #isPersistableBundleType(Object)}. | |
*/ | |
public static PersistableBundle toPersistableBundle(Bundle bundle) { | |
if (bundle == null) { | |
return null; | |
} | |
PersistableBundle persistableBundle = new PersistableBundle(); | |
for (String key : bundle.keySet()) { | |
Object value = bundle.get(key); | |
if (isPersistableBundleType(value)) { | |
putIntoBundle(persistableBundle, key, value); | |
} | |
} | |
return persistableBundle; | |
} | |
/** | |
* Checks if the specified object can be put into a {@link PersistableBundle}. | |
* | |
* @see <a href="https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/PersistableBundle.java#49">PersistableBundle Implementation</a> | |
*/ | |
public static boolean isPersistableBundleType(Object value) { | |
return ((value instanceof PersistableBundle) || | |
(value instanceof Integer) || (value instanceof int[]) || | |
(value instanceof Long) || (value instanceof long[]) || | |
(value instanceof Double) || (value instanceof double[]) || | |
(value instanceof String) || (value instanceof String[]) || | |
(value instanceof Boolean) || (value instanceof boolean[]) | |
); | |
} | |
/** | |
* Attempts to insert the specified key value pair into the specified bundle. | |
* | |
* @throws IllegalArgumentException if the value type can not be put into the bundle. | |
*/ | |
public static void putIntoBundle(BaseBundle baseBundle, String key, Object value) throws IllegalArgumentException { | |
if (value == null) { | |
throw new IllegalArgumentException("Unable to determine type of null values"); | |
} else if (value instanceof Integer) { | |
baseBundle.putInt(key, (int) value); | |
} else if (value instanceof int[]) { | |
baseBundle.putIntArray(key, (int[]) value); | |
} else if (value instanceof Long) { | |
baseBundle.putLong(key, (long) value); | |
} else if (value instanceof long[]) { | |
baseBundle.putLongArray(key, (long[]) value); | |
} else if (value instanceof Double) { | |
baseBundle.putDouble(key, (double) value); | |
} else if (value instanceof double[]) { | |
baseBundle.putDoubleArray(key, (double[]) value); | |
} else if (value instanceof String) { | |
baseBundle.putString(key, (String) value); | |
} else if (value instanceof String[]) { | |
baseBundle.putStringArray(key, (String[]) value); | |
} else if (value instanceof Boolean) { | |
baseBundle.putBoolean(key, (boolean) value); | |
} else if (value instanceof boolean[]) { | |
baseBundle.putBooleanArray(key, (boolean[]) value); | |
} else if (value instanceof PersistableBundle) { | |
baseBundle.putAll((PersistableBundle) value); | |
} else { | |
throw new IllegalArgumentException("Objects of type " + value.getClass().getSimpleName() | |
+ " can not be put into a " + BaseBundle.class.getSimpleName()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.os.PersistableBundle; | |
import android.util.ArrayMap; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import java.util.Date; | |
import java.util.Map; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertTrue; | |
import static org.junit.Assert.fail; | |
public class BundleUtilTest { | |
private static final Map<String, Object> persistableObjects = new ArrayMap<>(); | |
private static final Map<String, Object> nonPersistableObjects = new ArrayMap<>(); | |
@BeforeClass | |
public static void setUp() throws Exception { | |
persistableObjects.put(Integer.class.getSimpleName(), 123); | |
persistableObjects.put(int.class.getSimpleName(), new int[]{123}); | |
persistableObjects.put(Long.class.getSimpleName(), 123L); | |
persistableObjects.put(long[].class.getSimpleName(), new long[]{123L}); | |
persistableObjects.put(Double.class.getSimpleName(), 123.0); | |
persistableObjects.put(double[].class.getSimpleName(), new double[]{123.0}); | |
persistableObjects.put(String.class.getSimpleName(), "123"); | |
persistableObjects.put(String.class.getSimpleName() + "[]", new String[]{"123"}); | |
persistableObjects.put(Boolean.class.getSimpleName(), true); | |
persistableObjects.put(boolean[].class.getSimpleName(), new boolean[]{true}); | |
nonPersistableObjects.put("Null", null); | |
nonPersistableObjects.put(IllegalArgumentException.class.getSimpleName(), new IllegalArgumentException()); | |
nonPersistableObjects.put(Date.class.getSimpleName(), new Date(0)); | |
} | |
@Test | |
public void toBundle_validPersistableBundle_validBundle() { | |
PersistableBundle persistableBundle = new PersistableBundle(); | |
for (Map.Entry<String, Object> objectEntry : persistableObjects.entrySet()) { | |
BundleUtil.putIntoBundle(persistableBundle, objectEntry.getKey(), objectEntry.getValue()); | |
} | |
Bundle bundle = BundleUtil.toBundle(persistableBundle); | |
for (Map.Entry<String, Object> objectEntry : persistableObjects.entrySet()) { | |
assertTrue(objectEntry.getKey() + " should be in bundle", bundle.containsKey(objectEntry.getKey())); | |
} | |
} | |
@Test | |
public void toPersistableBundle_validBundle_validPersistableBundle() { | |
Bundle bundle = new Bundle(); | |
for (Map.Entry<String, Object> objectEntry : persistableObjects.entrySet()) { | |
BundleUtil.putIntoBundle(bundle, objectEntry.getKey(), objectEntry.getValue()); | |
} | |
bundle.putBundle(Bundle.class.getSimpleName(), new Bundle()); | |
bundle.putByte(Byte.class.getSimpleName(), (byte) 0); | |
bundle.putByte(Byte.class.getSimpleName(), (byte) 0); | |
PersistableBundle persistableBundle = BundleUtil.toPersistableBundle(bundle); | |
for (Map.Entry<String, Object> objectEntry : persistableObjects.entrySet()) { | |
assertTrue(objectEntry.getKey() + " should be in bundle", persistableBundle.containsKey(objectEntry.getKey())); | |
} | |
for (Map.Entry<String, Object> objectEntry : nonPersistableObjects.entrySet()) { | |
assertFalse(objectEntry.getKey() + " should not be in bundle", persistableBundle.containsKey(objectEntry.getKey())); | |
} | |
} | |
@Test | |
public void isPersistableBundleType() { | |
for (Map.Entry<String, Object> objectEntry : persistableObjects.entrySet()) { | |
assertTrue(objectEntry.getKey() + " should be persistable", BundleUtil.isPersistableBundleType(objectEntry.getValue())); | |
} | |
for (Map.Entry<String, Object> objectEntry : nonPersistableObjects.entrySet()) { | |
assertFalse(objectEntry.getKey() + " should not be persistable", BundleUtil.isPersistableBundleType(objectEntry.getValue())); | |
} | |
} | |
@Test | |
public void putIntoBundle_persistableValues_addedToBundle() { | |
Bundle bundle = new Bundle(); | |
for (Map.Entry<String, Object> objectEntry : persistableObjects.entrySet()) { | |
BundleUtil.putIntoBundle(bundle, objectEntry.getKey(), objectEntry.getValue()); | |
Object actual = bundle.get(objectEntry.getKey()); | |
assertTrue(objectEntry.getKey() + " should be in bundle", bundle.containsKey(objectEntry.getKey())); | |
assertEquals(objectEntry.getKey() + " does not equal original value", objectEntry.getValue(), actual); | |
} | |
} | |
@Test | |
public void putIntoBundle_nonPersistableValues_throwsException() { | |
Bundle bundle = new Bundle(); | |
for (Map.Entry<String, Object> objectEntry : nonPersistableObjects.entrySet()) { | |
try { | |
BundleUtil.putIntoBundle(bundle, objectEntry.getKey(), objectEntry.getValue()); | |
fail(objectEntry.getKey() + " should not be in bundle"); | |
} catch (IllegalArgumentException e) { | |
// expected | |
} | |
assertFalse(objectEntry.getKey() + " should not be in bundle", bundle.containsKey(objectEntry.getKey())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment