From 4ef8702ce9391f2be717faa3d2f90c801a40b80e Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Wed, 28 Aug 2019 13:24:45 -0600 Subject: Fix subtle bug when using empty withValues(). We still need to populate mValues when the ContentValues was empty. Bug: 139356941 Test: atest --test-mapping packages/providers/ContactsProvider Change-Id: Ice90afbb7994e1a1d10076a2aa48b4d1187abe9f --- .../android/content/ContentProviderOperation.java | 29 ++++++++++++++++------ 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'core/java/android/content/ContentProviderOperation.java') diff --git a/core/java/android/content/ContentProviderOperation.java b/core/java/android/content/ContentProviderOperation.java index 621f331b52b2..5c2de57d77a5 100644 --- a/core/java/android/content/ContentProviderOperation.java +++ b/core/java/android/content/ContentProviderOperation.java @@ -684,10 +684,26 @@ public class ContentProviderOperation implements Parcelable { return new ContentProviderOperation(this); } - private void setValue(@NonNull String key, @NonNull Object value) { + private void ensureValues() { if (mValues == null) { mValues = new ArrayMap<>(); } + } + + private void ensureExtras() { + if (mExtras == null) { + mExtras = new ArrayMap<>(); + } + } + + private void ensureSelectionArgs() { + if (mSelectionArgs == null) { + mSelectionArgs = new SparseArray<>(); + } + } + + private void setValue(@NonNull String key, @NonNull Object value) { + ensureValues(); final boolean oldReference = mValues.get(key) instanceof BackReference; final boolean newReference = value instanceof BackReference; if (!oldReference || newReference) { @@ -696,9 +712,7 @@ public class ContentProviderOperation implements Parcelable { } private void setExtra(@NonNull String key, @NonNull Object value) { - if (mExtras == null) { - mExtras = new ArrayMap<>(); - } + ensureExtras(); final boolean oldReference = mExtras.get(key) instanceof BackReference; final boolean newReference = value instanceof BackReference; if (!oldReference || newReference) { @@ -707,9 +721,7 @@ public class ContentProviderOperation implements Parcelable { } private void setSelectionArg(int index, @NonNull Object value) { - if (mSelectionArgs == null) { - mSelectionArgs = new SparseArray<>(); - } + ensureSelectionArgs(); final boolean oldReference = mSelectionArgs.get(index) instanceof BackReference; final boolean newReference = value instanceof BackReference; if (!oldReference || newReference) { @@ -728,6 +740,7 @@ public class ContentProviderOperation implements Parcelable { */ public @NonNull Builder withValues(@NonNull ContentValues values) { assertValuesAllowed(); + ensureValues(); final ArrayMap rawValues = values.getValues(); for (int i = 0; i < rawValues.size(); i++) { setValue(rawValues.keyAt(i), rawValues.valueAt(i)); @@ -815,6 +828,7 @@ public class ContentProviderOperation implements Parcelable { */ public @NonNull Builder withExtras(@NonNull Bundle extras) { assertExtrasAllowed(); + ensureExtras(); for (String key : extras.keySet()) { setExtra(key, extras.get(key)); } @@ -885,6 +899,7 @@ public class ContentProviderOperation implements Parcelable { assertSelectionAllowed(); mSelection = selection; if (selectionArgs != null) { + ensureSelectionArgs(); for (int i = 0; i < selectionArgs.length; i++) { setSelectionArg(i, selectionArgs[i]); } -- cgit v1.2.3