Merge "Throw BadParcelableException from set/getParcelable" am: 294dd8f1da am: c661b70d60
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1503133 Change-Id: I9cab7d61cfe9fcf6e551b6c225bd36fd222b1753
This commit is contained in:
@@ -7286,7 +7286,7 @@ package android.os {
|
|||||||
method @Nullable public <T extends android.os.Parcelable> T getParcelable(@NonNull Class<T>);
|
method @Nullable public <T extends android.os.Parcelable> T getParcelable(@NonNull Class<T>);
|
||||||
method public int getStability();
|
method public int getStability();
|
||||||
method public void readFromParcel(@NonNull android.os.Parcel);
|
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);
|
method public void writeToParcel(@NonNull android.os.Parcel, int);
|
||||||
field @NonNull public static final android.os.Parcelable.Creator<android.os.ParcelableHolder> CREATOR;
|
field @NonNull public static final android.os.Parcelable.Creator<android.os.ParcelableHolder> CREATOR;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,31 +120,37 @@ public final class ParcelableHolder implements Parcelable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a parcelable into ParcelableHolder, the previous parcelable will be removed.
|
* 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) {
|
public void setParcelable(@Nullable Parcelable p) {
|
||||||
// a ParcelableHolder can only hold things at its stability or higher
|
// A ParcelableHolder can only hold things at its stability or higher.
|
||||||
if (p != null && this.getStability() > p.getStability()) {
|
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;
|
mParcelable = p;
|
||||||
if (mParcel != null) {
|
if (mParcel != null) {
|
||||||
mParcel.recycle();
|
mParcel.recycle();
|
||||||
mParcel = null;
|
mParcel = null;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the parcelable that was written by {@link #setParcelable} or {@link #readFromParcel},
|
* @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
|
* or {@code null} if the parcelable has not been written.
|
||||||
* the type written by (@link #setParcelable}.
|
* @throws BadParcelableException if T is different from the type written by
|
||||||
|
* (@link #setParcelable}.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public <T extends Parcelable> T getParcelable(@NonNull Class<T> clazz) {
|
public <T extends Parcelable> T getParcelable(@NonNull Class<T> clazz) {
|
||||||
if (mParcel == null) {
|
if (mParcel == null) {
|
||||||
if (!clazz.isInstance(mParcelable)) {
|
if (mParcelable != null && !clazz.isInstance(mParcelable)) {
|
||||||
return null;
|
throw new BadParcelableException(
|
||||||
|
"The ParcelableHolder has " + mParcelable.getClass().getName()
|
||||||
|
+ ", but the requested type is " + clazz.getName());
|
||||||
}
|
}
|
||||||
return (T) mParcelable;
|
return (T) mParcelable;
|
||||||
}
|
}
|
||||||
@@ -152,8 +158,10 @@ public final class ParcelableHolder implements Parcelable {
|
|||||||
mParcel.setDataPosition(0);
|
mParcel.setDataPosition(0);
|
||||||
|
|
||||||
T parcelable = mParcel.readParcelable(clazz.getClassLoader());
|
T parcelable = mParcel.readParcelable(clazz.getClassLoader());
|
||||||
if (!clazz.isInstance(parcelable)) {
|
if (parcelable != null && !clazz.isInstance(parcelable)) {
|
||||||
return null;
|
throw new BadParcelableException(
|
||||||
|
"The ParcelableHolder has " + parcelable.getClass().getName()
|
||||||
|
+ ", but the requested type is " + clazz.getName());
|
||||||
}
|
}
|
||||||
mParcelable = parcelable;
|
mParcelable = parcelable;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user