package android.content;
import android.annotation.UnsupportedAppUsage;
-import android.content.ContentProvider;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
private final ContentValues mValuesBackReferences;
private final Map<Integer, Integer> mSelectionArgsBackReferences;
private final boolean mYieldAllowed;
+ private final boolean mFailureAllowed;
private final static String TAG = "ContentProviderOperation";
mSelectionArgsBackReferences = builder.mSelectionArgsBackReferences;
mValuesBackReferences = builder.mValuesBackReferences;
mYieldAllowed = builder.mYieldAllowed;
+ mFailureAllowed = builder.mFailureAllowed;
}
private ContentProviderOperation(Parcel source) {
}
}
mYieldAllowed = source.readInt() != 0;
+ mFailureAllowed = source.readInt() != 0;
}
/** @hide */
mSelectionArgsBackReferences = cpo.mSelectionArgsBackReferences;
mValuesBackReferences = cpo.mValuesBackReferences;
mYieldAllowed = cpo.mYieldAllowed;
+ mFailureAllowed = cpo.mFailureAllowed;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(0);
}
dest.writeInt(mYieldAllowed ? 1 : 0);
+ dest.writeInt(mFailureAllowed ? 1 : 0);
}
/**
return mYieldAllowed;
}
+ /** {@hide} */
+ public boolean isFailureAllowed() {
+ return mFailureAllowed;
+ }
+
/** @hide exposed for unit tests */
@UnsupportedAppUsage
public int getType() {
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
* back references that were requested using
if (mType == TYPE_INSERT) {
Uri newUri = provider.insert(mUri, values);
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);
}
if (!TextUtils.equals(cursorValue, expectedValue)) {
// Throw exception when expected values don't match
Log.e(TAG, this.toString());
- throw new OperationApplicationException("Found value " + cursorValue
+ return fail("Found value " + cursorValue
+ " when expected " + expectedValue + " for column "
+ projection[i]);
}
if (mExpectedCount != null && mExpectedCount != numRows) {
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);
private ContentValues mValuesBackReferences;
private Map<Integer, Integer> mSelectionArgsBackReferences;
private boolean mYieldAllowed;
+ private boolean mFailureAllowed;
/** Create a {@link Builder} of a given type. The uri must not be null. */
private Builder(int type, Uri uri) {
mYieldAllowed = yieldAllowed;
return this;
}
+
+ /** {@hide} */
+ public Builder withFailureAllowed(boolean failureAllowed) {
+ mFailureAllowed = failureAllowed;
+ return this;
+ }
}
}
package android.content;
-import android.content.ContentProvider;
import android.net.Uri;
-import android.os.Parcelable;
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
public class ContentProviderResult implements Parcelable {
public final Uri uri;
public final Integer count;
+ /** {@hide} */
+ public final String failure;
public ContentProviderResult(Uri uri) {
- if (uri == null) throw new IllegalArgumentException("uri must not be null");
- this.uri = uri;
- this.count = null;
+ this(Preconditions.checkNotNull(uri), null, null);
}
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.uri = null;
+ this.failure = failure;
}
public ContentProviderResult(Parcel source) {
- int type = source.readInt();
- if (type == 1) {
- count = source.readInt();
+ if (source.readInt() != 0) {
+ uri = Uri.CREATOR.createFromParcel(source);
+ } else {
uri = null;
+ }
+ if (source.readInt() != 0) {
+ count = source.readInt();
} else {
count = null;
- uri = Uri.CREATOR.createFromParcel(source);
+ }
+ if (source.readInt() != 0) {
+ failure = source.readString();
+ } else {
+ failure = null;
}
}
public ContentProviderResult(ContentProviderResult cpr, int userId) {
uri = ContentProvider.maybeAddUserId(cpr.uri, userId);
count = cpr.count;
+ failure = cpr.failure;
}
+ @Override
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(count);
} else {
- dest.writeInt(2);
- uri.writeToParcel(dest, 0);
+ dest.writeInt(0);
+ }
+ if (failure != null) {
+ dest.writeInt(1);
+ dest.writeString(failure);
+ } else {
+ dest.writeInt(0);
}
}
+ @Override
public int describeContents() {
return 0;
}
public static final @android.annotation.NonNull Creator<ContentProviderResult> CREATOR =
new Creator<ContentProviderResult>() {
+ @Override
public ContentProviderResult createFromParcel(Parcel source) {
return new ContentProviderResult(source);
}
+ @Override
public ContentProviderResult[] newArray(int size) {
return new ContentProviderResult[size];
}
};
+ @Override
public String toString() {
+ final StringBuilder sb = new StringBuilder("ContentProviderResult(");
+ if (uri != null) {
+ sb.append("uri=" + uri + " ");
+ }
+ if (count != null) {
+ sb.append("count=" + count + " ");
+ }
if (uri != null) {
- return "ContentProviderResult(uri=" + uri.toString() + ")";
+ sb.append("failure=" + failure + " ");
}
- return "ContentProviderResult(count=" + count + ")";
+ sb.deleteCharAt(sb.length() - 1);
+ sb.append(")");
+ return sb.toString();
}
}