Merge "Allow failures of applyBatch() operations." into qt-dev

This commit is contained in:
Jeff Sharkey
2019-04-16 03:26:18 +00:00
committed by Android (Google) Code Review
2 changed files with 89 additions and 19 deletions

View File

@@ -17,7 +17,6 @@
package android.content; package android.content;
import android.annotation.UnsupportedAppUsage; import android.annotation.UnsupportedAppUsage;
import android.content.ContentProvider;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Parcel; import android.os.Parcel;
@@ -59,6 +58,7 @@ public class ContentProviderOperation implements Parcelable {
private final ContentValues mValuesBackReferences; private final ContentValues mValuesBackReferences;
private final Map<Integer, Integer> mSelectionArgsBackReferences; private final Map<Integer, Integer> mSelectionArgsBackReferences;
private final boolean mYieldAllowed; private final boolean mYieldAllowed;
private final boolean mFailureAllowed;
private final static String TAG = "ContentProviderOperation"; private final static String TAG = "ContentProviderOperation";
@@ -76,6 +76,7 @@ public class ContentProviderOperation implements Parcelable {
mSelectionArgsBackReferences = builder.mSelectionArgsBackReferences; mSelectionArgsBackReferences = builder.mSelectionArgsBackReferences;
mValuesBackReferences = builder.mValuesBackReferences; mValuesBackReferences = builder.mValuesBackReferences;
mYieldAllowed = builder.mYieldAllowed; mYieldAllowed = builder.mYieldAllowed;
mFailureAllowed = builder.mFailureAllowed;
} }
private ContentProviderOperation(Parcel source) { private ContentProviderOperation(Parcel source) {
@@ -98,6 +99,7 @@ public class ContentProviderOperation implements Parcelable {
} }
} }
mYieldAllowed = source.readInt() != 0; mYieldAllowed = source.readInt() != 0;
mFailureAllowed = source.readInt() != 0;
} }
/** @hide */ /** @hide */
@@ -111,6 +113,7 @@ public class ContentProviderOperation implements Parcelable {
mSelectionArgsBackReferences = cpo.mSelectionArgsBackReferences; mSelectionArgsBackReferences = cpo.mSelectionArgsBackReferences;
mValuesBackReferences = cpo.mValuesBackReferences; mValuesBackReferences = cpo.mValuesBackReferences;
mYieldAllowed = cpo.mYieldAllowed; mYieldAllowed = cpo.mYieldAllowed;
mFailureAllowed = cpo.mFailureAllowed;
} }
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
@@ -157,6 +160,7 @@ public class ContentProviderOperation implements Parcelable {
dest.writeInt(0); dest.writeInt(0);
} }
dest.writeInt(mYieldAllowed ? 1 : 0); dest.writeInt(mYieldAllowed ? 1 : 0);
dest.writeInt(mFailureAllowed ? 1 : 0);
} }
/** /**
@@ -212,6 +216,11 @@ public class ContentProviderOperation implements Parcelable {
return mYieldAllowed; return mYieldAllowed;
} }
/** {@hide} */
public boolean isFailureAllowed() {
return mFailureAllowed;
}
/** @hide exposed for unit tests */ /** @hide exposed for unit tests */
@UnsupportedAppUsage @UnsupportedAppUsage
public int getType() { public int getType() {
@@ -274,6 +283,14 @@ public class ContentProviderOperation implements Parcelable {
return mType == TYPE_ASSERT; return mType == TYPE_ASSERT;
} }
private ContentProviderResult fail(String msg) throws OperationApplicationException {
if (mFailureAllowed) {
return new ContentProviderResult(msg);
} else {
throw new OperationApplicationException(msg);
}
}
/** /**
* Applies this operation using the given provider. The backRefs array is used to resolve any * Applies this operation using the given provider. The backRefs array is used to resolve any
* back references that were requested using * back references that were requested using
@@ -297,7 +314,8 @@ public class ContentProviderOperation implements Parcelable {
if (mType == TYPE_INSERT) { if (mType == TYPE_INSERT) {
Uri newUri = provider.insert(mUri, values); Uri newUri = provider.insert(mUri, values);
if (newUri == null) { if (newUri == null) {
throw new OperationApplicationException("insert failed"); Log.e(TAG, this.toString());
return fail("Insert into " + mUri + " returned no result");
} }
return new ContentProviderResult(newUri); return new ContentProviderResult(newUri);
} }
@@ -329,7 +347,7 @@ public class ContentProviderOperation implements Parcelable {
if (!TextUtils.equals(cursorValue, expectedValue)) { if (!TextUtils.equals(cursorValue, expectedValue)) {
// Throw exception when expected values don't match // Throw exception when expected values don't match
Log.e(TAG, this.toString()); Log.e(TAG, this.toString());
throw new OperationApplicationException("Found value " + cursorValue return fail("Found value " + cursorValue
+ " when expected " + expectedValue + " for column " + " when expected " + expectedValue + " for column "
+ projection[i]); + projection[i]);
} }
@@ -346,7 +364,7 @@ public class ContentProviderOperation implements Parcelable {
if (mExpectedCount != null && mExpectedCount != numRows) { if (mExpectedCount != null && mExpectedCount != numRows) {
Log.e(TAG, this.toString()); Log.e(TAG, this.toString());
throw new OperationApplicationException("wrong number of rows: " + numRows); return fail("Expected " + mExpectedCount + " rows but actual " + numRows);
} }
return new ContentProviderResult(numRows); return new ContentProviderResult(numRows);
@@ -491,6 +509,7 @@ public class ContentProviderOperation implements Parcelable {
private ContentValues mValuesBackReferences; private ContentValues mValuesBackReferences;
private Map<Integer, Integer> mSelectionArgsBackReferences; private Map<Integer, Integer> mSelectionArgsBackReferences;
private boolean mYieldAllowed; private boolean mYieldAllowed;
private boolean mFailureAllowed;
/** Create a {@link Builder} of a given type. The uri must not be null. */ /** Create a {@link Builder} of a given type. The uri must not be null. */
private Builder(int type, Uri uri) { private Builder(int type, Uri uri) {
@@ -683,5 +702,11 @@ public class ContentProviderOperation implements Parcelable {
mYieldAllowed = yieldAllowed; mYieldAllowed = yieldAllowed;
return this; return this;
} }
/** {@hide} */
public Builder withFailureAllowed(boolean failureAllowed) {
mFailureAllowed = failureAllowed;
return this;
}
} }
} }

View File

@@ -16,10 +16,11 @@
package android.content; package android.content;
import android.content.ContentProvider;
import android.net.Uri; import android.net.Uri;
import android.os.Parcelable;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable;
import com.android.internal.util.Preconditions;
/** /**
* Contains the result of the application of a {@link ContentProviderOperation}. It is guaranteed * Contains the result of the application of a {@link ContentProviderOperation}. It is guaranteed
@@ -28,26 +29,44 @@ import android.os.Parcel;
public class ContentProviderResult implements Parcelable { public class ContentProviderResult implements Parcelable {
public final Uri uri; public final Uri uri;
public final Integer count; public final Integer count;
/** {@hide} */
public final String failure;
public ContentProviderResult(Uri uri) { public ContentProviderResult(Uri uri) {
if (uri == null) throw new IllegalArgumentException("uri must not be null"); this(Preconditions.checkNotNull(uri), null, null);
this.uri = uri;
this.count = null;
} }
public ContentProviderResult(int count) { public ContentProviderResult(int count) {
this(null, count, null);
}
/** {@hide} */
public ContentProviderResult(String failure) {
this(null, null, failure);
}
/** {@hide} */
public ContentProviderResult(Uri uri, Integer count, String failure) {
this.uri = uri;
this.count = count; this.count = count;
this.uri = null; this.failure = failure;
} }
public ContentProviderResult(Parcel source) { public ContentProviderResult(Parcel source) {
int type = source.readInt(); if (source.readInt() != 0) {
if (type == 1) { uri = Uri.CREATOR.createFromParcel(source);
count = source.readInt(); } else {
uri = null; uri = null;
}
if (source.readInt() != 0) {
count = source.readInt();
} else { } else {
count = null; count = null;
uri = Uri.CREATOR.createFromParcel(source); }
if (source.readInt() != 0) {
failure = source.readString();
} else {
failure = null;
} }
} }
@@ -55,37 +74,63 @@ public class ContentProviderResult implements Parcelable {
public ContentProviderResult(ContentProviderResult cpr, int userId) { public ContentProviderResult(ContentProviderResult cpr, int userId) {
uri = ContentProvider.maybeAddUserId(cpr.uri, userId); uri = ContentProvider.maybeAddUserId(cpr.uri, userId);
count = cpr.count; count = cpr.count;
failure = cpr.failure;
} }
@Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
if (uri == null) { if (uri != null) {
dest.writeInt(1);
uri.writeToParcel(dest, flags);
} else {
dest.writeInt(0);
}
if (count != null) {
dest.writeInt(1); dest.writeInt(1);
dest.writeInt(count); dest.writeInt(count);
} else { } else {
dest.writeInt(2); dest.writeInt(0);
uri.writeToParcel(dest, 0); }
if (failure != null) {
dest.writeInt(1);
dest.writeString(failure);
} else {
dest.writeInt(0);
} }
} }
@Override
public int describeContents() { public int describeContents() {
return 0; return 0;
} }
public static final @android.annotation.NonNull Creator<ContentProviderResult> CREATOR = public static final @android.annotation.NonNull Creator<ContentProviderResult> CREATOR =
new Creator<ContentProviderResult>() { new Creator<ContentProviderResult>() {
@Override
public ContentProviderResult createFromParcel(Parcel source) { public ContentProviderResult createFromParcel(Parcel source) {
return new ContentProviderResult(source); return new ContentProviderResult(source);
} }
@Override
public ContentProviderResult[] newArray(int size) { public ContentProviderResult[] newArray(int size) {
return new ContentProviderResult[size]; return new ContentProviderResult[size];
} }
}; };
@Override
public String toString() { public String toString() {
final StringBuilder sb = new StringBuilder("ContentProviderResult(");
if (uri != null) { if (uri != null) {
return "ContentProviderResult(uri=" + uri.toString() + ")"; sb.append("uri=" + uri + " ");
} }
return "ContentProviderResult(count=" + count + ")"; if (count != null) {
sb.append("count=" + count + " ");
}
if (uri != null) {
sb.append("failure=" + failure + " ");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(")");
return sb.toString();
} }
} }