diff options
| author | 2020-07-03 03:03:22 +0000 | |
|---|---|---|
| committer | 2020-07-03 03:03:22 +0000 | |
| commit | 63178d96bd477a582488681fe80d237acf8a0d0c (patch) | |
| tree | a2db3f76ec6cb0a6d22c15f8a55c24b1dbebf189 | |
| parent | 29cd04ede0313020571794f3b1d9bd047d4bd5e7 (diff) | |
| parent | 72217807c67e21f0364c1e4e1d3d5005a2ed36bd (diff) | |
Merge "Fix Bundle#getParcelableArray call" into rvc-dev am: d70c7dda50 am: 72217807c6
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/12034230
Change-Id: I771521370a581188f14bff35c7b04ba4b4c6c636
| -rw-r--r-- | core/java/android/app/Notification.java | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java index 0e3f35e358c0..79d2a8102358 100644 --- a/core/java/android/app/Notification.java +++ b/core/java/android/app/Notification.java @@ -96,9 +96,9 @@ import com.android.internal.util.ContrastColorUtil; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -1620,7 +1620,7 @@ public class Notification implements Parcelable * of non-textual RemoteInputs do not access these remote inputs. */ public RemoteInput[] getDataOnlyRemoteInputs() { - return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS); + return getParcelableArrayFromBundle(mExtras, EXTRA_DATA_ONLY_INPUTS, RemoteInput.class); } /** @@ -1802,8 +1802,8 @@ public class Notification implements Parcelable checkContextualActionNullFields(); ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>(); - RemoteInput[] previousDataInputs = - (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS); + RemoteInput[] previousDataInputs = getParcelableArrayFromBundle( + mExtras, EXTRA_DATA_ONLY_INPUTS, RemoteInput.class); if (previousDataInputs != null) { for (RemoteInput input : previousDataInputs) { dataOnlyInputs.add(input); @@ -5368,8 +5368,8 @@ public class Notification implements Parcelable big.setViewVisibility(R.id.actions_container, View.GONE); } - RemoteInputHistoryItem[] replyText = (RemoteInputHistoryItem[]) - mN.extras.getParcelableArray(EXTRA_REMOTE_INPUT_HISTORY_ITEMS); + RemoteInputHistoryItem[] replyText = getParcelableArrayFromBundle( + mN.extras, EXTRA_REMOTE_INPUT_HISTORY_ITEMS, RemoteInputHistoryItem.class); if (validRemoteInput && replyText != null && replyText.length > 0 && !TextUtils.isEmpty(replyText[0].getText()) && p.maxRemoteInputHistory > 0) { @@ -8155,8 +8155,9 @@ public class Notification implements Parcelable if (mBuilder.mActions.size() > 0) { maxRows--; } - RemoteInputHistoryItem[] remoteInputHistory = (RemoteInputHistoryItem[]) - mBuilder.mN.extras.getParcelableArray(EXTRA_REMOTE_INPUT_HISTORY_ITEMS); + RemoteInputHistoryItem[] remoteInputHistory = getParcelableArrayFromBundle( + mBuilder.mN.extras, EXTRA_REMOTE_INPUT_HISTORY_ITEMS, + RemoteInputHistoryItem.class); if (remoteInputHistory != null && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) { // Let's remove some messages to make room for the remote input history. @@ -9579,8 +9580,8 @@ public class Notification implements Parcelable mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS); mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT); - Notification[] pages = getNotificationArrayFromBundle( - wearableBundle, KEY_PAGES); + Notification[] pages = getParcelableArrayFromBundle( + wearableBundle, KEY_PAGES, Notification.class); if (pages != null) { Collections.addAll(mPages, pages); } @@ -10838,17 +10839,22 @@ public class Notification implements Parcelable } /** - * Get an array of Notification objects from a parcelable array bundle field. + * Get an array of Parcelable objects from a parcelable array bundle field. * Update the bundle to have a typed array so fetches in the future don't need * to do an array copy. */ - private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) { - Parcelable[] array = bundle.getParcelableArray(key); - if (array instanceof Notification[] || array == null) { - return (Notification[]) array; + @Nullable + private static <T extends Parcelable> T[] getParcelableArrayFromBundle( + Bundle bundle, String key, Class<T> itemClass) { + final Parcelable[] array = bundle.getParcelableArray(key); + final Class<?> arrayClass = Array.newInstance(itemClass, 0).getClass(); + if (arrayClass.isInstance(array) || array == null) { + return (T[]) array; + } + final T[] typedArray = (T[]) Array.newInstance(itemClass, array.length); + for (int i = 0; i < array.length; i++) { + typedArray[i] = (T) array[i]; } - Notification[] typedArray = Arrays.copyOf(array, array.length, - Notification[].class); bundle.putParcelableArray(key, typedArray); return typedArray; } |