From 08bbffb049c135c5dfd40d261118c90d1a6dc111 Mon Sep 17 00:00:00 2001 From: Bjorn Bringert Date: Thu, 25 Feb 2010 11:16:22 +0000 Subject: [PATCH] Support CharSequence lists+arrays in Bundle+Intent Fixes http://b/issue?id=2468093 Change-Id: Id82686f6ca8c9501f6db8a07018278a78ddacd05 --- api/current.xml | 112 ++++++++++++++++++++++++++++++++++ core/java/android/content/Intent.java | 74 ++++++++++++++++++++++ core/java/android/os/Bundle.java | 68 +++++++++++++++++++++ core/java/android/os/Parcel.java | 65 +++++++++++++++++++- 4 files changed, 317 insertions(+), 2 deletions(-) diff --git a/api/current.xml b/api/current.xml index 8a9a77a86501..7ab40e19b67b 100644 --- a/api/current.xml +++ b/api/current.xml @@ -36445,6 +36445,32 @@ + + + + + + + + + + + + + + + + + + + + @@ -112461,6 +112517,32 @@ + + + + + + + + + + + + + + + + + + + + value was found. + * + * @see #putCharSequenceArrayListExtra(String, ArrayList) + */ + public ArrayList getCharSequenceArrayListExtra(String name) { + return mExtras == null ? null : mExtras.getCharSequenceArrayList(name); + } + + /** + * Retrieve extended data from the intent. + * + * @param name The name of the desired item. + * + * @return the value of an item that previously added with putExtra() * or null if no boolean array value was found. * * @see #putExtra(String, boolean[]) @@ -3607,6 +3621,20 @@ public class Intent implements Parcelable, Cloneable { * @param name The name of the desired item. * * @return the value of an item that previously added with putExtra() + * or null if no CharSequence array value was found. + * + * @see #putExtra(String, CharSequence[]) + */ + public CharSequence[] getCharSequenceArrayExtra(String name) { + return mExtras == null ? null : mExtras.getCharSequenceArray(name); + } + + /** + * Retrieve extended data from the intent. + * + * @param name The name of the desired item. + * + * @return the value of an item that previously added with putExtra() * or null if no Bundle value was found. * * @see #putExtra(String, Bundle) @@ -4309,6 +4337,29 @@ public class Intent implements Parcelable, Cloneable { * like "com.android.contacts.ShowAll". * * @param name The name of the extra data, with package prefix. + * @param value The ArrayList data value. + * + * @return Returns the same Intent object, for chaining multiple calls + * into a single statement. + * + * @see #putExtras + * @see #removeExtra + * @see #getCharSequenceArrayListExtra(String) + */ + public Intent putCharSequenceArrayListExtra(String name, ArrayList value) { + if (mExtras == null) { + mExtras = new Bundle(); + } + mExtras.putCharSequenceArrayList(name, value); + return this; + } + + /** + * Add extended data to the intent. The name must include a package + * prefix, for example the app com.android.contacts would use names + * like "com.android.contacts.ShowAll". + * + * @param name The name of the extra data, with package prefix. * @param value The Serializable data value. * * @return Returns the same Intent object, for chaining multiple calls @@ -4539,6 +4590,29 @@ public class Intent implements Parcelable, Cloneable { * like "com.android.contacts.ShowAll". * * @param name The name of the extra data, with package prefix. + * @param value The CharSequence array data value. + * + * @return Returns the same Intent object, for chaining multiple calls + * into a single statement. + * + * @see #putExtras + * @see #removeExtra + * @see #getCharSequenceArrayExtra(String) + */ + public Intent putExtra(String name, CharSequence[] value) { + if (mExtras == null) { + mExtras = new Bundle(); + } + mExtras.putCharSequenceArray(name, value); + return this; + } + + /** + * Add extended data to the intent. The name must include a package + * prefix, for example the app com.android.contacts would use names + * like "com.android.contacts.ShowAll". + * + * @param name The name of the extra data, with package prefix. * @param value The Bundle data value. * * @return Returns the same Intent object, for chaining multiple calls diff --git a/core/java/android/os/Bundle.java b/core/java/android/os/Bundle.java index a91655f8e8e9..d6fe107b10b2 100644 --- a/core/java/android/os/Bundle.java +++ b/core/java/android/os/Bundle.java @@ -525,6 +525,18 @@ public final class Bundle implements Parcelable, Cloneable { } /** + * 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. + * + * @param key a String, or null + * @param value an ArrayList object, or null + */ + public void putCharSequenceArrayList(String key, ArrayList value) { + unparcel(); + mMap.put(key, 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. * @@ -645,6 +657,18 @@ public final class Bundle implements Parcelable, Cloneable { } /** + * 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. + * + * @param key a String, or null + * @param value a CharSequence array object, or null + */ + public void putCharSequenceArray(String key, CharSequence[] value) { + unparcel(); + mMap.put(key, value); + } + + /** * Inserts a Bundle value into the mapping of this Bundle, replacing * any existing value for the given key. Either key or value may be null. * @@ -1186,6 +1210,28 @@ public final class Bundle implements Parcelable, Cloneable { * value is explicitly associated with the key. * * @param key a String, or null + * @return an ArrayList value, or null + */ + public ArrayList getCharSequenceArrayList(String key) { + unparcel(); + Object o = mMap.get(key); + if (o == null) { + return null; + } + try { + return (ArrayList) o; + } catch (ClassCastException e) { + typeWarning(key, o, "ArrayList", e); + return null; + } + } + + /** + * 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. + * + * @param key a String, or null * @return a boolean[] value, or null */ public boolean[] getBooleanArray(String key) { @@ -1384,6 +1430,28 @@ public final class Bundle implements Parcelable, Cloneable { * value is explicitly associated with the key. * * @param key a String, or null + * @return a CharSequence[] value, or null + */ + public CharSequence[] getCharSequenceArray(String 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; + } + } + + /** + * 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. + * + * @param key a String, or null * @return an IBinder value, or null * * @deprecated diff --git a/core/java/android/os/Parcel.java b/core/java/android/os/Parcel.java index 6cfcceedfa46..8ad600c644f3 100644 --- a/core/java/android/os/Parcel.java +++ b/core/java/android/os/Parcel.java @@ -212,6 +212,7 @@ public final class Parcel { private static final int VAL_SERIALIZABLE = 21; private static final int VAL_SPARSEBOOLEANARRAY = 22; private static final int VAL_BOOLEANARRAY = 23; + private static final int VAL_CHARSEQUENCEARRAY = 24; private static final int EX_SECURITY = -1; private static final int EX_BAD_PARCELABLE = -2; @@ -411,6 +412,15 @@ public final class Parcel { public final native void writeString(String val); /** + * Write a CharSequence value into the parcel at the current dataPosition(), + * growing dataCapacity() if needed. + * @hide + */ + public final void writeCharSequence(CharSequence val) { + TextUtils.writeToParcel(val, this, 0); + } + + /** * Write an object into the parcel at the current dataPosition(), * growing dataCapacity() if needed. */ @@ -827,6 +837,21 @@ public final class Parcel { } } + /** + * @hide + */ + public final void writeCharSequenceArray(CharSequence[] val) { + if (val != null) { + int N = val.length; + writeInt(N); + for (int i=0; i= 0) { @@ -1045,7 +1070,7 @@ public final class Parcel { } else if (v instanceof CharSequence) { // Must be after String writeInt(VAL_CHARSEQUENCE); - TextUtils.writeToParcel((CharSequence) v, this, 0); + writeCharSequence((CharSequence) v); } else if (v instanceof List) { writeInt(VAL_LIST); writeList((List) v); @@ -1061,6 +1086,10 @@ public final class Parcel { } else if (v instanceof String[]) { writeInt(VAL_STRINGARRAY); writeStringArray((String[]) v); + } else if (v instanceof CharSequence[]) { + // Must be after String[] and before Object[] + writeInt(VAL_CHARSEQUENCEARRAY); + writeCharSequenceArray((CharSequence[]) v); } else if (v instanceof IBinder) { writeInt(VAL_IBINDER); writeStrongBinder((IBinder) v); @@ -1257,6 +1286,14 @@ public final class Parcel { public final native String readString(); /** + * Read a CharSequence value from the parcel at the current dataPosition(). + * @hide + */ + public final CharSequence readCharSequence() { + return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this); + } + + /** * Read an object from the parcel at the current dataPosition(). */ public final native IBinder readStrongBinder(); @@ -1389,6 +1426,27 @@ public final class Parcel { } /** + * Read and return a CharSequence[] object from the parcel. + * {@hide} + */ + public final CharSequence[] readCharSequenceArray() { + CharSequence[] array = null; + + int length = readInt(); + if (length >= 0) + { + array = new CharSequence[length]; + + for (int i = 0 ; i < length ; i++) + { + array[i] = readCharSequence(); + } + } + + return array; + } + + /** * Read and return a new ArrayList object from the parcel at the current * dataPosition(). Returns null if the previously written list object was * null. The given class loader will be used to load any enclosed @@ -1728,7 +1786,7 @@ public final class Parcel { return readInt() == 1; case VAL_CHARSEQUENCE: - return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this); + return readCharSequence(); case VAL_LIST: return readArrayList(loader); @@ -1742,6 +1800,9 @@ public final class Parcel { case VAL_STRINGARRAY: return readStringArray(); + case VAL_CHARSEQUENCEARRAY: + return readCharSequenceArray(); + case VAL_IBINDER: return readStrongBinder(); -- 2.11.0