Merge "Adding typed Parcel read APIs 2" am: 79c9ea52e5

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1854703

Change-Id: I90f888eb7eb78f658881436ee6d6a9ff6cdfac65
This commit is contained in:
Hao Ke
2021-10-27 16:21:08 +00:00
committed by Automerger Merge Worker
2 changed files with 146 additions and 41 deletions

View File

@@ -31433,7 +31433,9 @@ package android.os {
method @NonNull public static android.os.Parcel obtain(); method @NonNull public static android.os.Parcel obtain();
method @NonNull public static android.os.Parcel obtain(@NonNull android.os.IBinder); method @NonNull public static android.os.Parcel obtain(@NonNull android.os.IBinder);
method @Nullable public Object[] readArray(@Nullable ClassLoader); method @Nullable public Object[] readArray(@Nullable ClassLoader);
method @Nullable public <T> T[] readArray(@Nullable ClassLoader, @NonNull Class<T>);
method @Nullable public java.util.ArrayList readArrayList(@Nullable ClassLoader); method @Nullable public java.util.ArrayList readArrayList(@Nullable ClassLoader);
method @Nullable public <T> java.util.ArrayList<T> readArrayList(@Nullable ClassLoader, @NonNull Class<? extends T>);
method public void readBinderArray(@NonNull android.os.IBinder[]); method public void readBinderArray(@NonNull android.os.IBinder[]);
method public void readBinderList(@NonNull java.util.List<android.os.IBinder>); method public void readBinderList(@NonNull java.util.List<android.os.IBinder>);
method public boolean readBoolean(); method public boolean readBoolean();
@@ -31461,6 +31463,7 @@ package android.os {
method @Nullable public <T extends android.os.Parcelable> T readParcelable(@Nullable ClassLoader); method @Nullable public <T extends android.os.Parcelable> T readParcelable(@Nullable ClassLoader);
method @Nullable public <T extends android.os.Parcelable> T readParcelable(@Nullable ClassLoader, @NonNull Class<T>); method @Nullable public <T extends android.os.Parcelable> T readParcelable(@Nullable ClassLoader, @NonNull Class<T>);
method @Nullable public android.os.Parcelable[] readParcelableArray(@Nullable ClassLoader); method @Nullable public android.os.Parcelable[] readParcelableArray(@Nullable ClassLoader);
method @Nullable public <T> T[] readParcelableArray(@Nullable ClassLoader, @NonNull Class<T>);
method @Nullable public android.os.Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader); method @Nullable public android.os.Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader);
method @Nullable public <T> android.os.Parcelable.Creator<T> readParcelableCreator(@Nullable ClassLoader, @NonNull Class<T>); method @Nullable public <T> android.os.Parcelable.Creator<T> readParcelableCreator(@Nullable ClassLoader, @NonNull Class<T>);
method @NonNull public <T extends android.os.Parcelable> java.util.List<T> readParcelableList(@NonNull java.util.List<T>, @Nullable ClassLoader); method @NonNull public <T extends android.os.Parcelable> java.util.List<T> readParcelableList(@NonNull java.util.List<T>, @Nullable ClassLoader);
@@ -31470,6 +31473,7 @@ package android.os {
method @NonNull public android.util.Size readSize(); method @NonNull public android.util.Size readSize();
method @NonNull public android.util.SizeF readSizeF(); method @NonNull public android.util.SizeF readSizeF();
method @Nullable public <T> android.util.SparseArray<T> readSparseArray(@Nullable ClassLoader); method @Nullable public <T> android.util.SparseArray<T> readSparseArray(@Nullable ClassLoader);
method @Nullable public <T> android.util.SparseArray<T> readSparseArray(@Nullable ClassLoader, @NonNull Class<? extends T>);
method @Nullable public android.util.SparseBooleanArray readSparseBooleanArray(); method @Nullable public android.util.SparseBooleanArray readSparseBooleanArray();
method @Nullable public String readString(); method @Nullable public String readString();
method public void readStringArray(@NonNull String[]); method public void readStringArray(@NonNull String[]);

View File

@@ -20,6 +20,7 @@ import static java.util.Objects.requireNonNull;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.TestApi; import android.annotation.TestApi;
import android.app.AppOpsManager; import android.app.AppOpsManager;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
@@ -3094,14 +3095,24 @@ public final class Parcel {
* Parcelables. * Parcelables.
*/ */
@Nullable @Nullable
public final ArrayList readArrayList(@Nullable ClassLoader loader) { public ArrayList readArrayList(@Nullable ClassLoader loader) {
int N = readInt(); return readArrayListInternal(loader, /* clazz */ null);
if (N < 0) { }
return null;
} /**
ArrayList l = new ArrayList(N); * Same as {@link #readArrayList(ClassLoader)} but accepts {@code clazz} parameter as
readListInternal(l, N, loader, /* clazz */ null); * the type required for each item.
return l; *
* @throws BadParcelableException Throws BadParcelableException if the item to be deserialized
* is not an instance of that class or any of its children classes or there was an error
* trying to instantiate an element.
*/
@SuppressLint({"ConcreteCollection", "NullableCollection"})
@Nullable
public <T> ArrayList<T> readArrayList(@Nullable ClassLoader loader,
@NonNull Class<? extends T> clazz) {
Objects.requireNonNull(clazz);
return readArrayListInternal(loader, clazz);
} }
/** /**
@@ -3111,14 +3122,23 @@ public final class Parcel {
* Parcelables. * Parcelables.
*/ */
@Nullable @Nullable
public final Object[] readArray(@Nullable ClassLoader loader) { public Object[] readArray(@Nullable ClassLoader loader) {
int N = readInt(); return readArrayInternal(loader, /* clazz */ null);
if (N < 0) { }
return null;
} /**
Object[] l = new Object[N]; * Same as {@link #readArray(ClassLoader)} but accepts {@code clazz} parameter as
readArrayInternal(l, N, loader); * the type required for each item.
return l; *
* @throws BadParcelableException Throws BadParcelableException if the item to be deserialized
* is not an instance of that class or any of its children classes or there was an error
* trying to instantiate an element.
*/
@SuppressLint({"ArrayReturn", "NullableCollection"})
@Nullable
public <T> T[] readArray(@Nullable ClassLoader loader, @NonNull Class<T> clazz) {
Objects.requireNonNull(clazz);
return readArrayInternal(loader, clazz);
} }
/** /**
@@ -3128,14 +3148,23 @@ public final class Parcel {
* Parcelables. * Parcelables.
*/ */
@Nullable @Nullable
public final <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) { public <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) {
int N = readInt(); return readSparseArrayInternal(loader, /* clazz */ null);
if (N < 0) { }
return null;
} /**
SparseArray sa = new SparseArray(N); * Same as {@link #readSparseArray(ClassLoader)} but accepts {@code clazz} parameter as
readSparseArrayInternal(sa, N, loader); * the type required for each item.
return sa; *
* @throws BadParcelableException Throws BadParcelableException if the item to be deserialized
* is not an instance of that class or any of its children classes or there was an error
* trying to instantiate an element.
*/
@Nullable
public <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader,
@NonNull Class<? extends T> clazz) {
Objects.requireNonNull(clazz);
return readSparseArrayInternal(loader, clazz);
} }
/** /**
@@ -3851,7 +3880,7 @@ public final class Parcel {
"Parcel " + this + ": Unmarshalling unknown type code " + type "Parcel " + this + ": Unmarshalling unknown type code " + type
+ " at offset " + off); + " at offset " + off);
} }
if (clazz != null && !clazz.isInstance(object)) { if (object != null && clazz != null && !clazz.isInstance(object)) {
throw new BadParcelableException("Unparcelled object " + object throw new BadParcelableException("Unparcelled object " + object
+ " is not an instance of required class " + clazz.getName() + " is not an instance of required class " + clazz.getName()
+ " provided in the parameter"); + " provided in the parameter");
@@ -3910,7 +3939,6 @@ public final class Parcel {
} }
/** /**
*
* @param clazz The type of the parcelable expected or {@code null} for performing no checks. * @param clazz The type of the parcelable expected or {@code null} for performing no checks.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -3969,7 +3997,7 @@ public final class Parcel {
* as the required type. * as the required type.
* *
* @throws BadParcelableException Throws BadParcelableException if the item to be deserialized * @throws BadParcelableException Throws BadParcelableException if the item to be deserialized
* is not an instance of that class or any of its children class or there there was an error * is not an instance of that class or any of its children classes or there there was an error
* trying to read the {@link Parcelable.Creator}. * trying to read the {@link Parcelable.Creator}.
*/ */
@Nullable @Nullable
@@ -4092,17 +4120,25 @@ public final class Parcel {
return p; return p;
} }
/** @hide */ /**
* Same as {@link #readParcelableArray(ClassLoader)} but accepts {@code clazz} parameter as
* the type required for each item.
*
* @throws BadParcelableException Throws BadParcelableException if the item to be deserialized
* is not an instance of that class or any of its children classes or there was an error
* trying to instantiate an element.
*/
@SuppressLint({"ArrayReturn", "NullableCollection"})
@SuppressWarnings("unchecked")
@Nullable @Nullable
public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader, public <T> T[] readParcelableArray(@Nullable ClassLoader loader, @NonNull Class<T> clazz) {
@NonNull Class<T> clazz) { int n = readInt();
int N = readInt(); if (n < 0) {
if (N < 0) {
return null; return null;
} }
T[] p = (T[]) Array.newInstance(clazz, N); T[] p = (T[]) Array.newInstance(clazz, n);
for (int i = 0; i < N; i++) { for (int i = 0; i < n; i++) {
p[i] = readParcelable(loader); p[i] = readParcelableInternal(loader, clazz);
} }
return p; return p;
} }
@@ -4320,9 +4356,12 @@ public final class Parcel {
return result; return result;
} }
private void readListInternal(@NonNull List outVal, int n, /**
@Nullable ClassLoader loader) { * The method is replaced by {@link #readListInternal(List, int, ClassLoader, Class)}, however
readListInternal(outVal, n, loader, null); * we are keeping this unused method here to allow unsupported app usages.
*/
private void readListInternal(@NonNull List outVal, int n, @Nullable ClassLoader loader) {
readListInternal(outVal, n, loader, /* clazz */ null);
} }
/** /**
@@ -4338,26 +4377,88 @@ public final class Parcel {
} }
} }
/**
* @param clazz The type of the object expected or {@code null} for performing no checks.
*/
@SuppressLint({"ConcreteCollection", "NullableCollection"})
@Nullable
private <T> ArrayList<T> readArrayListInternal(@Nullable ClassLoader loader,
@Nullable Class<? extends T> clazz) {
int n = readInt();
if (n < 0) {
return null;
}
ArrayList<T> l = new ArrayList<>(n);
readListInternal(l, n, loader, clazz);
return l;
}
/**
* The method is replaced by {@link #readArrayInternal(ClassLoader, Class)}, however
* we are keeping this unused method here to allow unsupported app usages.
*/
private void readArrayInternal(@NonNull Object[] outVal, int N, private void readArrayInternal(@NonNull Object[] outVal, int N,
@Nullable ClassLoader loader) { @Nullable ClassLoader loader) {
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++) {
Object value = readValue(loader); Object value = readValue(loader, /* clazz */ null);
//Log.d(TAG, "Unmarshalling value=" + value);
outVal[i] = value; outVal[i] = value;
} }
} }
/**
* @param clazz The type of the object expected or {@code null} for performing no checks.
*/
@SuppressWarnings("unchecked")
@Nullable
private <T> T[] readArrayInternal(@Nullable ClassLoader loader, @Nullable Class<T> clazz) {
int n = readInt();
if (n < 0) {
return null;
}
T[] outVal = (T[]) ((clazz == null) ? new Object[n] : Array.newInstance(clazz, n));
for (int i = 0; i < n; i++) {
T value = readValue(loader, clazz);
outVal[i] = value;
}
return outVal;
}
/**
* The method is replaced by {@link #readSparseArray(ClassLoader, Class)}, however
* we are keeping this unused method here to allow unsupported app usages.
*/
private void readSparseArrayInternal(@NonNull SparseArray outVal, int N, private void readSparseArrayInternal(@NonNull SparseArray outVal, int N,
@Nullable ClassLoader loader) { @Nullable ClassLoader loader) {
while (N > 0) { while (N > 0) {
int key = readInt(); int key = readInt();
Object value = readValue(loader); Object value = readValue(loader);
//Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
outVal.append(key, value); outVal.append(key, value);
N--; N--;
} }
} }
/**
* @param clazz The type of the object expected or {@code null} for performing no checks.
*/
@Nullable
private <T> SparseArray<T> readSparseArrayInternal(@Nullable ClassLoader loader,
@Nullable Class<? extends T> clazz) {
int n = readInt();
if (n < 0) {
return null;
}
SparseArray<T> outVal = new SparseArray<>(n);
while (n > 0) {
int key = readInt();
T value = readValue(loader, clazz);
outVal.append(key, value);
n--;
}
return outVal;
}
private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) { private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) {
while (N > 0) { while (N > 0) {