summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/api/system-current.txt2
-rw-r--r--core/java/android/os/ParcelableHolder.java30
2 files changed, 20 insertions, 12 deletions
diff --git a/core/api/system-current.txt b/core/api/system-current.txt
index d36bb9c60761..a48ad95ea40e 100644
--- a/core/api/system-current.txt
+++ b/core/api/system-current.txt
@@ -7286,7 +7286,7 @@ package android.os {
method @Nullable public <T extends android.os.Parcelable> T getParcelable(@NonNull Class<T>);
method public int getStability();
method public void readFromParcel(@NonNull android.os.Parcel);
- method public boolean setParcelable(@Nullable android.os.Parcelable);
+ method public void setParcelable(@Nullable android.os.Parcelable);
method public void writeToParcel(@NonNull android.os.Parcel, int);
field @NonNull public static final android.os.Parcelable.Creator<android.os.ParcelableHolder> CREATOR;
}
diff --git a/core/java/android/os/ParcelableHolder.java b/core/java/android/os/ParcelableHolder.java
index fb9fa109bfa3..1be367c7fca6 100644
--- a/core/java/android/os/ParcelableHolder.java
+++ b/core/java/android/os/ParcelableHolder.java
@@ -120,31 +120,37 @@ public final class ParcelableHolder implements Parcelable {
/**
* Write a parcelable into ParcelableHolder, the previous parcelable will be removed.
- * @return {@code false} if the parcelable's stability is more unstable ParcelableHolder.
+ * @throws BadParcelableException if the parcelable's stability is more unstable
+ * ParcelableHolder.
*/
- public boolean setParcelable(@Nullable Parcelable p) {
- // a ParcelableHolder can only hold things at its stability or higher
+ public void setParcelable(@Nullable Parcelable p) {
+ // A ParcelableHolder can only hold things at its stability or higher.
if (p != null && this.getStability() > p.getStability()) {
- return false;
+ throw new BadParcelableException(
+ "A ParcelableHolder can only hold things at its stability or higher. "
+ + "The ParcelableHolder's stability is " + this.getStability()
+ + ", but the parcelable's stability is " + p.getStability());
}
mParcelable = p;
if (mParcel != null) {
mParcel.recycle();
mParcel = null;
}
- return true;
}
/**
* @return the parcelable that was written by {@link #setParcelable} or {@link #readFromParcel},
- * or {@code null} if the parcelable has not been written, or T is different from
- * the type written by (@link #setParcelable}.
+ * or {@code null} if the parcelable has not been written.
+ * @throws BadParcelableException if T is different from the type written by
+ * (@link #setParcelable}.
*/
@Nullable
public <T extends Parcelable> T getParcelable(@NonNull Class<T> clazz) {
if (mParcel == null) {
- if (!clazz.isInstance(mParcelable)) {
- return null;
+ if (mParcelable != null && !clazz.isInstance(mParcelable)) {
+ throw new BadParcelableException(
+ "The ParcelableHolder has " + mParcelable.getClass().getName()
+ + ", but the requested type is " + clazz.getName());
}
return (T) mParcelable;
}
@@ -152,8 +158,10 @@ public final class ParcelableHolder implements Parcelable {
mParcel.setDataPosition(0);
T parcelable = mParcel.readParcelable(clazz.getClassLoader());
- if (!clazz.isInstance(parcelable)) {
- return null;
+ if (parcelable != null && !clazz.isInstance(parcelable)) {
+ throw new BadParcelableException(
+ "The ParcelableHolder has " + parcelable.getClass().getName()
+ + ", but the requested type is " + clazz.getName());
}
mParcelable = parcelable;