diff --git a/core/java/android/view/contentcapture/ContentCaptureContext.java b/core/java/android/view/contentcapture/ContentCaptureContext.java index 2d2987a035da3..355b18282f50c 100644 --- a/core/java/android/view/contentcapture/ContentCaptureContext.java +++ b/core/java/android/view/contentcapture/ContentCaptureContext.java @@ -86,7 +86,6 @@ public final class ContentCaptureContext implements Parcelable { private final @Nullable Uri mUri; // Fields below are set by server when the session starts - // TODO(b/111276913): create new object for taskId + componentName / reuse on other places private final @Nullable ComponentName mComponentName; private final int mTaskId; private final int mDisplayId; @@ -213,6 +212,7 @@ public final class ContentCaptureContext implements Parcelable { public static final class Builder { private Bundle mExtras; private Uri mUri; + private boolean mDestroyed; /** * Sets extra options associated with this context. @@ -221,11 +221,13 @@ public final class ContentCaptureContext implements Parcelable { * * @param extras extra options. * @return this builder. + * + * @throws IllegalStateException if {@link #build()} was already called. */ @NonNull public Builder setExtras(@NonNull Bundle extras) { - // TODO(b/111276913): check build just once / throw exception / test / document mExtras = Preconditions.checkNotNull(extras); + throwIfDestroyed(); return this; } @@ -236,23 +238,35 @@ public final class ContentCaptureContext implements Parcelable { * * @param uri URI associated with this context. * @return this builder. + * + * @throws IllegalStateException if {@link #build()} was already called. */ @NonNull public Builder setUri(@NonNull Uri uri) { - // TODO(b/111276913): check build just once / throw exception / test / document mUri = Preconditions.checkNotNull(uri); + throwIfDestroyed(); return this; } /** * Builds the {@link ContentCaptureContext}. + * + * @throws IllegalStateException if {@link #build()} was already called or no call to either + * {@link #setExtras(Bundle)} or {@link #setUri(Uri)} was made. + * + * @return the built {@code ContentCaptureContext} */ public ContentCaptureContext build() { - // TODO(b/111276913): check build just once / throw exception / test / document - // TODO(b/111276913): make sure it at least one property (uri / extras) / test / - // throw exception / documment + throwIfDestroyed(); + Preconditions.checkState(mExtras != null || mUri != null, "Must call setUri() " + + "or setExtras() before calling build()"); + mDestroyed = true; return new ContentCaptureContext(this); } + + private void throwIfDestroyed() { + Preconditions.checkState(!mDestroyed, "Already called #build()"); + } } /** diff --git a/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java b/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java index ff97aa1d3914a..7cf5c7ce80440 100644 --- a/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java +++ b/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java @@ -31,7 +31,7 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; /** - * Unit test for {@link ContentCaptureSessionTest}. + * Unit tests for {@link ContentCaptureSession}. * *
To run it: * {@code atest FrameworksCoreTests:android.view.contentcapture.ContentCaptureSessionTest} diff --git a/core/tests/coretests/src/android/view/contentcapture/ViewNodeTest.java b/core/tests/coretests/src/android/view/contentcapture/ViewNodeTest.java index eadde62808488..b84a098574c23 100644 --- a/core/tests/coretests/src/android/view/contentcapture/ViewNodeTest.java +++ b/core/tests/coretests/src/android/view/contentcapture/ViewNodeTest.java @@ -42,7 +42,7 @@ import org.mockito.junit.MockitoJUnitRunner; import java.util.Locale; /** - * Unit test for {@link ViewNode}. + * Unit tests for {@link ViewNode}. * *
To run it: {@code atest FrameworksCoreTests:android.view.contentcapture.ViewNodeTest} */