From a89fede08c7c98bae3c82edc3eefbc9bae6e1de3 Mon Sep 17 00:00:00 2001 From: Bernardo Rufino Date: Mon, 14 Mar 2022 09:53:25 +0000 Subject: [PATCH] Add safer Intent extra APIs and deprecate old ones Complementing new safer Bundle APIs introduced in aosp/1988908. Test: Working on CTS CTS-Coverage-Bug: 224457848 Bug: 224457848 Change-Id: I1d2b15d1214f2bd2eccade45693365098200ae01 --- core/api/current.txt | 12 ++-- core/java/android/content/Intent.java | 82 +++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/core/api/current.txt b/core/api/current.txt index c178ccfdbe5b1..abb3870994704 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -9748,12 +9748,16 @@ package android.content { method @Nullable public long[] getLongArrayExtra(String); method public long getLongExtra(String, long); method @Nullable public String getPackage(); - method @Nullable public android.os.Parcelable[] getParcelableArrayExtra(String); - method @Nullable public java.util.ArrayList getParcelableArrayListExtra(String); - method @Nullable public T getParcelableExtra(String); + method @Deprecated @Nullable public android.os.Parcelable[] getParcelableArrayExtra(String); + method @Nullable public T[] getParcelableArrayExtra(@Nullable String, @NonNull Class); + method @Deprecated @Nullable public java.util.ArrayList getParcelableArrayListExtra(String); + method @Nullable public java.util.ArrayList getParcelableArrayListExtra(@Nullable String, @NonNull Class); + method @Deprecated @Nullable public T getParcelableExtra(String); + method @Nullable public T getParcelableExtra(@Nullable String, @NonNull Class); method @Nullable public String getScheme(); method @Nullable public android.content.Intent getSelector(); - method @Nullable public java.io.Serializable getSerializableExtra(String); + method @Deprecated @Nullable public java.io.Serializable getSerializableExtra(String); + method @Nullable public T getSerializableExtra(@Nullable String, @NonNull Class); method @Nullable public short[] getShortArrayExtra(String); method public short getShortExtra(String, short); method @Nullable public android.graphics.Rect getSourceBounds(); diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java index 8a5e097c2a738..22c838c2f7bbb 100644 --- a/core/java/android/content/Intent.java +++ b/core/java/android/content/Intent.java @@ -8581,12 +8581,31 @@ public class Intent implements Parcelable, Cloneable { * @return the value of an item previously added with putExtra(), * or null if no Parcelable value was found. * + * @deprecated Use the type-safer {@link #getParcelableExtra(String, Class)} starting from + * Android {@link Build.VERSION_CODES#TIRAMISU}. + * * @see #putExtra(String, Parcelable) */ + @Deprecated public @Nullable T getParcelableExtra(String name) { return mExtras == null ? null : mExtras.getParcelable(name); } + /** + * Retrieve extended data from the intent. + * + * @param name The name of the desired item. + * @param clazz The type of the object expected. + * + * @return the value of an item previously added with putExtra(), + * or null if no Parcelable value was found. + * + * @see #putExtra(String, Parcelable) + */ + public @Nullable T getParcelableExtra(@Nullable String name, @NonNull Class clazz) { + return mExtras == null ? null : mExtras.getParcelable(name, clazz); + } + /** * Retrieve extended data from the intent. * @@ -8595,12 +8614,33 @@ public class Intent implements Parcelable, Cloneable { * @return the value of an item previously added with putExtra(), * or null if no Parcelable[] value was found. * + * @deprecated Use the type-safer {@link #getParcelableArrayExtra(String, Class)} starting from + * Android {@link Build.VERSION_CODES#TIRAMISU}. + * * @see #putExtra(String, Parcelable[]) */ + @Deprecated public @Nullable Parcelable[] getParcelableArrayExtra(String name) { return mExtras == null ? null : mExtras.getParcelableArray(name); } + /** + * Retrieve extended data from the intent. + * + * @param name The name of the desired item. + * @param clazz The type of the items inside the array. This is only verified when unparceling. + * + * @return the value of an item previously added with putExtra(), + * or null if no Parcelable[] value was found. + * + * @see #putExtra(String, Parcelable[]) + */ + @SuppressLint({"ArrayReturn", "NullableCollection"}) + public @Nullable T[] getParcelableArrayExtra(@Nullable String name, + @NonNull Class clazz) { + return mExtras == null ? null : mExtras.getParcelableArray(name, clazz); + } + /** * Retrieve extended data from the intent. * @@ -8610,12 +8650,35 @@ public class Intent implements Parcelable, Cloneable { * putParcelableArrayListExtra(), or null if no * ArrayList value was found. * + * @deprecated Use the type-safer {@link #getParcelableArrayListExtra(String, Class)} starting + * from Android {@link Build.VERSION_CODES#TIRAMISU}. + * * @see #putParcelableArrayListExtra(String, ArrayList) */ + @Deprecated public @Nullable ArrayList getParcelableArrayListExtra(String name) { return mExtras == null ? null : mExtras.getParcelableArrayList(name); } + /** + * Retrieve extended data from the intent. + * + * @param name The name of the desired item. + * @param clazz The type of the items inside the array list. This is only verified when + * unparceling. + * + * @return the value of an item previously added with + * putParcelableArrayListExtra(), or null if no + * ArrayList value was found. + * + * @see #putParcelableArrayListExtra(String, ArrayList) + */ + @SuppressLint({"ConcreteCollection", "NullableCollection"}) + public @Nullable ArrayList getParcelableArrayListExtra(@Nullable String name, + @NonNull Class clazz) { + return mExtras == null ? null : mExtras.getParcelableArrayList(name, clazz); + } + /** * Retrieve extended data from the intent. * @@ -8624,12 +8687,31 @@ public class Intent implements Parcelable, Cloneable { * @return the value of an item previously added with putExtra(), * or null if no Serializable value was found. * + * @deprecated Use the type-safer {@link #getSerializableExtra(String, Class)} starting from + * Android {@link Build.VERSION_CODES#TIRAMISU}. + * * @see #putExtra(String, Serializable) */ public @Nullable Serializable getSerializableExtra(String name) { return mExtras == null ? null : mExtras.getSerializable(name); } + /** + * Retrieve extended data from the intent. + * + * @param name The name of the desired item. + * @param clazz The type of the object expected. + * + * @return the value of an item previously added with putExtra(), + * or null if no Serializable value was found. + * + * @see #putExtra(String, Serializable) + */ + public @Nullable T getSerializableExtra(@Nullable String name, + @NonNull Class clazz) { + return mExtras == null ? null : mExtras.getSerializable(name, clazz); + } + /** * Retrieve extended data from the intent. *