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

Commit 8dea8744 authored by Jens Ole Lauridsen's avatar Jens Ole Lauridsen
Browse files

parcel: Add efficient methods for writing and reading a parcelable.

The documentation used to recommend calling Parcelable.writeToParcel
instead of using the Parcel API for sending Parcelable types.
This leaves the developer to have to deal with null values and makes
it harder to create tools that generate correct efficient code.
I suggest that we add a these 2 methods:
   writeTypedObject and createTypedObject
as an alternative.

Change-Id: I85443417909dcb9590d3f0a72f0130a4da4ead38
parent db720ce9
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -23294,6 +23294,7 @@ package android.os {
    method public final android.os.IBinder readStrongBinder();
    method public final void readTypedArray(T[], android.os.Parcelable.Creator<T>);
    method public final void readTypedList(java.util.List<T>, android.os.Parcelable.Creator<T>);
    method public final T readTypedObject(android.os.Parcelable.Creator<T>);
    method public final java.lang.Object readValue(java.lang.ClassLoader);
    method public final void recycle();
    method public final void setDataCapacity(int);
@@ -23338,6 +23339,7 @@ package android.os {
    method public final void writeStrongInterface(android.os.IInterface);
    method public final void writeTypedArray(T[], int);
    method public final void writeTypedList(java.util.List<T>);
    method public final void writeTypedObject(T, int);
    method public final void writeValue(java.lang.Object);
    field public static final android.os.Parcelable.Creator<java.lang.String> STRING_CREATOR;
  }
+2 −0
Original line number Diff line number Diff line
@@ -25182,6 +25182,7 @@ package android.os {
    method public final android.os.IBinder readStrongBinder();
    method public final void readTypedArray(T[], android.os.Parcelable.Creator<T>);
    method public final void readTypedList(java.util.List<T>, android.os.Parcelable.Creator<T>);
    method public final T readTypedObject(android.os.Parcelable.Creator<T>);
    method public final java.lang.Object readValue(java.lang.ClassLoader);
    method public final void recycle();
    method public final void setDataCapacity(int);
@@ -25226,6 +25227,7 @@ package android.os {
    method public final void writeStrongInterface(android.os.IInterface);
    method public final void writeTypedArray(T[], int);
    method public final void writeTypedList(java.util.List<T>);
    method public final void writeTypedObject(T, int);
    method public final void writeValue(java.lang.Object);
    field public static final android.os.Parcelable.Creator<java.lang.String> STRING_CREATOR;
  }
+42 −5
Original line number Diff line number Diff line
@@ -115,15 +115,15 @@ import java.util.Set;
 * later reading.</p>
 * 
 * <p>There are also some methods that provide a more efficient way to work
 * with Parcelables: {@link #writeTypedArray},
 * {@link #writeTypedList(List)},
 * {@link #readTypedArray} and {@link #readTypedList}.  These methods
 * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
 * {@link #writeTypedList}, {@link #readTypedObject},
 * {@link #createTypedArray} and {@link #createTypedArrayList}.  These methods
 * do not write the class information of the original object: instead, the
 * caller of the read function must know what type to expect and pass in the
 * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
 * properly construct the new object and read its data.  (To more efficient
 * write and read a single Parceable object, you can directly call
 * {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
 * write and read a single Parceable object that is not null, you can directly
 * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
 * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
 * yourself.)</p>
 * 
@@ -1222,6 +1222,24 @@ public final class Parcel {
        }
    }

    /**
     * Flatten the Parcelable object into the parcel.
     *
     * @param val The Parcelable object to be written.
     * @param parcelableFlags Contextual flags as per
     * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
     *
     * @see #readTypedObject
     */
    public final <T extends Parcelable> void writeTypedObject(T val, int parcelableFlags) {
        if (val != null) {
            writeInt(1);
            val.writeToParcel(this, parcelableFlags);
        } else {
            writeInt(0);
        }
    }

    /**
     * Flatten a generic object in to a parcel.  The given Object value may
     * currently be one of the following types:
@@ -2137,6 +2155,25 @@ public final class Parcel {
        return createTypedArray(c);
    }

    /**
     * Read and return a typed Parcelable object from a parcel.
     * Returns null if the previous written object was null.
     * The object <em>must</em> have previous been written via
     * {@link #writeTypedObject} with the same object type.
     *
     * @return A newly created object of the type that was previously
     *         written.
     *
     * @see #writeTypedObject
     */
    public final <T> T readTypedObject(Parcelable.Creator<T> c) {
        if (readInt() != 0) {
            return c.createFromParcel(this);
        } else {
            return null;
        }
    }

    /**
     * Write a heterogeneous array of Parcelable objects into the Parcel.
     * Each object in the array is written along with its class name, so