From 050781e2d80af37a634b6d2eeb1030850cd0cd3d Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Sat, 31 Oct 2026 04:08:40 +0000 Subject: [PATCH 1/2] Give android.os.Bundle the Android class's semantics and the members Firebase uses The shim held a map behind four getters whose behaviour differed from Android's: containsKey threw for a missing key, and a getter threw a ClassCastException for a value of another type. It now mirrors android.os.Bundle for the primitive, String and Bundle values the ported libraries and firebase-kotlin-sdk's Analytics use: a no-argument constructor, the put methods for boolean, int, long, float, double, String and Bundle, the matching getters with and without a default, get, containsKey answering rather than throwing, putAll, remove, clear, size and isEmpty, with a getter returning its default when the key is missing or the value has another type, as on Android. The Map constructor stays for ServiceInfo's metadata. --- src/main/java/android/os/Bundle.java | 128 ++++++++++++++++++++++++--- src/test/kotlin/BundleTest.kt | 75 ++++++++++++++++ 2 files changed, 192 insertions(+), 11 deletions(-) create mode 100644 src/test/kotlin/BundleTest.kt diff --git a/src/main/java/android/os/Bundle.java b/src/main/java/android/os/Bundle.java index f18db78..a675f32 100644 --- a/src/main/java/android/os/Bundle.java +++ b/src/main/java/android/os/Bundle.java @@ -1,46 +1,152 @@ package android.os; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; +/** + * A mapping from String keys to values, with the Android class's semantics for the primitive, String and Bundle values + * the ported Firebase libraries use: a getter returns its default when the key is missing or the value has another + * type, {@link #containsKey} answers rather than throws, and keys may be null. + */ public class Bundle { private final Map data; + public Bundle() { + this.data = new LinkedHashMap<>(); + } + public Bundle(Map data) { this.data = data; } public Bundle(Bundle bundle) { - this.data = new HashMap<>(bundle.data); + this.data = new LinkedHashMap<>(bundle.data); + } + + public int size() { + return data.size(); + } + + public boolean isEmpty() { + return data.isEmpty(); + } + + public void clear() { + data.clear(); } public boolean containsKey(String key) { - if(data.containsKey(key)) return true; - throw new IllegalArgumentException(key); + return data.containsKey(key); + } + + public Object get(String key) { + return data.get(key); + } + + public void remove(String key) { + data.remove(key); + } + + public void putAll(Bundle bundle) { + data.putAll(bundle.data); } public Set keySet() { return data.keySet(); } + public void putBoolean(String key, boolean value) { + data.put(key, value); + } + + public void putInt(String key, int value) { + data.put(key, value); + } + + public void putLong(String key, long value) { + data.put(key, value); + } + + public void putFloat(String key, float value) { + data.put(key, value); + } + + public void putDouble(String key, double value) { + data.put(key, value); + } + + public void putString(String key, String value) { + data.put(key, value); + } + + public void putBundle(String key, Bundle value) { + data.put(key, value); + } + public boolean getBoolean(String key) { + return getBoolean(key, false); + } - return (Boolean)data.get(key); + public boolean getBoolean(String key, boolean defaultValue) { + return get(key, Boolean.class, defaultValue); } - public Object get(String key) { - containsKey(key); - return data.get(key); + public int getInt(String key) { + return getInt(key, 0); + } + + public int getInt(String key, int defaultValue) { + return get(key, Integer.class, defaultValue); + } + + public long getLong(String key) { + return getLong(key, 0L); + } + + public long getLong(String key, long defaultValue) { + return get(key, Long.class, defaultValue); + } + + public float getFloat(String key) { + return getFloat(key, 0.0f); + } + + public float getFloat(String key, float defaultValue) { + return get(key, Float.class, defaultValue); + } + + public double getDouble(String key) { + return getDouble(key, 0.0); + } + + public double getDouble(String key, double defaultValue) { + return get(key, Double.class, defaultValue); } public String getString(String key) { - containsKey(key); - return (String)data.get(key); + return get(key, String.class, null); } - public int getInt(String key) { - return (Integer)data.get(key); + /** Returns {@code defaultValue} when the key is missing, the value is null or it is not a String. */ + public String getString(String key, String defaultValue) { + String value = getString(key); + return value == null ? defaultValue : value; + } + + public Bundle getBundle(String key) { + return get(key, Bundle.class, null); + } + + private T get(String key, Class type, T defaultValue) { + Object value = data.get(key); + return type.isInstance(value) ? type.cast(value) : defaultValue; + } + + @Override + public String toString() { + return "Bundle[" + data + "]"; } } diff --git a/src/test/kotlin/BundleTest.kt b/src/test/kotlin/BundleTest.kt new file mode 100644 index 0000000..dfff989 --- /dev/null +++ b/src/test/kotlin/BundleTest.kt @@ -0,0 +1,75 @@ +/* + * Tests the android.os.Bundle shim against the Android class's semantics for the members the ported + * Firebase libraries and firebase-kotlin-sdk's Analytics use. + */ +import android.os.Bundle +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +class BundleTest { + + @Test + fun `stores and reads typed values`() { + val bundle = Bundle() + bundle.putString("string", "value") + bundle.putInt("int", 42) + bundle.putLong("long", 1L shl 40) + bundle.putDouble("double", 42.5) + bundle.putFloat("float", 1.5f) + bundle.putBoolean("boolean", true) + bundle.putBundle("bundle", Bundle().apply { putString("nested", "x") }) + + assertEquals("value", bundle.getString("string")) + assertEquals(42, bundle.getInt("int")) + assertEquals(1L shl 40, bundle.getLong("long")) + assertEquals(42.5, bundle.getDouble("double"), 0.0) + assertEquals(1.5f, bundle.getFloat("float"), 0.0f) + assertTrue(bundle.getBoolean("boolean")) + assertEquals("x", bundle.getBundle("bundle")?.getString("nested")) + assertEquals(setOf("string", "int", "long", "double", "float", "boolean", "bundle"), bundle.keySet()) + assertEquals(7, bundle.size()) + assertFalse(bundle.isEmpty()) + } + + @Test + fun `returns the default for a missing key or another type`() { + val bundle = Bundle() + bundle.putString("string", "value") + bundle.putString("null", null) + + assertFalse(bundle.containsKey("missing")) + assertTrue(bundle.containsKey("null")) + assertNull(bundle.get("missing")) + assertNull(bundle.getString("missing")) + assertEquals("default", bundle.getString("missing", "default")) + assertEquals("default", bundle.getString("null", "default")) + assertEquals(0, bundle.getInt("missing")) + assertEquals(7, bundle.getInt("missing", 7)) + assertEquals(0, bundle.getInt("string")) + assertEquals(0L, bundle.getLong("string")) + assertEquals(0.0, bundle.getDouble("string"), 0.0) + assertFalse(bundle.getBoolean("string")) + assertTrue(bundle.getBoolean("missing", true)) + assertNull(bundle.getBundle("string")) + } + + @Test + fun `copies, merges, removes and clears`() { + val source = Bundle(mapOf("a" to 1, "b" to "two")) + val copy = Bundle(source) + copy.putInt("c", 3) + assertEquals(2, source.size()) + assertEquals(3, copy.size()) + + val target = Bundle() + target.putAll(copy) + assertEquals("two", target.getString("b")) + target.remove("b") + assertFalse(target.containsKey("b")) + target.clear() + assertTrue(target.isEmpty()) + } +} From 468db019be7270b397783c7d333cb1dbfb45a298 Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Sat, 31 Oct 2026 04:36:12 +0000 Subject: [PATCH 2/2] Copy the map given to Bundle(Map) so the bundle stays mutable --- src/main/java/android/os/Bundle.java | 3 ++- src/test/kotlin/BundleTest.kt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/android/os/Bundle.java b/src/main/java/android/os/Bundle.java index a675f32..406ae48 100644 --- a/src/main/java/android/os/Bundle.java +++ b/src/main/java/android/os/Bundle.java @@ -18,8 +18,9 @@ public Bundle() { this.data = new LinkedHashMap<>(); } + /** A bundle holding a copy of {@code data}, so that it stays mutable whatever map it is created from. */ public Bundle(Map data) { - this.data = data; + this.data = new LinkedHashMap<>(data); } public Bundle(Bundle bundle) { diff --git a/src/test/kotlin/BundleTest.kt b/src/test/kotlin/BundleTest.kt index dfff989..0504c50 100644 --- a/src/test/kotlin/BundleTest.kt +++ b/src/test/kotlin/BundleTest.kt @@ -58,7 +58,8 @@ class BundleTest { @Test fun `copies, merges, removes and clears`() { - val source = Bundle(mapOf("a" to 1, "b" to "two")) + val source = Bundle(java.util.Collections.singletonMap("a", 1)) + source.putString("b", "two") // the map constructor copies, so an immutable map does not make the bundle immutable val copy = Bundle(source) copy.putInt("c", 3) assertEquals(2, source.size())