BaseBundlepublic class BaseBundle extends Object A mapping from String values to various types. |
Fields Summary |
---|
private static final String | TAG | static final boolean | DEBUG | static final int | BUNDLE_MAGIC | static final Parcel | EMPTY_PARCEL | android.util.ArrayMap | mMap | Parcel | mParcelledData | private ClassLoader | mClassLoaderThe ClassLoader used when unparcelling data from mParcelledData. |
Constructors Summary |
---|
BaseBundle(ClassLoader loader, int capacity)Constructs a new, empty Bundle that uses a specific ClassLoader for
instantiating Parcelable and Serializable objects.
mMap = capacity > 0 ?
new ArrayMap<String, Object>(capacity) : new ArrayMap<String, Object>();
mClassLoader = loader == null ? getClass().getClassLoader() : loader;
| BaseBundle()Constructs a new, empty Bundle.
this((ClassLoader) null, 0);
| BaseBundle(Parcel parcelledData)Constructs a Bundle whose data is stored as a Parcel. The data
will be unparcelled on first contact, using the assigned ClassLoader.
readFromParcelInner(parcelledData);
| BaseBundle(Parcel parcelledData, int length)
readFromParcelInner(parcelledData, length);
| BaseBundle(ClassLoader loader)Constructs a new, empty Bundle that uses a specific ClassLoader for
instantiating Parcelable and Serializable objects.
this(loader, 0);
| BaseBundle(int capacity)Constructs a new, empty Bundle sized to hold the given number of
elements. The Bundle will grow as needed.
this((ClassLoader) null, capacity);
| BaseBundle(BaseBundle b)Constructs a Bundle containing a copy of the mappings from the given
Bundle.
if (b.mParcelledData != null) {
if (b.mParcelledData == EMPTY_PARCEL) {
mParcelledData = EMPTY_PARCEL;
} else {
mParcelledData = Parcel.obtain();
mParcelledData.appendFrom(b.mParcelledData, 0, b.mParcelledData.dataSize());
mParcelledData.setDataPosition(0);
}
} else {
mParcelledData = null;
}
if (b.mMap != null) {
mMap = new ArrayMap<String, Object>(b.mMap);
} else {
mMap = null;
}
mClassLoader = b.mClassLoader;
|
Methods Summary |
---|
public void | clear()Removes all elements from the mapping of this Bundle.
unparcel();
mMap.clear();
| public boolean | containsKey(java.lang.String key)Returns true if the given key is contained in the mapping
of this Bundle.
unparcel();
return mMap.containsKey(key);
| public java.lang.Object | get(java.lang.String key)Returns the entry with the given key as an object.
unparcel();
return mMap.get(key);
| public boolean | getBoolean(java.lang.String key)Returns the value associated with the given key, or false if
no mapping of the desired type exists for the given key.
unparcel();
if (DEBUG) Log.d(TAG, "Getting boolean in "
+ Integer.toHexString(System.identityHashCode(this)));
return getBoolean(key, false);
| public boolean | getBoolean(java.lang.String key, boolean defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return defaultValue;
}
try {
return (Boolean) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Boolean", defaultValue, e);
return defaultValue;
}
| public boolean[] | getBooleanArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (boolean[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "byte[]", e);
return null;
}
| byte | getByte(java.lang.String key)Returns the value associated with the given key, or (byte) 0 if
no mapping of the desired type exists for the given key.
unparcel();
return getByte(key, (byte) 0);
| java.lang.Byte | getByte(java.lang.String key, byte defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return defaultValue;
}
try {
return (Byte) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Byte", defaultValue, e);
return defaultValue;
}
| byte[] | getByteArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (byte[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "byte[]", e);
return null;
}
| char | getChar(java.lang.String key)Returns the value associated with the given key, or (char) 0 if
no mapping of the desired type exists for the given key.
unparcel();
return getChar(key, (char) 0);
| char | getChar(java.lang.String key, char defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return defaultValue;
}
try {
return (Character) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Character", defaultValue, e);
return defaultValue;
}
| char[] | getCharArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (char[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "char[]", e);
return null;
}
| java.lang.CharSequence | getCharSequence(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
final Object o = mMap.get(key);
try {
return (CharSequence) o;
} catch (ClassCastException e) {
typeWarning(key, o, "CharSequence", e);
return null;
}
| java.lang.CharSequence | getCharSequence(java.lang.String key, java.lang.CharSequence defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key or if a null
value is explicitly associated with the given key.
final CharSequence cs = getCharSequence(key);
return (cs == null) ? defaultValue : cs;
| java.lang.CharSequence[] | getCharSequenceArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (CharSequence[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "CharSequence[]", e);
return null;
}
| java.util.ArrayList | getCharSequenceArrayList(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (ArrayList<CharSequence>) o;
} catch (ClassCastException e) {
typeWarning(key, o, "ArrayList<CharSequence>", e);
return null;
}
| java.lang.ClassLoader | getClassLoader()Return the ClassLoader currently associated with this Bundle.
return mClassLoader;
| public double | getDouble(java.lang.String key)Returns the value associated with the given key, or 0.0 if
no mapping of the desired type exists for the given key.
unparcel();
return getDouble(key, 0.0);
| public double | getDouble(java.lang.String key, double defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return defaultValue;
}
try {
return (Double) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Double", defaultValue, e);
return defaultValue;
}
| public double[] | getDoubleArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (double[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "double[]", e);
return null;
}
| float | getFloat(java.lang.String key)Returns the value associated with the given key, or 0.0f if
no mapping of the desired type exists for the given key.
unparcel();
return getFloat(key, 0.0f);
| float | getFloat(java.lang.String key, float defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return defaultValue;
}
try {
return (Float) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Float", defaultValue, e);
return defaultValue;
}
| float[] | getFloatArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (float[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "float[]", e);
return null;
}
| public int | getInt(java.lang.String key)Returns the value associated with the given key, or 0 if
no mapping of the desired type exists for the given key.
unparcel();
return getInt(key, 0);
| public int | getInt(java.lang.String key, int defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return defaultValue;
}
try {
return (Integer) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Integer", defaultValue, e);
return defaultValue;
}
| public int[] | getIntArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (int[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "int[]", e);
return null;
}
| java.util.ArrayList | getIntegerArrayList(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (ArrayList<Integer>) o;
} catch (ClassCastException e) {
typeWarning(key, o, "ArrayList<Integer>", e);
return null;
}
| public long | getLong(java.lang.String key)Returns the value associated with the given key, or 0L if
no mapping of the desired type exists for the given key.
unparcel();
return getLong(key, 0L);
| public long | getLong(java.lang.String key, long defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return defaultValue;
}
try {
return (Long) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Long", defaultValue, e);
return defaultValue;
}
| public long[] | getLongArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (long[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "long[]", e);
return null;
}
| public java.lang.String | getPairValue()TODO: optimize this later (getting just the value part of a Bundle
with a single pair) once Bundle.forPair() above is implemented
with a special single-value Map implementation/serialization.
Note: value in single-pair Bundle may be null.
unparcel();
int size = mMap.size();
if (size > 1) {
Log.w(TAG, "getPairValue() used on Bundle with multiple pairs.");
}
if (size == 0) {
return null;
}
Object o = mMap.valueAt(0);
try {
return (String) o;
} catch (ClassCastException e) {
typeWarning("getPairValue()", o, "String", e);
return null;
}
| java.io.Serializable | getSerializable(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (Serializable) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Serializable", e);
return null;
}
| short | getShort(java.lang.String key)Returns the value associated with the given key, or (short) 0 if
no mapping of the desired type exists for the given key.
unparcel();
return getShort(key, (short) 0);
| short | getShort(java.lang.String key, short defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return defaultValue;
}
try {
return (Short) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Short", defaultValue, e);
return defaultValue;
}
| short[] | getShortArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (short[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "short[]", e);
return null;
}
| public java.lang.String | getString(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
final Object o = mMap.get(key);
try {
return (String) o;
} catch (ClassCastException e) {
typeWarning(key, o, "String", e);
return null;
}
| public java.lang.String | getString(java.lang.String key, java.lang.String defaultValue)Returns the value associated with the given key, or defaultValue if
no mapping of the desired type exists for the given key or if a null
value is explicitly associated with the given key.
final String s = getString(key);
return (s == null) ? defaultValue : s;
| public java.lang.String[] | getStringArray(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (String[]) o;
} catch (ClassCastException e) {
typeWarning(key, o, "String[]", e);
return null;
}
| java.util.ArrayList | getStringArrayList(java.lang.String key)Returns the value associated with the given key, or null if
no mapping of the desired type exists for the given key or a null
value is explicitly associated with the key.
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (ArrayList<String>) o;
} catch (ClassCastException e) {
typeWarning(key, o, "ArrayList<String>", e);
return null;
}
| public boolean | isEmpty()Returns true if the mapping of this Bundle is empty, false otherwise.
unparcel();
return mMap.isEmpty();
| public boolean | isParcelled()
return mParcelledData != null;
| public java.util.Set | keySet()Returns a Set containing the Strings used as keys in this Bundle.
unparcel();
return mMap.keySet();
| public void | putAll(PersistableBundle bundle)Inserts all mappings from the given PersistableBundle into this BaseBundle.
unparcel();
bundle.unparcel();
mMap.putAll(bundle.mMap);
| void | putAll(java.util.Map map)Inserts all mappings from the given Map into this BaseBundle.
unparcel();
mMap.putAll(map);
| public void | putBoolean(java.lang.String key, boolean value)Inserts a Boolean value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| public void | putBooleanArray(java.lang.String key, boolean[] value)Inserts a boolean array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putByte(java.lang.String key, byte value)Inserts a byte value into the mapping of this Bundle, replacing
any existing value for the given key.
unparcel();
mMap.put(key, value);
| void | putByteArray(java.lang.String key, byte[] value)Inserts a byte array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putChar(java.lang.String key, char value)Inserts a char value into the mapping of this Bundle, replacing
any existing value for the given key.
unparcel();
mMap.put(key, value);
| void | putCharArray(java.lang.String key, char[] value)Inserts a char array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putCharSequence(java.lang.String key, java.lang.CharSequence value)Inserts a CharSequence value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putCharSequenceArray(java.lang.String key, java.lang.CharSequence[] value)Inserts a CharSequence array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putCharSequenceArrayList(java.lang.String key, java.util.ArrayList value)Inserts an ArrayList value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| public void | putDouble(java.lang.String key, double value)Inserts a double value into the mapping of this Bundle, replacing
any existing value for the given key.
unparcel();
mMap.put(key, value);
| public void | putDoubleArray(java.lang.String key, double[] value)Inserts a double array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putFloat(java.lang.String key, float value)Inserts a float value into the mapping of this Bundle, replacing
any existing value for the given key.
unparcel();
mMap.put(key, value);
| void | putFloatArray(java.lang.String key, float[] value)Inserts a float array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| public void | putInt(java.lang.String key, int value)Inserts an int value into the mapping of this Bundle, replacing
any existing value for the given key.
unparcel();
mMap.put(key, value);
| public void | putIntArray(java.lang.String key, int[] value)Inserts an int array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putIntegerArrayList(java.lang.String key, java.util.ArrayList value)Inserts an ArrayList value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| public void | putLong(java.lang.String key, long value)Inserts a long value into the mapping of this Bundle, replacing
any existing value for the given key.
unparcel();
mMap.put(key, value);
| public void | putLongArray(java.lang.String key, long[] value)Inserts a long array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putSerializable(java.lang.String key, java.io.Serializable value)Inserts a Serializable value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putShort(java.lang.String key, short value)Inserts a short value into the mapping of this Bundle, replacing
any existing value for the given key.
unparcel();
mMap.put(key, value);
| void | putShortArray(java.lang.String key, short[] value)Inserts a short array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| public void | putString(java.lang.String key, java.lang.String value)Inserts a String value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| public void | putStringArray(java.lang.String key, java.lang.String[] value)Inserts a String array value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | putStringArrayList(java.lang.String key, java.util.ArrayList value)Inserts an ArrayList value into the mapping of this Bundle, replacing
any existing value for the given key. Either key or value may be null.
unparcel();
mMap.put(key, value);
| void | readFromParcelInner(Parcel parcel)Reads the Parcel contents into this Bundle, typically in order for
it to be passed through an IBinder connection.
int length = parcel.readInt();
if (length < 0) {
throw new RuntimeException("Bad length in parcel: " + length);
}
readFromParcelInner(parcel, length);
| private void | readFromParcelInner(Parcel parcel, int length)
if (length == 0) {
// Empty Bundle or end of data.
mParcelledData = EMPTY_PARCEL;
return;
}
int magic = parcel.readInt();
if (magic != BUNDLE_MAGIC) {
//noinspection ThrowableInstanceNeverThrown
throw new IllegalStateException("Bad magic number for Bundle: 0x"
+ Integer.toHexString(magic));
}
// Advance within this Parcel
int offset = parcel.dataPosition();
parcel.setDataPosition(offset + length);
Parcel p = Parcel.obtain();
p.setDataPosition(0);
p.appendFrom(parcel, offset, length);
if (DEBUG) Log.d(TAG, "Retrieving " + Integer.toHexString(System.identityHashCode(this))
+ ": " + length + " bundle bytes starting at " + offset);
p.setDataPosition(0);
mParcelledData = p;
| public void | remove(java.lang.String key)Removes any entry with the given key from the mapping of this Bundle.
unparcel();
mMap.remove(key);
| void | setClassLoader(java.lang.ClassLoader loader)Changes the ClassLoader this Bundle uses when instantiating objects.
mClassLoader = loader;
| public int | size()Returns the number of mappings contained in this Bundle.
unparcel();
return mMap.size();
| void | typeWarning(java.lang.String key, java.lang.Object value, java.lang.String className, java.lang.Object defaultValue, java.lang.ClassCastException e)
StringBuilder sb = new StringBuilder();
sb.append("Key ");
sb.append(key);
sb.append(" expected ");
sb.append(className);
sb.append(" but value was a ");
sb.append(value.getClass().getName());
sb.append(". The default value ");
sb.append(defaultValue);
sb.append(" was returned.");
Log.w(TAG, sb.toString());
Log.w(TAG, "Attempt to cast generated internal exception:", e);
| void | typeWarning(java.lang.String key, java.lang.Object value, java.lang.String className, java.lang.ClassCastException e)
typeWarning(key, value, className, "<null>", e);
| synchronized void | unparcel()If the underlying data are stored as a Parcel, unparcel them
using the currently assigned class loader.
if (mParcelledData == null) {
if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ ": no parcelled data");
return;
}
if (mParcelledData == EMPTY_PARCEL) {
if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ ": empty");
if (mMap == null) {
mMap = new ArrayMap<String, Object>(1);
} else {
mMap.erase();
}
mParcelledData = null;
return;
}
int N = mParcelledData.readInt();
if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ ": reading " + N + " maps");
if (N < 0) {
return;
}
if (mMap == null) {
mMap = new ArrayMap<String, Object>(N);
} else {
mMap.erase();
mMap.ensureCapacity(N);
}
mParcelledData.readArrayMapInternal(mMap, N, mClassLoader);
mParcelledData.recycle();
mParcelledData = null;
if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ " final map: " + mMap);
| void | writeToParcelInner(Parcel parcel, int flags)Writes the Bundle contents to a Parcel, typically in order for
it to be passed through an IBinder connection.
if (mParcelledData != null) {
if (mParcelledData == EMPTY_PARCEL) {
parcel.writeInt(0);
} else {
int length = mParcelledData.dataSize();
parcel.writeInt(length);
parcel.writeInt(BUNDLE_MAGIC);
parcel.appendFrom(mParcelledData, 0, length);
}
} else {
// Special case for empty bundles.
if (mMap == null || mMap.size() <= 0) {
parcel.writeInt(0);
return;
}
int lengthPos = parcel.dataPosition();
parcel.writeInt(-1); // dummy, will hold length
parcel.writeInt(BUNDLE_MAGIC);
int startPos = parcel.dataPosition();
parcel.writeArrayMapInternal(mMap);
int endPos = parcel.dataPosition();
// Backpatch length
parcel.setDataPosition(lengthPos);
int length = endPos - startPos;
parcel.writeInt(length);
parcel.setDataPosition(endPos);
}
|
|